可扩展列表视图默认选择
Posted
技术标签:
【中文标题】可扩展列表视图默认选择【英文标题】:Expandable listview default selection 【发布时间】:2014-05-07 12:08:33 【问题描述】:我创建了一个导航抽屉,里面有一个可扩展的列表视图。 我希望可展开的列表视图在活动打开时默认展开其中一个组并选择其子项。用户可以根据自己的要求更改选择。
无论我创建什么都不能满足我的要求。可扩展列表允许用户选择多个选项。
MainActivity:
public class MainActivity extends Activity
DrawerLayout mDrawerLayout;
ExpandableListView mDrawerList;
ExpandableListAdapter listAdapter;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
private ActionBar mActionBar;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Action bar with collapse icon for drawer menu===================
mActionBar = getActionBar();
mActionBar.setHomeButtonEnabled(true);
mActionBar.setDisplayHomeAsUpEnabled(false);
// Action bar with collapse icon for drawer menu===================
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ExpandableListView) findViewById(R.id.left_drawer);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader,
listDataChild);
// setting list adapter
mDrawerList.setAdapter(listAdapter);
// Listview Group click listener
mDrawerList.setOnGroupClickListener(new OnGroupClickListener()
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id)
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
);
// // Listview Group expanded listener
// mDrawerList.setOnGroupExpandListener(new OnGroupExpandListener()
//
// @Override
// public void onGroupExpand(int groupPosition)
// Toast.makeText(getApplicationContext(),
// listDataHeader.get(groupPosition) + " Expanded",
// Toast.LENGTH_SHORT).show();
//
// );
//
// // Listview Group collasped listener
// mDrawerList.setOnGroupCollapseListener(new OnGroupCollapseListener()
//
//
// @Override
// public void onGroupCollapse(int groupPosition)
// Toast.makeText(getApplicationContext(),
// listDataHeader.get(groupPosition) + " Collapsed",
// Toast.LENGTH_SHORT).show();
//
//
// );
// mDrawerList.expandGroup(2);
// mDrawerList.expandGroup(2, true);
// Listview on child click listener
mDrawerList.setOnChildClickListener(new OnChildClickListener()
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id)
mDrawerLayout.closeDrawer(mDrawerList);
// TODO Auto-generated method stub
// Toast.makeText(
// getApplicationContext(),
// listDataHeader.get(groupPosition)
// + " : "
// + listDataChild.get(
// listDataHeader.get(groupPosition)).get(
// childPosition), Toast.LENGTH_SHORT)
// .show();
v.setBackgroundColor(Color.BLUE);
makeAToast("Group: " + groupPosition + " Child: "
+ childPosition);
return false;
);
// To collapse previously opened group===========
mDrawerList.setOnGroupExpandListener(new OnGroupExpandListener()
int previousItem = -1;
@Override
public void onGroupExpand(int groupPosition)
if (groupPosition != previousItem)
mDrawerList.collapseGroup(previousItem);
previousItem = groupPosition;
);
// To collapse previously opened group===========
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
switch (item.getItemId())
case android.R.id.home:
if (mDrawerLayout.isDrawerOpen(mDrawerList))
mDrawerLayout.closeDrawer(mDrawerList);
else
mDrawerLayout.openDrawer(mDrawerList);
break;
default:
break;
return super.onOptionsItemSelected(item);
/*
* Preparing the list data
*/
private void prepareListData()
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding header data
listDataHeader.add("Today");
// Adding child data
List<String> today = new ArrayList<String>();
today.add("Tanushree Guha");
today.add("Prasenjit Roy");
today.add("Pan Singh Tomar");
today.add("Milka Singh");
today.add("Rohit Ramanujan");
today.add("Ramesh Bhatt");
today.add("Debjani Brahma");
listDataHeader.add("Tomorrow");
List<String> tomorrow = new ArrayList<String>();
tomorrow.add("Dipanjan Bhowmik");
tomorrow.add("Sarmistha Sinha");
tomorrow.add("Pranay Lalwani");
tomorrow.add("Mohit Shaw");
tomorrow.add("Lovelace Agarwal");
tomorrow.add("Tanmay Banerjee");
listDataHeader.add("Later");
List<String> later = new ArrayList<String>();
later.add("Yusuf Khan");
later.add("Jitendar Sharma");
later.add("Debashree Roy");
later.add("Mainak Ghosh");
later.add("Olivia Gomes");
listDataChild.put(listDataHeader.get(0), today); // Header, Child data
listDataChild.put(listDataHeader.get(1), tomorrow);
listDataChild.put(listDataHeader.get(2), later);
// method to show toast message
public void makeAToast(String str)
Toast toast = Toast.makeText(this, str, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
activity_main.xml 布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".MainActivity" >
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_
android:layout_ >
<FrameLayout
android:id="@+id/content_frame"
android:layout_
android:layout_ >
</FrameLayout>
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view. -->
<ExpandableListView
android:id="@+id/left_drawer"
android:layout_
android:layout_
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#999999"
android:dividerHeight="1dp"
android:background="#ffffff"/>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
ExpandableListAdapter 类:
public class ExpandableListAdapter extends BaseExpandableListAdapter
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData)
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
@Override
public Object getChild(int groupPosition, int childPosititon)
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
@Override
public long getChildId(int groupPosition, int childPosition)
return childPosition;
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null)
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
@Override
public int getChildrenCount(int groupPosition)
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
@Override
public Object getGroup(int groupPosition)
return this._listDataHeader.get(groupPosition);
@Override
public int getGroupCount()
return this._listDataHeader.size();
@Override
public long getGroupId(int groupPosition)
return groupPosition;
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null)
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
@Override
public boolean hasStableIds()
return false;
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
return true;
如果需要更多源代码,我会提供。我应该怎么做才能满足我的要求?
【问题讨论】:
你好,默认选中是什么意思? 默认选中,组展开,子突出显示 【参考方案1】:我会推荐使用适用于 Android 的 SlideExpandableListView:
(那张截图很垃圾,但你可以猜到动画)
这是该库的官方描述:
对 Android 提供的 ExpandableListView 不满意?想要 Spotify 应用程序之类的东西。该库允许您自定义列表视图,其中每个列表项都有一个区域,一旦用户单击某个按钮,该区域就会滑出。
特点
提供更好的可用于普通 ListView 的 ExpandableListView 默认动画 易于使用
在 GitHub 上检查该存储库:https://github.com/tjerkw/Android-SlideExpandableListView/。
【讨论】:
这个项目缺少类 我猜你需要先导入那个 gradle 项目。另请参阅this tutorial。 库仍然出现错误,您能否为我提供 eclipse 的替代方案,或者查看我的代码并告诉我该怎么做【参考方案2】:对于问题的第一部分:
我希望可展开的列表视图展开其中一个组并选择 活动打开时默认为其子之一
mExpandableListView.setSelectedChild(groupPosition, childPosition, shouldExpandGroup);
在哪里:
-
groupPosition 是应该首先展开的组的位置
默认应选择的孩子的位置 *更多信息如下
shouldExpandGroup 是一个布尔值,应设置为 true 以扩展组
以上 2 的信息。
维护一个包含组位置和用户选择的子位置的哈希图,例如:
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(groupPosition, childPosition);
对于每个子选择,只需将其放入哈希图中,如果存在,它将覆盖该值
将此哈希图存储在文件中或可以再次访问的位置(可能是共享首选项,但不允许存储对象)
当activity启动时从hashmap中获取值,并设置选择, 您可以为所有组设置选择,但将所有组的 shouldExpand 设置为 false 除了第一个。如果地图为空,则将默认选择设置为 0。
【讨论】:
mDrawerList.setSelectedChild(1, 2, true);不工作............列表没有扩大。我该怎么办?我是在 oncreate 方法里面写的 稍等,我回来 在 setSelectedChild 之后调用mDrawerList.expandGroup(1);
.. 看起来最后一个参数不起作用。
只会扩展组。但是我应该怎么做才能选择扩展组的孩子。
您可以这样做,在列表视图的适配器类中,检查组和子项的位置,然后选择它,我的意思是您需要有一个可绘制的选择器以显示行被选中以上是关于可扩展列表视图默认选择的主要内容,如果未能解决你的问题,请参考以下文章