1. 程式人生 > >多條目載入

多條目載入

需要重寫getViewTypeCount,getItemViewType兩個方法

  private void initData() {
        //這個過程是在子線中,請求網路比較慢,有可能8秒;
        new MAsyncTask().execute(url);
    }

    /**
     *
     */
    private class MAdapter extends BaseAdapter {

        /**
         * 有多少種類型;
         *
         * @return
         */
        @Override
        public int getViewTypeCount() {
            return 2;
        }

        /**
         * 條目的型別用int值來區分, 從0開始排序,0123....
         *
         * @param position
         * @return
         */
        @Override
        public int getItemViewType(int position) {

            if (position % 2 == 0) {
                return 0;
            } else {
                return 1;
            }

        }


        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {

            int viewType = getItemViewType(position);
            switch (viewType) {
                case 0: //偶數條目 返回文字+圖片條目;
                    ViewHolder0 viewHolder0;
                    if (view == null) {
                        viewHolder0 = new ViewHolder0();
                        Log.e("wzq", "偶數賦值");
                        view = View.inflate(MainActivity.this, R.layout.lv_item_00, null);
                        viewHolder0.textView1 = view.findViewById(R.id.textView1);
                        viewHolder0.textView2 = view.findViewById(R.id.textView2);
                        viewHolder0.imageView = view.findViewById(R.id.imageView);
                        view.setTag(viewHolder0);
                    } else {
                        viewHolder0 = (ViewHolder0) view.getTag();
                    }
                    viewHolder0.textView1.setText(list.get(position).getNews_id());
                    viewHolder0.textView2.setText(list.get(position).getNews_title());
                    imageLoaderInstance.displayImage(list.get(position).getPic_url(), viewHolder0.imageView);


                    break;
                case 1: // 奇數條,只返回文字條目
                    ViewHolder1 viewHolder1 = null;
                    if (view == null) {
                        viewHolder1 = new ViewHolder1();
                        Log.e("wzq", "奇數賦值");
                        view = View.inflate(MainActivity.this, R.layout.lv_item_11, null);
                        viewHolder1.tv1 = (TextView) view.findViewById(R.id.tv1);
                        viewHolder1.tv2 = (TextView) view.findViewById(R.id.tv2);
                        view.setTag(viewHolder1);
                    } else {
                        viewHolder1 = (ViewHolder1) view.getTag();
                    }
                    viewHolder1.tv1.setText(list.get(position).getNews_id());
                    viewHolder1.tv1.setText(list.get(position).getNews_title());

                    break;

            }

            return view;
        }


        @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;
        }

    }