欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ExpandListView實現(xiàn)下拉列表案例

 更新時間:2022年08月26日 08:32:29   作者:磚廠打工仔  
這篇文章主要為大家詳細(xì)介紹了ExpandListView實現(xiàn)下拉列表案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

ExpandListView實現(xiàn)下拉列表案例,供大家參考,具體內(nèi)容如下

使用方式與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>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論