您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么使用ExpandListView實現下拉列表”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“怎么使用ExpandListView實現下拉列表”吧!
使用方式與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的個數 @Override public int getGroupCount() { return groupList.size(); } //返回某個Group對應的Item的個數 @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; } //獲取指定組處的組數據 @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; } //獲取指定組的數據、指定子列表項處的子列表項數據 @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; } //設置子列表是否可選中 @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集合存放對應分類的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實現下拉列表”的內容了,經過本文的學習后,相信大家對怎么使用ExpandListView實現下拉列表這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。