溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么使用ExpandListView實現(xiàn)下拉列表

發(fā)布時間:2022-08-26 09:37:38 來源:億速云 閱讀:110 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“怎么使用ExpandListView實現(xiàn)下拉列表”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么使用ExpandListView實現(xiàn)下拉列表”吧!

使用方式與ListView類似,是ListView的一個延申,Group為TextView,子元素為ListView。

代碼:

實體類:

Group.java

public class Group {
    private String gName;

    public Group() {
    }

    public Group(String gName) {
        this.gName = gName;
    }

    public String getName() {
        return gName;
    }

    public void setName(String gName) {
        this.gName = gName;
    }
}

Item.java

public class Item {

    private int iId;
    private String iName;

    public Item() {
    }

    public Item(int iId, String iName) {
        this.iId = iId;
        this.iName = iName;
    }

    public int getId() {
        return iId;
    }

    public String getName() {
        return iName;
    }

    public void setId(int iId) {
        this.iId = iId;
    }

    public void setName(String iName) {
        this.iName = iName;
    }
}

自定義適配器:ExpandableListViewAdapter.java

public class ExpandableListViewAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private ArrayList<Group> groupList;
    private ArrayList<ArrayList<Item>> itemList;

    public ExpandableListViewAdapter(Context context, ArrayList<Group> gData, ArrayList<ArrayList<Item>> iData) {
        this.mContext = context;
        this.groupList = gData;
        this.itemList = iData;
    }

    //返回Group的個數(shù)
    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    //返回某個Group對應(yīng)的Item的個數(shù)
    @Override
    public int getChildrenCount(int groupPosition) {
        return itemList.get(groupPosition).size();
    }

    //返回某個Group對象
    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    //返回某個Item對象
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return itemList.get(groupPosition).get(childPosition);
    }

    //返回Group的id
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    //返回Item的id
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    //獲取指定組處的組數(shù)據(jù)
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolderGroup holderGroup = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_exlist_group,parent,false);
            holderGroup = new ViewHolderGroup();
            holderGroup.tv_name_group = convertView.findViewById(R.id.tv_group_name);
            convertView.setTag(holderGroup);
        }else {
            holderGroup = (ViewHolderGroup) convertView.getTag();
        }
        holderGroup.tv_name_group.setText(groupList.get(groupPosition).getName());
        return convertView;
    }

    //獲取指定組的數(shù)據(jù)、指定子列表項處的子列表項數(shù)據(jù)
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolderItem holderItem = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_exlist_item,parent,false);
            holderItem = new ViewHolderItem();
            holderItem.iv_img_icon = convertView.findViewById(R.id.img_icon);
            holderItem.tv_name_item = convertView.findViewById(R.id.tv_item_name);
            convertView.setTag(holderItem);
        }else {
            holderItem = (ViewHolderItem) convertView.getTag();
        }
        holderItem.iv_img_icon.setImageResource(itemList.get(groupPosition).get(childPosition).getId());
        holderItem.tv_name_item.setText(itemList.get(groupPosition).get(childPosition).getName());
        return convertView;
    }

    //設(shè)置子列表是否可選中
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class ViewHolderGroup {
        TextView tv_name_group;
    }

    class ViewHolderItem {
        ImageView iv_img_icon;
        TextView tv_name_item;
    }
}

主布局:ExpandableListViewActivity.java

public class ExpandableListViewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expandable_list_view);
        //View
        ExpandableListView exListView = findViewById(R.id.expand_list_person);
        //Model
        ArrayList<Group> groupList = new ArrayList<>();
        ArrayList<ArrayList<Item>> itemList = new ArrayList<>();
        groupList.add(new Group("朋友"));
        groupList.add(new Group("同事"));
        groupList.add(new Group("陌生人"));
        //用來存放單個item,下面用三個不同的item集合存放對應(yīng)分類的item
        ArrayList<Item> items = new ArrayList<>();
        //朋友
        items.add(new Item(R.mipmap.img2, "小莉"));
        items.add(new Item(R.mipmap.img7, "小紅"));
        items.add(new Item(R.mipmap.img8, "小美"));
        itemList.add(items);
        //同事
        items = new ArrayList<>();
        items.add(new Item(R.mipmap.img17, "小倩"));
        items.add(new Item(R.mipmap.img12, "小雯"));
        items.add(new Item(R.mipmap.img13, "小芳"));
        itemList.add(items);
        //陌生人
        items = new ArrayList<>();
        items.add(new Item(R.mipmap.img14, "小涵"));
        items.add(new Item(R.mipmap.img15, "小蕾"));
        items.add(new Item(R.mipmap.img16, "小雪"));
        itemList.add(items);

        ExpandableListViewAdapter adapter = new ExpandableListViewAdapter(this, groupList, itemList);
        exListView.setAdapter(adapter);

        //為下拉列表中子元素綁定點擊事件
        exListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(getApplicationContext(), "你點擊了" + itemList.get(groupPosition).get(childPosition).getName(), Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }
}

activity_expandable_list_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    tools:context=".MainActivity">

    <ExpandableListView
        android:id="@+id/expand_list_person"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

item_exlist_group.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="horizontal"
    android:padding="5dp">

    <TextView
        android:id="@+id/tv_group_name"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:gravity="center_vertical"
        android:paddingLeft="30dp"
        android:text="AP"
        android:textStyle="bold"
        android:textSize="20sp" />

</LinearLayout>

item_exlist_item.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="horizontal"
    android:padding="5dp">

    <ImageView
        android:id="@+id/img_icon"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:focusable="false"
        android:src="@mipmap/img2" />

    <TextView
        android:id="@+id/tv_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        android:focusable="false"
        android:text="小莉"
        android:textSize="18sp" />

</LinearLayout>

感謝各位的閱讀,以上就是“怎么使用ExpandListView實現(xiàn)下拉列表”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對怎么使用ExpandListView實現(xiàn)下拉列表這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI