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

Android ExpandableListView使用方法案例詳解

 更新時間:2021年08月23日 09:18:36   作者:會飛的魚兒android  
這篇文章主要介紹了Android ExpandableListView使用方法案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

一、前言

  “好記性不如爛筆頭”,再次驗(yàn)證了這句話是真的很有道理啊,一個月前看了一下ExpandableListView的使用,今天再看居然忘了這個是干啥的了,今天就詳細(xì)講解一下ExpandableListView的使用方法,感覺對于二級條目顯示功能都可以實(shí)現(xiàn)。

二、實(shí)現(xiàn)的功能

1、可實(shí)現(xiàn)二級列表?xiàng)l目顯示功能,具體包括可自定義,父布局和子布局??蓪?shí)現(xiàn)父布局全部展開和只展開一個功能。

三、具體代碼

1、主xml代碼

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <include layout="@layout/top"/>
    <ExpandableListView
        android:id="@+id/expand_list_id"
        android:layout_width="match_parent"
        android:groupIndicator="@null"
        android:layout_height="match_parent">
    </ExpandableListView>
</LinearLayout>

2、父布局xml代碼

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@color/blue"
    android:layout_height="50dp">
    <TextView
        android:id="@+id/parent_textview_id"
        android:layout_width="wrap_content"
        android:text="測試"
        android:textColor="@color/white"
        android:gravity="center_vertical"
        android:paddingLeft="10dp"
        android:textSize="18sp"
        android:layout_height="match_parent" />
 
    <ImageView
        android:id="@+id/parent_image"
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true"
        android:src="@drawable/img_arrow_right"
        android:layout_centerInParent="true"
        android:paddingRight="10dp"
        android:layout_height="wrap_content" />
 
</RelativeLayout>

3、子布局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">
    <TextView
        android:id="@+id/chidren_item"
        android:layout_width="match_parent"
        android:text="子項(xiàng)目"
        android:gravity="center_vertical"
        android:paddingLeft="10dp"
        android:textColor="@color/black"
        android:layout_height="40dp" />
 
</LinearLayout>

4、主activity代碼

**
 * Created by fyf on 2019/3/1.
 * 描述:用于實(shí)現(xiàn)ExpandableListView的類,主要功能是實(shí)現(xiàn)二級列表?xiàng)l目顯示
 */
public class ExpandableListviewActivity extends BaseActivity {
    private ExpandableListView expand_list_id;
    //Model:定義的數(shù)據(jù)
    private String[] groups = {"開發(fā)部", "人力資源部", "銷售部"};
 
    //注意,字符數(shù)組不要寫成{{"A1,A2,A3,A4"}, {"B1,B2,B3,B4,B5"}, {"C1,C2,C3,C4"}}
    private String[][] childs = {{"趙珊珊", "錢丹丹", "孫可可", "李冬冬"}, {"周大福", "吳端口", "鄭非", "王瘋狂"}, {"馮程程", "陳類", "楚哦", "魏王"}};
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expandable_list_view);
        setTitle("ExpandableListView二級下拉",false,"");
        initView();
    }
    private void initView(){
        expand_list_id=findViewById(R.id.expand_list_id);
        ExpandableListviewAdapter adapter=new ExpandableListviewAdapter(this,groups,childs);
        expand_list_id.setAdapter(adapter);
        //默認(rèn)展開第一個數(shù)組
        expand_list_id.expandGroup(0);
        //關(guān)閉數(shù)組某個數(shù)組,可以通過該屬性來實(shí)現(xiàn)全部展開和只展開一個列表功能
        //expand_list_id.collapseGroup(0);
        expand_list_id.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int groupPosition, long l) {
                showToastShort(groups[groupPosition]);
                return false;
            }
        });
        //子視圖的點(diǎn)擊事件
        expand_list_id.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long l) {
                showToastShort(childs[groupPosition][childPosition]);
                return true;
            }
        });
        //用于當(dāng)組項(xiàng)折疊時的通知。
        expand_list_id.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
                showToastShort("折疊了數(shù)據(jù)___"+groups[groupPosition]);
            }
        });
        //
        //用于當(dāng)組項(xiàng)折疊時的通知。
        expand_list_id.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                showToastShort("展開了數(shù)據(jù)___"+groups[groupPosition]);
            }
        });
    }
}

5、adapter代碼

/**
 * Created by fyf on 2019/3/1.
 * 描述:是二級顯示列表的adapter
 */
public class ExpandableListviewAdapter extends BaseExpandableListAdapter {
    //Model:定義的數(shù)據(jù)
    private String[] groups;
    //注意,字符數(shù)組不要寫成{{"A1,A2,A3,A4"}, {"B1,B2,B3,B4,B5"}, {"C1,C2,C3,C4"}}
    private String[][] childs;
    private Context context;
 
    public ExpandableListviewAdapter(Context context,String[] groups,String[][] childs){
        this.context=context;
        this.groups=groups;
        this.childs=childs;
    }
 
    @Override
    public int getGroupCount() {
        return groups.length;
    }
 
    @Override
    public int getChildrenCount(int i) {
        return childs[i].length;
    }
 
    @Override
    public Object getGroup(int i) {
        return groups[i];
    }
 
    @Override
    public Object getChild(int i, int i1) {
        return childs[i][i1];
    }
 
    @Override
    public long getGroupId(int i) {
        return i;
    }
 
    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }
 
    @Override
    //分組和子選項(xiàng)是否持有穩(wěn)定的ID, 就是說底層數(shù)據(jù)的改變會不會影響到它們
    public boolean hasStableIds() {
        return true;
    }
 
    @Override
/**
 *
 * 獲取顯示指定組的視圖對象
 *
 * @param groupPosition 組位置
 * @param isExpanded 該組是展開狀態(tài)還是伸縮狀態(tài),true=展開
 * @param convertView 重用已有的視圖對象
 * @param parent 返回的視圖對象始終依附于的視圖組
 */
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.expand_parent_item,parent,false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.parent_textview_id = convertView.findViewById(R.id.parent_textview_id);
            groupViewHolder.parent_image = convertView.findViewById(R.id.parent_image);
            convertView.setTag(groupViewHolder);
        }else {
            groupViewHolder = (GroupViewHolder)convertView.getTag();
        }
        groupViewHolder.parent_textview_id.setText(groups[groupPosition]);
        //如果是展開狀態(tài),
        if (isExpanded){
            groupViewHolder.parent_image.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.img_arrow_down));
        }else{
            groupViewHolder.parent_image.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.img_arrow_right));
        }
        return convertView;
    }
 
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView==null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.expand_chidren_item,parent,false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.chidren_item = (TextView)convertView.findViewById(R.id.chidren_item);
            convertView.setTag(childViewHolder);
 
        }else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        childViewHolder.chidren_item.setText(childs[groupPosition][childPosition]);
        return convertView;
    }
 
    //指定位置上的子元素是否可選中
    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
    static class GroupViewHolder {
        TextView parent_textview_id;
        ImageView parent_image;
    }
 
    static class ChildViewHolder {
        TextView chidren_item;
 
    }
}

到此這篇關(guān)于Android ExpandableListView使用方法案例詳解的文章就介紹到這了,更多相關(guān)Android ExpandableListView使用方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論