现在的位置: 首页 > 综合 > 正文

Android:如何在ListView中嵌套ListView (如何实现二级目录结构?)(2)

2013年12月04日 ⁄ 综合 ⁄ 共 2968字 ⁄ 字号 评论关闭

如何实现二级目录的折叠显示,以前一直使用ListView嵌套ListView显示,代码比较繁琐,而且布局也比较繁琐。后来发现了ExpandableListView 这个控件能够很好地完成二级目录的折叠显示。

先看一下效果图,然后再反过来看如何实现。

只能看见一级目录时图片如下:


可以看见二级目录的图如下:


实现过程如下:

在需要显示二级目录的Activity的layout文件中定义 ExpandableListView控件下所示:


<ExpandableListView
            android:id="@id/history_expandview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:groupIndicator="@null"
            android:indicatorRight="0px"/>


红色字体部分代表一级目录左边没有图标。


继承BaseExpandableListAdapter类,重写相应的方法,如下面的代码

public class HistorySiteAdapter extends BaseExpandableListAdapter {

private Context context;
private ArrayList<String> group;//group 为一级目录字符串的值
private ArrayList<ArrayList<URLHistoryItem>> children;//URLHistoryItem为Model类,children为二级目录的对象集合
private LayoutInflater mInflater;
private URLHistoryManager historyManager;

public
HistorySiteAdapter
(Context context) {
super();
this.context = context;
this.mInflater = LayoutInflater.from(this.context);
historyManager = new URLHistoryManager(this.context);
initData();
}

private void initData() {
children = new ArrayList<ArrayList<URLHistoryItem>>();

group = historyManager.getHistorySites();
for (String str:group) {
children.add(historyManager.getHistoryBySite(str));
}



}

@Override
public Object getChild(int arg0, int arg1) {
return children.get(arg0).get(arg1);
}

@Override
public long getChildId(int arg0, int arg1) {
return arg1;
}

@Override
public int getChildrenCount(int arg0) {
return children.get(arg0).size();
}

@Override
public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
ViewGroup arg4) {

             
 //此方法是二级目录的现实

View view = arg3;
if (view == null) {
view = mInflater.inflate(R.layout.history_date_item, null);

}
TextView times = (TextView) view.findViewById(R.id.times);
times.setText(children.get(arg0).get(arg1).visit_site);
TextView detail = (TextView) view.findViewById(R.id.detail);
detail.setText(children.get(arg0).get(arg1).visit_detail_url);
return view;
}

@Override
public Object getGroup(int arg0) {
return group.get(arg0);
}

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

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

@Override
public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) {
View view = arg2;

               //此方法为一级目录的现实

if (view == null) {
view = mInflater.inflate(R.layout.history_expand, null);
}
ImageView imageView = (ImageView) view.findViewById(R.id.tubiao);

             
 //根据它是否扩展,来显示不同的图片

if (arg1) {
imageView.setBackgroundResource(R.drawable.ic_expand_down);
} else {
imageView.setBackgroundResource(R.drawable.ic_expand_right);
}

TextView title = (TextView) view.findViewById(R.id.content_001);
title.setText(getGroup(arg0).toString());
return view;
}

public void onGroupExpanded(int groupPosition) {
}

public void onGroupCollapsed(int groupPosition) {
}

@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}

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

}




得到ExpandableListView,然后设置Adapter。

private ExpandableListView expandHistory;

expandHistory = (ExpandableListView) findViewById(R.id.history_expandview);

expandHistory.setAdapter(newHistorySiteAdapter(HistoryManageActivity.this));




希望对您有所帮助,如果你有问题请您联系我。





抱歉!评论已关闭.