另一种加载


import android.os.AsyncTask;
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.ImageView;
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.util.ArrayList;
import java.util.List;

import bean.Max;
import view.XListView;

public class MainActivity extends AppCompatActivity {
    Max max;
    ViewPager vp;
    MyAdapterxlv myAdapterxlv;
    XListView xlv;
    List listiv=new ArrayList();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageLoaderConfiguration config=ImageLoaderConfiguration.createDefault(this);
        ImageLoader.getInstance().init(config);
        vp=(ViewPager) findViewById(R.id.vp);
        xlv=(XListView) findViewById(R.id.xlv);
        initData();

        xlv.setPullRefreshEnable(true);
        xlv.setPullLoadEnable(true);
        xlv.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                getjsonbybean();
                xlv.stopRefresh();
            }

            @Override
            public void onLoadMore() {
                getjsonbybean();
                xlv.stopLoadMore();
            }
        });
    }
    class MyAdapterxlv extends BaseAdapter{
        public static final int One=0;
        public static final int Two=1;

        @Override
        public int getCount() {
            if(max.getResult().getIndexProducts()!=null){
                return max.getResult().getIndexProducts().size();
            }
            return 0;
        }

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

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

        @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) {
            ViewHolder1 holder1 = null;
            ViewHolder2 holder2 = null;
            int type = getItemViewType(i);
            if(view==null){
                if(type==One){
                    view=View.inflate(MainActivity.this,R.layout.xlv_item,null);
                    holder1=new ViewHolder1();
                    holder1.iv1= (ImageView) view.findViewById(R.id.iv1);
                    holder1.tv1= (TextView) view.findViewById(R.id.tv1);
                    view.setTag(holder1);
                }else{
                    view=View.inflate(MainActivity.this,R.layout.xlv_item2,null);
                    holder2=new ViewHolder2();
                    holder2.iv2= (ImageView) view.findViewById(R.id.iv2);
                    holder2.tv2= (TextView) view.findViewById(R.id.tv2);
                    view.setTag(holder2);
                }
            }else{
                if(type==One){
                    holder1= (ViewHolder1) view.getTag();
                }else if(type==Two){
                    holder2= (ViewHolder2) view.getTag();
                }
            }
            if(type==One){
                ImageLoader.getInstance().displayImage(max.getResult().getIndexProducts().get(i).getPic(),holder1.iv1);
                holder1.tv1.setText(max.getResult().getIndexProducts().get(i).getName());
            }else{
                ImageLoader.getInstance().displayImage(max.getResult().getIndexProducts().get(i).getPic(),holder2.iv2);
                holder2.tv2.setText(max.getResult().getIndexProducts().get(i).getName());
            }
            return view;
        }
    }
    class ViewHolder1{
        ImageView iv1;
        TextView tv1;
    }
    class ViewHolder2{
        ImageView iv2;
        TextView tv2;
    }
    private void initData() {
        new AsyncTask(){

            @Override
            protected String doInBackground(String... strings) {
                getjsonbybean();

                return null;
            }

            @Override
            protected void onPostExecute(String s) {
                initvpData();
                myAdapterxlv= new MyAdapterxlv();
                xlv.setAdapter(myAdapterxlv);
                myAdapterxlv.notifyDataSetChanged();
                super.onPostExecute(s);
            }
        }.execute();
    }

    private void initvpData() {
        if(max.getResult().getIndexProducts()!=null){
            for(int i=0;i

你可能感兴趣的:(另一种加载)