ExpandableListView - 选择保持孩子在" pressed"州 [英] ExpandableListView - keep selected child in a "pressed" state

查看:154
本文介绍了ExpandableListView - 选择保持孩子在" pressed"州的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是ExpandableListView在左侧导航栏中的平板电脑屏幕。

I'm using an ExpandableListView in a left nav for a tablet screen.

当用户presses一群孩子在我的扩展列表,我想保持孩子在pressed状态,使用户知道了这孩子的右手内容被显示为

When a user presses a child of a group in my expandable list, I'd like to keep the child in the pressed state so that the user knows for which child the right hand content is being shown for.

有关一个ListView,我能够完成这一行,在code这样的效果:

For a ListView, I was able to accomplish this effect with this line in the code:

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

,然后应用这个选择:

and then applying this selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@drawable/menu_item_pressed" />
<item android:state_pressed="false" android:state_focused="false" android:drawable="@drawable/menu_item_normal" />
<item android:state_pressed="true" android:drawable="@drawable/menu_item_pressed" />
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/menu_item_pressed" />

在state_activiated是的ListView项后ListView项真正为pressed。

The "state_activiated" is true on the listView item after the listView item is pressed.

我希望我的expandableListView同样techinque会想,但它没有。我使用的:

I was hoping this same techinque would would for my expandableListView but it hasn't. I used:

getExpandableListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

和再使用的相同的选择器上面,但它不工作。我也尝试过其他国家,如state_selected和state_checked和那些没有擦出火花。

and then used the same selector above, but it doesn't work. I've also tried other states such as state_selected and state_checked and those don't work either.

在选择上述正确应用pressed而不是pssed状态$ P $。它看起来像然而与ExpandableListView,状态不是激活后,pressing一个孩子。

The selector above correctly applies the pressed and not pressed state. It looks like however with an ExpandableListView, the state is not "activated" after pressing a child.

任何帮助是AP preciated。谢谢!

Any help is appreciated. Thanks!

推荐答案

我结束了不使用选择上的孩子认为解决这一问题。为expandableListView适配器取数据的每个组和儿童。我添加了一个选择布尔的子数据。片段需要每个孩子的选择布尔正确设置为true或false的照顾。然后,在适配器的getChildView的方法,如果孩子的选择布尔是真实的,我设置视图的背景资源,以我的pressed的背景。否则,我将其设置为未pressed的背景。除了这样做是为了使用一个选择器的子视图TextView的改变文字颜色,当pressed达到了预期的效果。

I ended up solving this problem without using a selector on the child view. The adapter for the expandableListView takes data for each group and child. I added a "selected" boolean for the child data. The fragment takes care of correctly setting each child's selected boolean to either true or false. Then, in the adapter's "getChildView" method, if the child's "selected" boolean is true, I set the view's background resource to my "pressed" background. Otherwise I set it to the "not pressed" background. Doing this in addition to using a selector for the textView on the child view to change the text color when pressed achieves the desired effect.

下面是我的东西确实片段,以保持孩子的选择布尔值:

Below are the things my Fragment does to maintain the child's "selected" boolean:

  • 让您的组/子数据作为一个类变量在你的片段
  • onChildClick - 遍历子数据,将所有选择= FALSE。然后设置当前组/孩子的孩子真。刷新expandableListView致电setEmptyView,重设监听器,并设置列表适配器。
  • onGroupClick - 遍历子数据,将所有选择= FALSE。然后设置当前组/孩子的孩子真。
  • 只要在你的片段,你表现出你的清单 - 填充适配器的数据,设置列表适配器,拨打notifyDataSetChanged适配器上。刷新expandableListView致电setEmptyView,重设监听器,并设置列表适配器。

在除了上述我维持currentGroupPosition和currentChildPosition作为类变量。使用setRetainInstance = true,允许这才能正常工作,像屏幕旋转。

In addition to the above I maintain a currentGroupPosition and currentChildPosition as class variables. Using setRetainInstance = true allows this to work properly on things like screen rotates.

这篇关于ExpandableListView - 选择保持孩子在&QUOT; pressed&QUOT;州的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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