Myapp.java(設(shè)置ImageLoader)_
import java.io.File;
import android.app.Application;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
File file = this.getCacheDir();
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
this)
// max width, max height,即保存的每個緩存文件的最大長寬
.memoryCacheExtraOptions(480, 800)
// 線程池內(nèi)加載的數(shù)量
.threadPoolSize(3)
// 線程優(yōu)先級
.threadPriority(Thread.NORM_PRIORITY - 2)
.defaultDisplayImageOptions(options)
// You can pass your own memory cache implementation你可以通過自己的內(nèi)存緩存實現(xiàn)
// .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
// .memoryCacheSize(2 * 1024 * 1024)
//硬盤緩存50MB
.diskCacheSize(50 * 1024 * 1024)
//將保存的時候的URI名稱用MD5
.diskCacheFileNameGenerator(new Md5FileNameGenerator())
// 加密
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())//將保存的時候的URI名稱用HASHCODE加密
.tasksProcessingOrder(QueueProcessingType.LIFO)
.diskCacheFileCount(100) //緩存的File數(shù)量
.diskCache(new UnlimitedDiscCache(file))// 自定義緩存路徑
// .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
// .imageDownloader(new BaseImageDownloader(context, 5 * 1000,
// 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超時時間
.writeDebugLogs() // Remove for release app
.build();
ImageLoader.getInstance().init(config);
}
}
MainActivity.java
package com.example.liuentong20170821;
import android.content.Intent;
import android.media.Image;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import bean.Max;
public class MainActivity extends AppCompatActivity {
Handler handler=new Handler();
GridView gv;
Max m;
int index=0;
ViewPager vp;
MyAdapter myAdapter;
List<ImageView> listvp;
List<Max.DataBean.EssayBean> list=new ArrayList<Max.DataBean.EssayBean>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv = (GridView) findViewById(R.id.gv);
vp=(ViewPager)findViewById(R.id.vp);
initData();//grideview設(shè)置初始化
myAdapter= new MyAdapter();
gv.setAdapter(myAdapter);
}
//grideview設(shè)置初始化
private void initData() {
new AsyncTask<String,String,String>(){
@Override
protected String doInBackground(String... strings) {
getjsonData();
return null;
}
@Override
protected void onPostExecute(String s) {
myAdapter.notifyDataSetChanged();
initvpData();//viewpager設(shè)置初始化
super.onPostExecute(s);
}
}.execute();
}
//viewpager設(shè)置初始化
private void initvpData() {
listvp=new ArrayList<ImageView>();
if(m!=null){
for(int i=0;i<m.getData().getEssay().size();i++){
ImageView iv = new ImageView(this);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
ImageLoader.getInstance().displayImage(m.getData().getEssay().get(i).getAuthor().get(0).getWeb_url(),iv);
listvp.add(iv);
}
}
MyVpAdapter myVpAdapter = new MyVpAdapter();
vp.setAdapter(myVpAdapter);
//handler延遲實現(xiàn)自動輪播
handler.postDelayed(new Runnable() {
@Override
public void run() {
index=vp.getCurrentItem();//index全局
index++;
vp.setCurrentItem(index);
handler.postDelayed(this,4000);//發(fā)送延遲實現(xiàn)無限自動
}
},4000);
}
class MyVpAdapter extends PagerAdapter{
@Override
public int getCount() {
if(m.getData().getEssay()!=null){
// return m.getData().getEssay().size();
return Integer.MAX_VALUE;//返回最大值---無限輪播
}
return 0;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
//position模上總長度
int index=position%m.getData().getEssay().size();
container.addView(listvp.get(index));
return listvp.get(index);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
if(m!=null){
return m.getData().getEssay().size();
}
return 0;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if(view==null){
view=View.inflate(MainActivity.this,R.layout.gv_item,null);
holder=new ViewHolder();
holder.tv_name= (TextView) view.findViewById(R.id.tv_name);
holder.iv_name= (ImageView) view.findViewById(R.id.iv_name);
view.setTag(holder);
}else{
holder= (ViewHolder) view.getTag();
}
holder.tv_name.setText(m.getData().getEssay().get(i).getAuthor().get(0).getUser_name());
ImageLoader.getInstance().displayImage(m.getData().getEssay().get(i).getAuthor().get(0).getWeb_url(),holder.iv_name);
return view;
}
}
class ViewHolder{
TextView tv_name;
ImageView iv_name;
}
private void getjsonData() {
try {
URL url=new URL("http://v3.wufazhuce.com:8000/api/reading/index/?version=3.5.0&platform=android");
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
int code = conn.getResponseCode();
if(code==200){
Gson gson = new Gson();
m = gson.fromJson(new InputStreamReader(conn.getInputStream()), Max.class);
list=m.getData().getEssay();
Log.i("*********************", m.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
mainactivity布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.liuentong20170821.MainActivity">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/vp">
</android.support.v4.view.ViewPager>
<GridView
android:id="@+id/gv"
android:layout_below="@+id/vp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="2"></GridView>
</RelativeLayout>
grideview的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_name"
android:textSize="15sp"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/iv_name"/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.liuentong20170821">
<uses-permission android:name="android.permission.INTERNET"/>//************
<application
android:name=".application"http://***********
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>