我的 Activity 中有一个ListView
,但现在我想将其更改为ExpandableListView
. 我是否需要完全更改我的列表视图,或者我可以将 expandableListView 添加到我现有的列表视图中吗?
问问题
1715 次
3 回答
1
这是我使用的示例..您可以更改并使用它。
// 列表视图代码
public class FevList extends ExpandableListActivity {
String title;
String url;
SQLiteDatabase database;
DbUtil db;
HashMap<String, String> map = new HashMap<String, String>();
ArrayList<HashMap<String, String>> subjectList = new ArrayList<HashMap<String, String>>();
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.exlist);
db = new DbUtil();
database = db.openConnection(this);
// Thread for getting values of title from DataBase.
SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(
this, createGroupList(), R.layout.group_row,
new String[] { "subject" }, new int[] { R.id.row_name },
createChildList(), R.layout.child_row,
new String[] { "title" }, new int[] { R.id.grp_child });
setListAdapter(expListAdapter);
registerForContextMenu(getExpandableListView());
} catch (Exception e) {
e.printStackTrace();
}
}
// code for adapter
/* Creating the Hashmap for the row */
ArrayList<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();
@SuppressWarnings("unchecked")
private List createGroupList() {
// write your code here.
return (List) result;
}
/* creatin the HashMap for the children */
@SuppressWarnings("unchecked")
private List createChildList() {
// write your code here.
return result;
}
public void onContentChanged() {
System.out.println("onContentChanged");
super.onContentChanged();
}
/* This function is called on each child click */
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// write your code here.
return true;
}
/* This function is called on expansion of the group */
public void onGroupExpand(int groupPosition) {
try {
System.out.println("Group exapanding Listener => groupPosition = "
+ groupPosition);
} catch (Exception e) {
System.out.println(" groupPosition Errrr +++ " + e.getMessage());
}
}
}
于 2013-08-29T13:39:08.237 回答
0
尝试这样的事情:
public class PharmaciesListScreen extends ExpandableListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ExpandableListView expandbleLis = getExpandableListView();
List<RegionItem> regionsList = new RegionsDBUtils(getApplication())
.getAllRegions();
PharmaciesListAdapter mNewAdapter = new PharmaciesListAdapter(regionsList);
mNewAdapter
.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
getExpandableListView().setAdapter(mNewAdapter);
}
适配器代码:
public class PharmaciesListAdapter extends BaseExpandableListAdapter {
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
//set up here you child elements.
//you can use inflater here
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = minflater.inflate(R.layout.grouprow, null);
}
((CheckedTextView) convertView).setText(mRegionsList.get(groupPosition).region);
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
public void setInflater(LayoutInflater mInflater, Activity act) {
this.minflater = mInflater;
activity = act;
}
}
注意:在适配器中,您需要实现更多方法。另外,看看这篇文章: Android ExpandableListView - 寻找教程
于 2013-08-29T12:46:05.390 回答
0
“这是 ExpandableListView 的一个很好的例子”
ExpandableListView 继承自 ListView。它向 ListView 实现添加了更多内容,例如扩展和折叠属性、标题和子视图等。从 listView 移动到可扩展列表视图时,您需要在以下方面进行更改:
- XML 视图
- 实现 ExpandableListAdapter 的适配器
- 在设置适配器时,您需要新的 ExpandableListAdapter(这个,每个列表的 DataHeader 列表,要在每个标题下填充的 DataChild 列表);
于 2017-02-15T04:25:53.297 回答