XLView内镶嵌viewpager无限轮播 AND 多条目

package com.bwie.skn.zhoukao03_1;

import android.os.AsyncTask;
import android.os.Bundle;
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.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bawei.wzq.wzlibrary.utils.NetWorkUtil;
import com.bawei.wzq.wzlibrary.view.XListView;
import com.bawei.wzq.wzlibrary.view.XListView.IXListViewListener;
import com.bwie.skn.zhoukao03_1.bean.JsonBean;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements IXListViewListener{
    private XListView xlv;
    private List<JsonBean.DataBean.AdlistBean> list = new ArrayList<>();
    private MyXLAdapter myXLAdapter;
    private View headerView;
    private ViewPager vp;
    private LinearLayout ll;
    private List<ImageView> ivList = new ArrayList<>();
    private MyVPAdapter myVPAdapter;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int i = vp.getCurrentItem();
            i++;
            vp.setCurrentItem(i);
            sendEmptyMessageDelayed(0,2000);
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化数据
        headerView = View.inflate(MainActivity.this,R.layout.xl_item0_layout,null);
        initView();
        xlv.addHeaderView(headerView);
        //适配
        myXLAdapter = new MyXLAdapter();
        xlv.setAdapter(myXLAdapter);
        myVPAdapter = new MyVPAdapter();
        //网络获取数据
        initData();
    }

    private void initData() {
        new MyAsyncTaskString().execute("http://www.meirixue.com/api.php?c=index&a=index");
    }
    //网络获取数据
    class MyAsyncTaskString extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... strings) {
            String s = new NetWorkUtil().getNetString(strings[0]);
            return s;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson = new Gson();
            JsonBean jsonBean = gson.fromJson(s, JsonBean.class);
            List<JsonBean.DataBean.AdlistBean> adlist = jsonBean.getData().getAdlist();
            list.addAll(adlist);
            myXLAdapter.notifyDataSetChanged();

            vp.setAdapter(myVPAdapter);
            //初始化点
            initDot();
            vp.setCurrentItem(1000*list.size());
            handler.sendEmptyMessageDelayed(0,2000);
            ViewPagerListener();
        }
    }
    //VP监听
    private void ViewPagerListener() {
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for(int i =0;i<ivList.size();i++){
                    if(position%list.size()==i){
                        ivList.get(position%list.size()).setImageResource(R.drawable.dot_focuable);
                    }else{
                        ivList.get(i).setImageResource(R.drawable.dot_normal);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    //初始化

    private void initDot() {
        if(ivList!=null){
            ivList.clear();
        }
        for(int x =0;x<list.size();x++){
            ImageView imageView = new ImageView(MainActivity.this);
            if(x==0){
                imageView.setImageResource(R.drawable.dot_focuable);
            }else{
                imageView.setImageResource(R.drawable.dot_normal);
            }

            ivList.add(imageView);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20);
            params.setMargins(10,0,10,0);
            ll.addView(imageView,params);
        }
    }

    //适配器
    class MyXLAdapter extends BaseAdapter{

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

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

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

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            int type = getItemViewType(i);
            ViewHordel1 vh1;
            ViewHordel2 vh2;
                switch(type){
                        case 0:
                            if(view == null){
                                vh1 = new ViewHordel1();
                                view = View.inflate(MainActivity.this, R.layout.xl_item1_layout,null);
                                vh1.image1= (ImageView) view.findViewById(R.id.image1);
                                vh1.tv = (TextView) view.findViewById(R.id.tv);
                                view.setTag(vh1);
                            }else{
                                vh1 = (ViewHordel1) view.getTag();
                            }
                            ImageLoader.getInstance().displayImage(list.get(i).getImg(),vh1.image1);
                            vh1.tv.setText(list.get(i).getTitle());
                        break;
                        case 1:
                            if(view == null){
                                vh2 = new ViewHordel2();
                                view = View.inflate(MainActivity.this, R.layout.xl_item2_layout,null);
                                vh2.image1= (ImageView) view.findViewById(R.id.image1);
                                vh2.image2=(ImageView) view.findViewById(R.id.image2);
                                vh2.tv = (TextView) view.findViewById(R.id.tv);
                                view.setTag(vh2);
                            }else{
                                vh2 = (ViewHordel2) view.getTag();
                            }
                            ImageLoader.getInstance().displayImage(list.get(i).getImg(),vh2.image1);
                            ImageLoader.getInstance().displayImage(list.get(i).getImg(),vh2.image2);
                            vh2.tv.setText(list.get(i).getTitle());
                        break;


                    }
            return view;
        }

        @Override
        public int getItemViewType(int position) {
            if(position%2==0){
                return 0;
            }else{
                return 1;
            }
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }
    }

    class ViewHordel1{
        ImageView image1;
        TextView tv;
    }
    class ViewHordel2{
        ImageView image1;
        TextView tv;
        ImageView image2;
    }
    class ViewHordel0{
        ViewPager vp;
        LinearLayout ll;
    }

    private void initView() {
        xlv = (XListView) findViewById(R.id.xlv);
        vp =(ViewPager) headerView.findViewById(R.id.vp);
        ll = (LinearLayout) headerView.findViewById(R.id.ll);
        xlv.setPullLoadEnable(true);
        xlv.setXListViewListener(this);
    }

    public void onLoad(){
        xlv.stopRefresh();
        xlv.stopLoadMore();
        xlv.setRefreshTime("刚刚");
    }
    /**
     * 下拉刷新
     */
    @Override
    public void onRefresh() {
        list.clear();
        initData();
        ll.removeAllViews();
        onLoad();
    }

    /**
     * 上拉加载
     */
    @Override
    public void onLoadMore() {
        ivList.clear();
        initData();
        onLoad();
    }

    class MyVPAdapter extends PagerAdapter{

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

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(MainActivity.this);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            ImageLoader.getInstance().displayImage(list.get(position%list.size()).getImg(),imageView);
            container.addView(imageView);
            return imageView;
        }
    }
}

你可能感兴趣的:(android)