ExpandableListView的应用

使用ExpandableListView实现下拉菜单栏

xml布局


1.activity_main.xml

<ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/id_expandableListView"

    android:indicatorLeft="10dp"
    android:indicatorRight="40dp"
    >

ExpandableListView>

2.item_child_chapter.xml


<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/id_tv_child"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:gravity="center_vertical"
    android:textSize="16sp">TextView>

3.item_parent_chapter.xml



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="#44337dd7"
    android:layout_height="56dp"
    android:orientation="horizontal">
     //这里自己可以找个图片代替
    <ImageView
        android:id="@+id/id_indicator_group"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_gravity="center_vertical"
        android:background="@drawable/indicator_group"
        />


    <TextView
        android:id="@+id/id_tv_parent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        tools:text="Android"
        android:textSize="24dp"
        android:textStyle="bold">

    TextView>

LinearLayout>

工具类


1.Chapter

public class Chapter {
     
    //分组条目
    private int id;
    private String name;
    private List<ChapterItem> chapterItems = new ArrayList<>();
    public void addChild(ChapterItem chapterItem){
     
        chapterItem.setPid(getId());
        chapterItems.add(chapterItem);
    }
    public void addChild(int id,String cname){
     
        ChapterItem chapterItem = new ChapterItem(id,cname);
        chapterItem.setPid(getId());
        chapterItems.add(chapterItem);
    }
    public Chapter() {
     

    }

    public Chapter(int id, String name) {
     
        this.id = id;
        this.name = name;
    }

    public int getId() {
     
        return id;
    }

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

    public String getName() {
     
        return name;
    }

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

    public List<ChapterItem> getChapterItems() {
     
        return chapterItems;
    }

    public void setChapterItems(List<ChapterItem> chapterItems) {
     
        this.chapterItems = chapterItems;
    }

    @Override
    public String toString() {
     
        return "Chapter{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", chapterItems=" + chapterItems +
                '}';
    }
}

2.ChapterItem

public class ChapterItem {
     
    private int id;
    private String name;
    private int pid;

    public ChapterItem() {
     
    }

    public ChapterItem(int id, String name) {
     
        this.id = id;
        this.name = name;
    }

    public int getId() {
     
        return id;
    }

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

    public String getName() {
     
        return name;
    }

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

    public int getPid() {
     
        return pid;
    }

    public void setPid(int pid) {
     
        this.pid = pid;
    }

    @Override
    public String toString() {
     
        return "ChapterItem{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pid=" + pid +
                '}';
    }
}

3.ChapteeLab

//这里是自己传入的数据,可以自己编写,这里做一个大概的
public class ChapteeLab {
     
//模拟数据[可以按照这个样子来继续写]
    public static List<Chapter> generateDatas() {
     
        List<Chapter> chapters = new ArrayList<>();
        Chapter root1 = new Chapter(1, "Android");
        //写值
        root1.addChild(1, "PullToRefresh");
        root1.addChild(2, "Android 8.0通知栏解决方案");
        root1.addChild(4, "Android 与WebView的js交互");
        root1.addChild(8, "Android UiAutomator 2.0 入门实战");
        root1.addChild(10, "移动端音频视频入门");
        //添加
        chapters.add(root1);
        
        return chapters;
 }
}

ChapteAdapter封装类

public class ChapteAdapter extends BaseExpandableListAdapter {
     
    private Context mContext;
    private List<Chapter> mDatas;
    private LayoutInflater mInflater;

    public ChapteAdapter(Context context, List<Chapter> chapters) {
     
        mContext = context;
        mDatas = chapters;
        mInflater = LayoutInflater.from(context);
    }


    @Override
    public int getGroupCount() {
     
        return mDatas.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
     
        return mDatas.get(groupPosition).getChapterItems().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
     
        return mDatas.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
     
        return mDatas.get(groupPosition).getChapterItems().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
     
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
     
        return childPosition;
    }


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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
     


        ParentViewHolder vh;
        if (convertView == null) {
     
            // 修改item height即可演示,第二个参数作用
            convertView = mInflater.inflate(R.layout.item_parent_chapter, parent, false);
            vh = new ParentViewHolder();
            vh.tv = convertView.findViewById(R.id.id_tv_parent);
            vh.iv = convertView.findViewById(R.id.id_indicator_group);
            convertView.setTag(vh);

        } else {
     
            vh = (ParentViewHolder) convertView.getTag();
        }
        vh.tv.setText(mDatas.get(groupPosition).getName());
        vh.iv.setSelected(isExpanded);

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
     


        ChildViewHolder vh;
        if (convertView == null) {
     
            convertView = mInflater.inflate(R.layout.item_child_chapter, parent, false);
            vh = new ChildViewHolder();
            vh.tv = convertView.findViewById(R.id.id_tv_child);

            convertView.setTag(vh);

        } else {
     
            vh = (ChildViewHolder) convertView.getTag();
        }
        vh.tv.setText(mDatas.get(groupPosition).getChapterItems().get(childPosition).getName());
        return convertView;
    }

    // 控制child item不可点击
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
     

        return true;
    }

    public static class ParentViewHolder {
     
        TextView tv;
        ImageView iv;
    }

    public static class ChildViewHolder {
     
        TextView tv;
    }
}

主类


1.MainActivity

public class MainActivity extends AppCompatActivity {
     
    private ExpandableListView mexpandableListView;
    private BaseExpandableListAdapter madapter;
    private List<Chapter> mDatas = new ArrayList<>();
    private static final String TAG = "xw";
    private ChapteBiz mchapteBiz = new ChapteBiz();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
     
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvents();
    }
    private void initEvents() {
     
        mexpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
     
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
     
                Log.d(TAG,"1"+i+"2"+i1);
                return false;
            }
        });
        mexpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
     
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
     
                Log.d(TAG,"1"+i);
                return false;
            }
        });
        mexpandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
     
            @Override
            public void onGroupCollapse(int i) {
     
                Log.d(TAG,"1"+i);
            }
        });
        mexpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
     
            @Override
            public void onGroupExpand(int i) {
     
                Log.d(TAG,"1"+i);
            }
        });
    }

    private void initView() {
     
        mexpandableListView = findViewById(R.id.id_expandableListView);
        mDatas.clear();
        mDatas.addAll(ChapteeLab.generateDatas());
        //设置适配器[封装BaseExpandableListAdapter(){......}]
        madapter = new ChapteAdapter(this,mDatas);
        mexpandableListView.setAdapter(madapter);
        //隐藏'>'
        mexpandableListView.setGroupIndicator(null);
    }
}

成果


ExpandableListView的应用_第1张图片
ExpandableListView的应用_第2张图片
谢谢看到这里,Android萌新,有问题望大佬指出,谢谢!

你可能感兴趣的:(android,java,android,studio)