android USES the ExpandableListView control to implement an example of a novel directory effect
- 2020-06-01 10:59:21
- OfStack
Today I'm going to talk to you about the directory implementation of android, just like you can see the novel directory 1,android provides the ExpandableListView control to achieve the level 2 list display effect, now I'm going to talk to you about the use of this control, the following is the definition of XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FFFFFF"
>
<ExpandableListView
android:id="@+id/elv_journal_catalog"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:cacheColorHint="#FFFFFF"
/>
</LinearLayout>
This code is very simple, similar to the way listView is written. Next is the code of ExpandableListView in activity:
private ExpandableListView elv_journal_catalog;
private List<List<Article>> childrenObj;
private JournalCatalogListAdapter adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.journal_catalog);
init();
elv_journal_catalog.setGroupIndicator(null);
elv_journal_catalog.setDivider(null);
loadData();
}
private void init() {
elv_journal_catalog = (ExpandableListView) findViewById(R.id.elv_journal_catalog);
elv_journal_catalog.setOnChildClickListener(listener);
}
private void loadData() {
Message msg = handler.obtainMessage();
msg.what = 1;
msg.sendToTarget();
childrenObj = new ArrayList<List<Article>>();
new Thread() {
@Override
public void run() {
if (!isLoading) {
queryArticleList();
} else {
queryArticleListFromSqlite();
}
}
}.start();
adapter = new JournalCatalogListAdapter(this, childrenObj);
elv_journal_catalog.setAdapter(adapter);
}
ExpandableListView display data by default when the list items under each module is closed, if you want to realize the initialization time. Start by ExpandableListView expandGroup (location) method to implement, and each parent list item on the left will be the icon in the system at one, this icon is used to represent a list of open and closed state, if don't show or to replace the icon can be used
ExpandableListView.setGroupIndicator (Drawable icon) method to implement, I'm not using any ICONS here, you can also define your own ICONS in adapter in xml.
The data structure can be determined according to the project situation. As the title is fixed, I only pass the data under each title. The code of JournalCatalogListAdapter is as follows:
public class JournalCatalogListAdapter extends BaseExpandableListAdapter {
private LayoutInflater inflater;
private String[] parent = new String[] { " Beauty beauty ", " Tide sheet is tasted ", " entertainment 8 divination ", " emotional ",
" view ", " A healthy life " };
private List<List<Article>> clildren = new ArrayList<List<Article>>();
public JournalCatalogListAdapter(Context context,
List<List<Article>> clildren) {
this.clildren = clildren;
inflater = LayoutInflater.from(context);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return clildren.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(
R.layout.journal_catalog_list_item_content, null);
}
TextView textView = (TextView) convertView
.findViewById(R.id.tv_journal_catalog_list_item_content);
Article a = (Article) getChild(groupPosition, childPosition);
textView.setText(a.getTitle());
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return clildren.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return parent[groupPosition];
}
@Override
public int getGroupCount() {
return parent.length;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(
R.layout.journal_catalog_list_item_title, null);
}
TextView textView = (TextView) convertView
.findViewById(R.id.tv_journal_catalog_list_item_title);
String title = String.valueOf(getGroup(groupPosition));
textView.setText(title);
convertView.setOnClickListener(null);
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}