如何突出显示ExpandableListView选项?

时间:2014-08-11 13:24:33

标签: android android-listview expandablelistview onclicklistener

当我选择childPosition时,它会突出显示并保持不变。但是,当我单击其组以折叠并再次展开时,突出显示消失。

代码:

    myList.setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE);    
        ....
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            int index = parent.getFlatListPosition(ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));
            parent.setItemChecked(index, true);
            return false;
          }
       };

MyListAdapter.java:

public class MyListAdapter extends BaseExpandableListAdapter {

 private Context context;
 private ArrayList<HeaderInfo> deptList;

 public MyListAdapter(Context context, ArrayList<HeaderInfo> deptList) {
  this.context = context;
  this.deptList = deptList;
 }

 @Override
 public Object getChild(int groupPosition, int childPosition) {
  ArrayList<DetailInfo> productList = deptList.get(groupPosition).getProductList();
  return productList.get(childPosition);
 }

 @Override
 public long getChildId(int groupPosition, int childPosition) {
  return childPosition;
 }

 @Override
 public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
   View view, ViewGroup parent) {
  DetailInfo detailInfo = (DetailInfo) getChild(groupPosition, childPosition);
  if (view == null) {
   LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   view = infalInflater.inflate(R.layout.child_row, null);
  }

  TextView sequence = (TextView) view.findViewById(R.id.sequence);
  sequence.setText(detailInfo.getSequence().trim() + ") ");
  TextView childItem = (TextView) view.findViewById(R.id.childItem);
  childItem.setText(detailInfo.getName().trim());

  return view;
 }

 @Override
 public int getChildrenCount(int groupPosition) {
  ArrayList<DetailInfo> productList = deptList.get(groupPosition).getProductList();
  return productList.size();
 }

 @Override
 public Object getGroup(int groupPosition) {
  return deptList.get(groupPosition);
 }

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

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

 @Override
 public View getGroupView(int groupPosition, boolean isLastChild, View view,
   ViewGroup parent) {

  HeaderInfo headerInfo = (HeaderInfo) getGroup(groupPosition);
  if (view == null) {
   LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   view = inf.inflate(R.layout.group_heading, null);
  }

  TextView heading = (TextView) view.findViewById(R.id.heading);
  heading.setText(headerInfo.getName().trim());

  return view;
 }

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

 @Override
 public boolean isChildSelectable(int groupPosition, int childPosition) {
  return true;
 }  
}

row_highlighter_child.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/holo_blue_light" android:state_activated="true"/>
    <item android:drawable="@android:color/holo_green_light" android:state_pressed="true"/>
    <item android:drawable="@android:color/holo_orange_light"/>
</selector>

child.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/row_highlighter_child"    
    android:orientation="vertical" >

    <TextView
        android:id="@+id/sequence"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:paddingLeft="35sp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/childItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/sequence"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

0 个答案:

没有答案