Viewpager和GridView

主线程的代码

package com.example.caolilu20170821;

import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager vp;
    private GridView gv;
    private List list=new ArrayList();
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int item = vp.getCurrentItem();
            item++;
            vp.setCurrentItem(item);
            send();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp = (ViewPager) findViewById(R.id.vp);
        gv = (GridView) findViewById(R.id.gv);
        init();
    }
    private void init(){
        new Thread(){
            private ByteArrayOutputStream outputStream;
            String path="http://v3.wufazhuce.com:8000/api/reading/index/";
            @Override
            public void run() {
                 super.run();
                try {
                    URL url = new URL(path);
                    HttpURLConnection connction= (HttpURLConnection) url.openConnection();
                    connction.setRequestMethod("GET");
                    connction.setConnectTimeout(5000);
                    int code = connction.getResponseCode();
                    if (code==200){
                        InputStream inputStream = connction.getInputStream();
                        outputStream = new ByteArrayOutputStream();
                        byte[] read=new byte[1024];
                        int le=-1;
                        while((le=inputStream.read(read))!=-1){
                            outputStream.write(read,0,le);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //Toast.makeText(MainActivity.this,outputStream.toString(),Toast.LENGTH_LONG).show();
                        try {
                            JSONObject jsonObject = new JSONObject(outputStream.toString());
                            JSONObject data = jsonObject.getJSONObject("data");
                            JSONArray essay = data.getJSONArray("essay");
                            for (int x=0;x"author");
                                for (int a=0;a"user_name");
                                    String web_url = object1.getString("web_url");
                                    Userbean userbean = new Userbean();
                                    userbean.setUser_name(user_name);
                                    userbean.setWeb_url(web_url);
                                    list.add(userbean);
                                    vp.setAdapter(new Myter());
                                    vp.setCurrentItem(10000);
                                    gv.setAdapter(new Myerer());
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                send();
                            }
                        });
                    }
                });
            }
        }.start();
    }
    private void send(){
        handler.sendEmptyMessageDelayed(0,2000);
    }
    class Myerer extends BaseAdapter{

        private Hnader hnader;

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView==null){
                hnader = new Hnader();
               convertView= View.inflate(MainActivity.this,R.layout.item,null);
               hnader.image= (ImageView) convertView.findViewById(R.id.imagex);
               hnader.text= (TextView) convertView.findViewById(R.id.textx);
                convertView.setTag(hnader);
            }else{
                hnader= (Hnader) convertView.getTag();
            }
            Userbean userbean = list.get(position);
            hnader.text.setText(userbean.getUser_name());
            ImageLoader.getInstance().displayImage(userbean.getWeb_url(),hnader.image);
            return convertView;
        }
        class Hnader{
            ImageView image;
            TextView text;
        }
    }
    class Myter extends PagerAdapter{

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView view = new ImageView(MainActivity.this);
            view.setScaleType(ImageView.ScaleType.FIT_XY);
            DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()
            .cacheOnDisk(true)
            .build();

            ImageLoader.getInstance().displayImage(list.get(position%list.size()).getWeb_url(),view);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }
    }
}
主布局
xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    >

    <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:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:id="@+id/gv"
         android:numColumns="2">GridView>
LinearLayout>

 

你可能感兴趣的:(Viewpager和GridView)