An_GrideView+viewPager(无限轮播,自动)+轮播图导入sd卡

Myapp.java(设置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)  
                // 线程池内加载的数量  
                .threadPoolSize(3)  
                // 线程优先级  
                .threadPriority(Thread.NORM_PRIORITY - 2)  
                .defaultDisplayImageOptions(options)
                // You can pass your own memory cache implementation你可以通过自己的内存缓存实现  
                // .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数量  
                .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 listvp;
    List list=new ArrayList();
    @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设置初始化
        myAdapter= new MyAdapter();
        gv.setAdapter(myAdapter);

    }
    //grideview设置初始化
    private void initData() {
        new AsyncTask(){

            @Override
            protected String doInBackground(String... strings) {
                getjsonData();
                return null;
            }

            @Override
            protected void onPostExecute(String s) {
                myAdapter.notifyDataSetChanged();
                initvpData();//viewpager设置初始化
                super.onPostExecute(s);
            }
        }.execute();
    }
    //viewpager设置初始化
    private void initvpData() {
        listvp=new ArrayList();
        if(m!=null){
            for(int i=0;i

mainactivity布局文件




    

    
    


grideview的布局




    

AndroidManifest.xml



//************
    
        
            
                

                
            
        
    


你可能感兴趣的:(An_GrideView+viewPager(无限轮播,自动)+轮播图导入sd卡)