listview多条目加载

//MainActivity

public class MainActivity extends AppCompatActivity {

    private String newsUrl = "http://v.juhe.cn/toutiao/index?type=&key=2f41498b35e69877fc56dc96776e5d1f";
    private ListView main_listview;
    private static final String TAG = "MainActivity=====";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化界面
        initViews();
        //从网络获取数据
        getDatasFromNet();
    }

    private void getDatasFromNet() {
        //实例化HttpUtils
        HttpUtils httpUtils = HttpUtils.getInstance();
        //进行网络请求
        httpUtils.get(newsUrl);
        //设置监听,接口回调
        httpUtils.setHttpUtilsListener(new HttpUtils.HttpUtilsListener() {
            @Override
            public void getDataSuccess(String json) {
                Log.d(TAG, "getDataSuccess: " + json);
                Gson gson = new Gson();
                Bean bean = gson.fromJson(json, Bean.class);
                //设置适配器
                List<Bean.ResultBean.DataBean> list = bean.getResult().getData();
                MyAdapter myAdapter = new MyAdapter(MainActivity.this, list);
                main_listview.setAdapter(myAdapter);
            }

            @Override
            public void getDataError(String error) {
                Log.d(TAG, "getDataError: " + error);
            }
        });
    }

    private void initViews() {
        main_listview = findViewById(R.id.main_listview);
    }

}


//MyApp

public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        initImageLoader();
    }

    private void initImageLoader() {
        //创建默认的ImageLoader配置参数
        ImageLoaderConfiguration configuration = ImageLoaderConfiguration
                .createDefault(this);

        //Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(configuration);
    }

    public static DisplayImageOptions getOptions() {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheInMemory(true) // default
                .cacheOnDisk(true) // default
                .bitmapConfig(Bitmap.Config.RGB_565) // default
                .displayer(new SimpleBitmapDisplayer()) // default
                .displayer(new FadeInBitmapDisplayer(500))
                .handler(new Handler()) // default
                .build();
        return options;
    }
}

//HttpUtils

public class HttpUtils {
    private static final String TAG = "HttpUtils----";
    //单利模式
    private static HttpUtils httpUtils = new HttpUtils();
    private final static int SUCCESS = 0;
    private final static int ERROR = 1;
    private MyHandler myHandler = new MyHandler();
    private HttpUtilsListener httpUtilsListener;

    public static HttpUtils getInstance() {
        if (httpUtils == null) {
            httpUtils = new HttpUtils();
        }
        return httpUtils;
    }

    //封装get
    public void get(final String url) {
        new Thread() {
            @Override
            public void run() {
                //进行网络请求
                try {
                    URL url1 = new URL(url);
                    HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
                    connection.setConnectTimeout(3000);
                    if (connection.getResponseCode() == 200) {
                        InputStream inputStream = connection.getInputStream();
                        //inputstream--string
                        String json = inputStreamToString(inputStream);
                        //发送
                        Message message = myHandler.obtainMessage();
                        message.what = SUCCESS;
                        message.obj = json;
                        myHandler.sendMessage(message);

                    } else {
                        Message message = myHandler.obtainMessage();
                        message.what = ERROR;
                        message.obj = "失败";
                        myHandler.sendMessage(message);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Message message = myHandler.obtainMessage();
                    message.what = ERROR;
                    message.obj = e.getMessage();
                    myHandler.sendMessage(message);
                }
            }
        }.start();
    }

    //使用Handler 处理线程问题
    class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SUCCESS:
                    //成功
                    String json = (String) msg.obj;
                    Log.d(TAG, "handleMessage: " + json);
                    //使用回调
                    httpUtilsListener.getDataSuccess(json);
                    break;
                case ERROR:
                    //成功
                    String error = (String) msg.obj;
                    Log.d(TAG, "handleMessage: " + error);
                    //使用回调
                    httpUtilsListener.getDataError(error);
                    break;
            }
        }
    }

    public String inputStreamToString(InputStream inputStream) throws IOException {
        int len = 0;
        byte[] butter = new byte[1024];
        StringBuffer stringBuffer = new StringBuffer();
        while ((len = inputStream.read(butter)) != -1) {
            String s = new String(butter, 0, len);
            stringBuffer.append(s);

        }
        return stringBuffer.toString();
    }

    //接口回调
    public interface HttpUtilsListener {
        void getDataSuccess(String json);

        void getDataError(String error);
    }

    //外部访问的方法
    public void setHttpUtilsListener(HttpUtilsListener httpUtilsListener) {
        this.httpUtilsListener = httpUtilsListener;
    }

}



//适配器

public class MyAdapter extends BaseAdapter {
    private Context context;
    private List<Bean.ResultBean.DataBean> list;
    private static final int TYPE_1 = 0;
    private static final int TYPE_2 = 1;

    public MyAdapter(Context context, List<Bean.ResultBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @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) {
        //获取视图类型
        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case TYPE_1:
                ViewHoderType1 viewHoderType1;
                //类型1  一张图片的
                if (convertView==null){
                    convertView=View.inflate(context,R.layout.item_one,null);
                    ImageView imageView=convertView.findViewById(R.id.item1_pic);
                    TextView textView=convertView.findViewById(R.id.item1_tv);
                    viewHoderType1=new ViewHoderType1(imageView,textView);
                    convertView.setTag(viewHoderType1);
                }else {
                    viewHoderType1= (ViewHoderType1) convertView.getTag();
                }
                //赋值
                viewHoderType1.getType1_tv().setText(list.get(position).getTitle());
                String thumbnail_pic_s = list.get(position).getThumbnail_pic_s();
                //加载图片
                ImageLoader.getInstance().displayImage(thumbnail_pic_s,viewHoderType1.getType1_pic(),MyApp.getOptions());
                break;
            case TYPE_2:
                ViewHolderType2 viewHolderType2;
                //类型2  三张图片的
                //类型1  一张图片的
                if (convertView==null){
                    convertView=View.inflate(context,R.layout.item_two,null);
                    ImageView imageView1=convertView.findViewById(R.id.item2_pic1);
                    ImageView imageView2=convertView.findViewById(R.id.item2_pic2);
                    ImageView imageView3=convertView.findViewById(R.id.item2_pic3);
                    TextView textView=convertView.findViewById(R.id.item2_tv);
                    viewHolderType2=new ViewHolderType2(textView,imageView1,imageView2,imageView3);
                    convertView.setTag(viewHolderType2);
                }else {
                    viewHolderType2= (ViewHolderType2) convertView.getTag();
                }
                //赋值
                viewHolderType2.getType2_tv().setText(list.get(position).getTitle());
                String thumbnail_pic_s1 = list.get(position).getThumbnail_pic_s();
                String thumbnail_pic_s2 = list.get(position).getThumbnail_pic_s02();
                String thumbnail_pic_s3 = list.get(position).getThumbnail_pic_s03();
                //加载图片
                ImageLoader.getInstance().displayImage(thumbnail_pic_s1,viewHolderType2.getType2_pic1(),MyApp.getOptions());
                ImageLoader.getInstance().displayImage(thumbnail_pic_s2,viewHolderType2.getType2_pic2(),MyApp.getOptions());
                ImageLoader.getInstance().displayImage(thumbnail_pic_s3,viewHolderType2.getType2_pic3(),MyApp.getOptions());
                break;
        }
        return convertView;
    }

    //获取条目视图的类型
    //什么类型是根据数据来的,而不会想象的。
    @Override
    public int getItemViewType(int position) {
        Bean.ResultBean.DataBean dataBean = list.get(position);
        String thumbnail_pic_s03 = dataBean.getThumbnail_pic_s03();
        //根据有没有第三张图片,判断类型
        //如果有,就是类型2,如果没有,那就是类型1
        if (TextUtils.isEmpty(thumbnail_pic_s03)) {
            return TYPE_1;
        }
        return TYPE_2;
    }

    //视图类型的数量
    @Override
    public int getViewTypeCount() {
        return 2;
    }
    //类型1 的ViewHolder
    class ViewHoderType1{
        private ImageView type1_pic;
        private TextView type1_tv;

        public ViewHoderType1(ImageView type1_pic, TextView type1_tv) {
            this.type1_pic = type1_pic;
            this.type1_tv = type1_tv;
        }

        public ImageView getType1_pic() {
            return type1_pic;
        }

        public void setType1_pic(ImageView type1_pic) {
            this.type1_pic = type1_pic;
        }

        public TextView getType1_tv() {
            return type1_tv;
        }

        public void setType1_tv(TextView type1_tv) {
            this.type1_tv = type1_tv;
        }
    }
    //类型2 的ViewHolder
    class ViewHolderType2{
        private TextView type2_tv;
        private ImageView type2_pic1;
        private ImageView type2_pic2;
        private ImageView type2_pic3;

        public ViewHolderType2(TextView type2_tv, ImageView type2_pic1, ImageView type2_pic2, ImageView type2_pic3) {
            this.type2_tv = type2_tv;
            this.type2_pic1 = type2_pic1;
            this.type2_pic2 = type2_pic2;
            this.type2_pic3 = type2_pic3;
        }

        public TextView getType2_tv() {
            return type2_tv;
        }

        public void setType2_tv(TextView type2_tv) {
            this.type2_tv = type2_tv;
        }

        public ImageView getType2_pic1() {
            return type2_pic1;
        }

        public void setType2_pic1(ImageView type2_pic1) {
            this.type2_pic1 = type2_pic1;
        }

        public ImageView getType2_pic2() {
            return type2_pic2;
        }

        public void setType2_pic2(ImageView type2_pic2) {
            this.type2_pic2 = type2_pic2;
        }

        public ImageView getType2_pic3() {
            return type2_pic3;
        }

        public void setType2_pic3(ImageView type2_pic3) {
            this.type2_pic3 = type2_pic3;
        }
    }
}




阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭