使用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
<?xml version="1.0" encoding="utf-8"?>
<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
<?xml version="1.0" encoding="utf-8"?>
<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);
}
}
成果
謝謝看到這里,Android萌新,有問題望大佬指出,謝謝!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/277063.html
標籤:其他
