Listview多条目显示

public class MainActivity extends AppCompatActivity {
    //设置三种类型,对应我不同类型Item 数字随意生成 ,主要为了区分
    private static final int FristType = 451;
    private static final int SecondType = 357;
    private static final int ThirdType = 114;
private ListView listView;
    String[] texts = {"玉皇", "王母", "长蛾", "八戒", "如来", "易宸锋", "守星者", "部长", "大师", "收藏家"};
    private int[] images = {R.drawable.jx_left_listitem_1, R.drawable.jx_left_listitem_5,
            R.drawable.jx_left_listitem_2, R.drawable.jx_left_listitem_3,
            R.drawable.jx_left_listitem_4, R.drawable.jx_left_listitem_5,
            R.drawable.jx_left_listitem_6, R.drawable.jx_left_listitem_6,
            R.drawable.jx_left_listitem_4, R.drawable.jx_left_listitem_5};
private List list;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView=(ListView)findViewById(R.id.mylist);//查找控件
        initData();//初始化数据
        listView.setAdapter(new MyAdapter());
    }
//这里是没有多条目的类型,手动我分一下,分为三个类型
    private void initData() {
       list= new ArrayList();
        for(int x=0;x<10;x++){
            DataBean  data=new DataBean();
            //第一种类型,也就是2等等
            if (x%2 ==0){
             data.setType(FristType);
             data.setText(texts[x]);

            }//第二种类型
            else if(x%3 ==0){
              data.setType(SecondType);
                data.setText(texts[x]);
                data.setImages(images[x]);
            }//第三种类型
            else{
              data.setType(ThirdType);
                data.setText(texts[x]);
                data.setImages(images[x]);
            }
             list.add(data);
        }
    }

    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return 0;
        }

        @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) {
            return null;
        }

        @Override
        public int getItemViewType(int position) {
            return list.get(position).getType();
        }

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

}

public class DataBean {
    //设置一个类型
private  int type;
    //item要用到的属性
    //设置图片信息
 private int images;
    ////设置文本信息
 private String text;

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getImages() {
        return images;
    }

    public void setImages(int images) {
        this.images = images;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

你可能感兴趣的:(Listview多条目显示)