好的,所以我一直在寻找广泛,我已经能够为每个组添加相同的子项,但我无法让它响应点击。现在它的表现就像一个子标题,我需要它像其他子项一样。
目前,我的列表是从位于另一个文件中的HashMap填充的。因为你不能在没有完全重建它的情况下添加到HashMap的中间,我认为最好的选择是在适配器中访问列表时将子项添加到某处。我只是不确定在哪里或如何。
从getChildView()可以看出,如果child的位置为0,我会添加重复的子布局(expandedListItemChildHeader)。我还为getChildrenCount()的大小添加了1个值。
这是我的适配器代码:
<android-sdk>
以下是我的MainActivity中的可扩展列表代码,它位于onCreate()方法中:(注意:下面示例代码顶部列出的变量在onCreate()方法之外,我只是将其包括在内参考
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
HashMap<String, List<String>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
@Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
@Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
@Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//if we're in the first position then add the list_item_header
if(expandedListPosition == 0)
{
convertView = layoutInflater.inflate(R.layout.list_item_header, null);
TextView expandedListTextView = (TextView) convertView.findViewById(R.id.expandedListItemChildHeader);
expandedListTextView.setText("All Devices");
}
//otherwise add the list_item
if (expandedListPosition > 0 && expandedListPosition < getChildrenCount(listPosition)) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition - 1);
convertView = layoutInflater.inflate(R.layout.list_item, null);
TextView expandedListTextView = (TextView) convertView.findViewById(R.id.expandedListItem);
expandedListTextView.setText(expandedListText);
}
return convertView;
}
@Override
public int getChildrenCount(int listPosition) {
//add a value of one to the size of the count of children per group to make
//room for the child header
return (this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size() + 1);
}
@Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
@Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
@Override
public long getGroupId(int listPosition) {
return listPosition;
}
@Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_group, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.listTitle);
listTitleTextView.setTypeface(Typeface.create("sans-serif-medium", Typeface.NORMAL));
listTitleTextView.setText(listTitle);
//Use this conditional to change direction of group indicator icons
//the ImageView in the xml layout is invisible by default.
ImageView groupIndicatorView = (ImageView) convertView.findViewById(R.id.expand_GroupIndicator);
if (getChildrenCount(listPosition) == 0 ) {
groupIndicatorView.setVisibility( View.INVISIBLE );
}
else {
groupIndicatorView.setVisibility( View.VISIBLE );
groupIndicatorView.setImageResource( isExpanded ? R.drawable.ic_expand_less_black_24dp : R.drawable.ic_expand_more_black_24dp );
}
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
这是我的布局 从list_item_header.xml开始
ExpandableListView expandableListView;
ExpandableListAdapter expandableListAdapter;
List<String> expandableListTitle;
HashMap<String, List<String>> expandableListDetail;
expandableListView = (ExpandableListView) findViewById(R.id.navDrawer_userListView);
expandableListDetail = ExpandableListDataPump.getData();
expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
expandableListAdapter = new CustomExpandableListAdapter(this, expandableListTitle, expandableListDetail);
expandableListView.setAdapter(expandableListAdapter);
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
//initialize int var
int previousGroup = 0;
@Override
public void onGroupExpand(int groupPosition) {
//this conditional enables only one drop down group to open at a time
if(groupPosition != previousGroup)
expandableListView.collapseGroup(previousGroup);
previousGroup = groupPosition;
Toast.makeText(
getApplicationContext(),
expandableListTitle.get(groupPosition)
+ " List Expanded.",
Toast.LENGTH_SHORT
).show();
}
});
expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
expandableListTitle.get(groupPosition) + " List Collapsed.",
Toast.LENGTH_SHORT
).show();
}
});
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
v.setSelected(true);
//Change title on toolbar to match group selection
getSupportActionBar().setTitle(expandableListTitle.get(groupPosition) + "'s Devices");
//Change subtitle on toolbar to match item selection
//must offset child position by 1 to have room for child header
getSupportActionBar().setSubtitle((String)expandableListAdapter.getChild(groupPosition, childPosition - 1));
Toast.makeText(
getApplicationContext(),
expandableListTitle.get(groupPosition)
+ " -> "
+ expandableListDetail.get(
expandableListTitle.get(groupPosition)).get(
childPosition - 1), Toast.LENGTH_SHORT
).show();
return false;
}
});
这是我的list_item.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="7dp">
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@null"
android:id="@+id/toggle_allDevices"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:paddingLeft="2dp"
android:tint="@color/drawerContent"/>
<TextView
android:id="@+id/expandedListItemChildHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@drawable/expandable_text_selector"
android:paddingLeft="65dp"
android:paddingTop="16dp"
android:textSize="16dp"
android:paddingBottom="16dp" />
</RelativeLayout>
</LinearLayout>
list_group.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="7dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@null"
android:id="@+id/deviceTypeIcon"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:paddingLeft="14dp"
android:tint="@color/drawerContent"/>
<TextView
android:id="@+id/expandedListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@drawable/expandable_text_selector"
android:paddingLeft="65dp"
android:paddingTop="16dp"
android:textSize="16dp"
android:paddingBottom="16dp" />
</RelativeLayout>
</LinearLayout>
最后是我的NavigationView的片段 主要活动:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="match_parent"
android:layout_height="0.25dp"
android:layout_alignParentTop="true"
android:background="@color/drawerContent" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="7dp">
<TextView
android:id="@+id/listTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textColor="@color/drawerContent"
android:background="@color/drawerBg"
android:paddingTop="16dp"
android:textSize="16dp"
android:paddingBottom="16dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/expand_GroupIndicator"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:src="@drawable/ic_expand_more_black_24dp"
android:paddingRight="15dp"
android:tint="@color/drawerContent"
android:visibility="invisible"/>
</RelativeLayout>
</LinearLayout>
任何帮助将不胜感激!谢谢!
答案 0 :(得分:0)
好的,我能解决问题的最后一部分。事实证明,我所要做的就是在我的onChildClick()方法中设置一个条件来调整我使用以下代码访问的索引:
//this conditional corrects for our new child header that is added in getChildView()
String newChildSelect = "";
if (childPosition == 0) {
newChildSelect = "All Devices";
getSupportActionBar().setSubtitle(newChildSelect);
}
else
{
newChildSelect = expandableListDetail.get(expandableListTitle.get(groupPosition)).get(childPosition - 1);
//Change subtitle on toolbar to match item selection
getSupportActionBar().setSubtitle((String)expandableListAdapter.getChild(groupPosition, childPosition - 1));
}