listview의 다중 항목 전시

12497 단어
        listview      ,               

public class MainActivity extends AppCompatActivity {

    private ListView lv;
    private List list=new ArrayList<>();
    private MyAdapter adapter;
    private int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv= (ListView) findViewById(R.id.lv);
        adapter=new MyAdapter();
        lv.setAdapter(adapter);
        LoadData();
    }

    //      
    private void LoadData() {
        RequestParams params=new RequestParams("http://v.juhe.cn/toutiao/index?type=top&key=2f092bd9ce76c0257052d6d3c93c11b4");
        x.http().get(params, new Callback.CommonCallback() {
            @Override
            public void onSuccess(String result) {
                Gson gson=new Gson();
                Data data=gson.fromJson(result,Data.class);
                list.addAll(data.getResult().getData());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    //listview adapter
    class MyAdapter extends BaseAdapter {

        final int TYPE_1 = 0;
        final int TYPE_2 = 1;

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

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

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

        //    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            Data.ResultBean.DataBean ddd = list.get(position);


            type = getItemViewType(position);
            if (convertView == null) {
                holder = new ViewHolder();
                switch (type) {
                    case TYPE_1:
                        convertView = View.inflate(MainActivity.this, R.layout.item1, null);
                        holder.title = (TextView) convertView.findViewById(R.id.title);
                        holder.image1 = (ImageView) convertView.findViewById(R.id.image1);
                        break;

                    case TYPE_2:
                        convertView = View.inflate(MainActivity.this, R.layout.item3, null);
                        holder.title = (TextView) convertView.findViewById(R.id.title);
                        holder.image1 = (ImageView) convertView.findViewById(R.id.image1);
                        holder.image2 = (ImageView) convertView.findViewById(R.id.image2);
                        holder.image3 = (ImageView) convertView.findViewById(R.id.image3);
                        break;
                }
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.title.setText(ddd.getTitle());
            switch (type) {
                case TYPE_1:
                    Glide.with(MainActivity.this).load(ddd.getThumbnail_pic_s()).into(holder.image1);
                    break;
                case TYPE_2:
                    Glide.with(MainActivity.this).load(ddd.getThumbnail_pic_s()).into(holder.image1);
                    Glide.with(MainActivity.this).load(ddd.getThumbnail_pic_s02()).into(holder.image2);
                    Glide.with(MainActivity.this).load(ddd.getThumbnail_pic_s03()).into(holder.image3);
                    break;
            }

            return convertView;
        }

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

        @Override
        public int getItemViewType(int position) {
            String p2 = list.get(position).getThumbnail_pic_s02();
            if (p2 == "" || p2 == null) {
                return TYPE_1;
            } else {
                return TYPE_2;
            }
        }
    }

    class ViewHolder{
        TextView title;
        ImageView image1,image2,image3;

    }
}
      :

좋은 웹페이지 즐겨찾기