在展开式列表中突出显示所选项目 [英] Highlight for selected item in expandable list

查看:69
本文介绍了在展开式列表中突出显示所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布局,左边有一个可扩展列表,右边有一个详细信息片段.这一切都很好.

I have a layout where I have an expandable list in a fragment on the left and a details fragment on the right. This all works fine.

现在,我想指出左边的哪个项目的详细信息显示在右边,这里我遇到了问题.

Now I would like to indicate what item on the left is having it's details shown on the right and here I'm having a problem.

在普通列表视图中,我已通过将列表视图的选择模式设置为单个,然后使用基于已激活"状态的可绘制状态来实现此目的.当我单击该项目时,背景会设置为我选择的颜色,并一直保持这种状态,直到我在列表中选择另一个项目为止.

In a normal list view I have achieved this by setting the choice mode for the list view to single and then using a state drawable based on the "activated" state. When I click the item, the background is set to my selected color and stays that way until I select another item on the list.

我试图将其应用于我的可扩展列表视图,但这是/是一次惨痛的失败.没有错误,但所选项目未保持其颜色状态.我不确定是否没有正确设置选择模式(我已经在布局文件中以及通过编程方式对其进行了尝试,但似乎没有什么不同),或者将其指向错误的地方(不知道怎么可能,但是...)

I tried to apply this to my expandable listview and it was/is a dismal failure. There were no errors, but the selected item did not maintain it's color state. I'm not sure if I'm not setting the choice mode properly for it (I've tried it in the layout file as well as programatically, it doesn't seem to make a difference), or pointing it to the wrong thing (not sure how that could be, but...)

任何帮助/指针都表示赞赏(即使方向完全不同).

Any help/pointers appreciated (even if it's in a completely different direction).

大多数当前故障:

展开式列表视图代码

private void fillData(String group, String child) {
    ExpandableListView lv;
    mGroupsCursor = mDbHelper.fetchGroup(group);
    getActivity().startManagingCursor(mGroupsCursor);
    mGroupsCursor.moveToFirst();
    lv = (ExpandableListView) getActivity().findViewById(R.id.explist);
    lv.setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE);        

    mAdapter = new MyExpandableListAdapter(mGroupsCursor, getActivity(),
            R.layout.explistlayout,
            R.layout.explistlayout,
            new String[] { "_id" },
            new int[] { android.R.id.text1 },
            new String[] { child },
            new int[] { android.R.id.text1 });
    lv.setAdapter(mAdapter);
    registerForContextMenu(lv);

    lv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
        @Override 
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)  
        { 
            mRowId  = id;
            EventDisplayFragment eventdisplay = new EventDisplayFragment();
            getFragmentManager().beginTransaction().replace(R.id.rightpane, eventdisplay).commit();
            return true; 
        } 
        }); 
}

public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {

    public MyExpandableListAdapter(Cursor cursor, Context context,
            int groupLayout, int childLayout, String[] groupFrom,
            int[] groupTo, String[] childrenFrom, int[] childrenTo) {
        super(context, cursor, groupLayout, groupFrom, groupTo,
                childLayout, childrenFrom, childrenTo);
    }
    @Override
    protected Cursor getChildrenCursor(Cursor groupCursor) {
        Cursor childCursor = mDbHelper.fetchChildren(mGroup, groupCursor
                .getString(groupCursor
                        .getColumnIndex(AttendanceDB.EVENT_ROWID)));
        getActivity().startManagingCursor(childCursor);
        childCursor.moveToFirst();
        return childCursor;
    }
}

item_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
     android:state_pressed="true" 
     android:drawable="@color/green" />
    <item 
     android:state_selected="true" 
     android:drawable="@color/blue" />
    <item 
     android:state_focused="true" 
     android:drawable="@color/violet" />
    <item 
     android:state_activated="true" 
     android:drawable="@color/blue" />
</selector>

推荐答案

在ExpandableListView上执行以下操作:

Do the following on ExpandableListView:

第1步.将选择模式设置为单一(可以在xml中作为 android:choiceMode ="singleChoice" 来完成)

Step 1. Set choice mode to single (Can be done in xml as android:choiceMode = "singleChoice")

第2步.使用选择器xml作为背景( android:listSelector ="@ drawable/selector_list_item" )

Step 2. Use a selector xml as background (android:listSelector = "@drawable/selector_list_item")

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
   android:exitFadeDuration="@android:integer/config_mediumAnimTime">

   <item android:drawable="@android:color/holo_blue_bright" android:state_pressed="true"/>
   <item android:drawable="@android:color/holo_blue_light" android:state_selected="true"/>
   <item android:drawable="@android:color/holo_blue_dark" android:state_activated="true"/>

</selector>

第3步.在onChildClick()回调中调用 expandableListView.setItemChecked(index,true).

Step 3. Call expandableListView.setItemChecked(index,true) in onChildClick() callback.

索引是子项的基于0的索引,计算方式如下

index is a 0 based index of the child item calculated as follows

组1 [索引= 0]

  • 子项1
  • 子项2
  • 子项3 [索引= 3]

第2组 [索引= 4]

  • 子项1
  • 子项2
  • 子项3 [索引= 7]

第3组 [索引= 8]

  • 子项1
  • 子项2
  • 子项3 [索引= 11]

如果我们也有列表头,那么它们也会考虑索引值.

If we have list headers as well, they would also account for index values.

这是一个有效的示例:

@Override
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 true;
}

这篇关于在展开式列表中突出显示所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆