ListView的多種item的實現方式

KelvinWalke 7年前發布 | 17K 次閱讀 ListView Android開發 移動開發

使用ListView一般步驟:

  1. 設置顯示的ListView,設置顯示的每一項item的view布局文件
  2. 設置每個item顯示的數據
  3. 將數據顯示的View中,繼承BaseAdapter,重寫 getCount() , getItemId() , getItem() , getView() 這個四個方法;

如果實現ListView的多種類型item的顯示,那么就要再重寫兩個方法

getViewTypeCount():得到總共item的顯示的種類數,

getItemViewType():得到每個item顯示的類型;為整型數據;

實現的效果如下:

ListView的多種item實現.png

一、準備填充的數據模型

1、解析json數據源

json數據放在res下的raw文件夾下:

[
  {
    "letter": "A",
    "cities": [
      "安慶",
      "安徽",
      "安全"
    ]
  },
  {
    "letter": "B",
    "cities": [
      "包頭",
      "寶鋼",
      "渤海",
      "本溪",
      "蚌埠"
    ]
  },
  {
    "letter": "C",
    "cities": [
      "長春",
      "長城",
      "長沙",
      "常州",
      "郴州",
      "重慶"
    ]
  },
  {
    "letter": "D",
    "cities": [
      "東莞",
      "東山",
      "大連",
      "大慶"
    ]
  }
]

2、建立數據對象

可以看到這個ListView有兩種類型,一個是顯示字母,一個是顯示內容,所以數據模型的建立如下,使用int型的type對數據類型進行標識;標識的值必須從0開始計數,有兩種類型,那么就取 0,1 這兩個值;

public class StringBean {
    String letter;
    String city;
    int type;
    public String getLetter() {
        return letter;
    }
    public void setLetter(String letter) {
        this.letter = letter;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public int getType() {
        return type;
    }
    public void setType(int type) {
        this.type = type;
    }
    @Override
    public String toString() {
        return "StringBean{" +
                "letter='" + letter + '\'' +
                ", city='" + city + '\'' +
                ", type=" + type +
                '}';
    }
}

解析json數據填充成集合數據源這里就不提供了

二、準備兩種item類型的布局文件

1、顯示字母的type_layout.xml的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tvType"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#999"
        android:text="A"
        android:textSize="20sp" />
</LinearLayout>

2、顯示城市city_layout.xml的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tvCity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:textSize="20sp"
        />
</LinearLayout>

三、設置顯示ListView的數據和布局的適配器

這里的ListView的item有兩種類型,所以 getViewTypeCount() 返回2;

getItemViewType() 返回的是每次繪制每一個item的view顯示的是何種類型,在數據模型StringBean有設置;

關于類型的整型設置,可能有很多人認為只要是任意的整型數字就可以了,其實不是這樣

item類型標識值 必須從0開始 計數,如果item有兩種類型,那么類型標識值就是 0,1

如果是不從0開始標識,那么會報ArrayIndexOutOfBoundsException數組下標越界的異常

public class ListAdapter extends BaseAdapter {
    ArrayList<StringBean>list;
    Context context;
    LayoutInflater inflater;
    ListAdapter(ArrayList<StringBean>list,Context context){
        this.list=list;
        this.context=context;
        inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @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;
    }
    @Override
    public View getView(int position, View converView, ViewGroup viewGroup) {
        View view=converView;
        StringBean bean=list.get(position);
        int type=bean.getType();
        if(type==0){
            if(view==null){
                view=inflater.inflate(R.layout.type_layout,viewGroup,false);
            }
            TextView type_text= (TextView) view.findViewById(R.id.tvType);
            type_text.setText(bean.getLetter());
        }else if (type==1){
            if(converView==null){
                view=inflater.inflate(R.layout.city_layout,viewGroup,false);
            }
            TextView city_text= (TextView) view.findViewById(R.id.tvCity);
            city_text.setText(bean.getCity());
        }
        return view;
    }
    @Override
    public int getItemViewType(int i) {
        return list.get(i).getType();
    }
    @Override
    public int getViewTypeCount() {
        return 2;
    }
}

四、設置ListView

ListView的布局文件,在這里就不給出了

public class MainActivity extends AppCompatActivity {
    ArrayList<StringBean> list;
    ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initBean();
        initView();
    }
    public void initBean(){
        UserDao dao=new UserDao(this);
        list=dao.getList();
    }
    public void initView(){
        listView= (ListView) findViewById(R.id.listView);
       ListAdapter adapter=new ListAdapter(list,this);
        listView.setAdapter(adapter);
    }
}

 

來自:http://www.jianshu.com/p/dd34306e9cb1

 

 本文由用戶 KelvinWalke 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!