ExpandableListView开崩溃的问题 [英] ExpandableListView open collapse problem

查看:685
本文介绍了ExpandableListView开崩溃的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是自定义的扩展列表适配器。我想强调一个孩子,当用户点击就可以了。这工作得很好,直到用户打开/折叠组。假设用户触摸集团2项1.这突显集团2项1。然后用户打开组1.现在第3组第2项被突出显示。我做了一些测试选择不同的项目,我无法找到一个模式,其中突出显示的行会跳转到。有时它在列表中了,有时也将其打倒。我无法找出逻辑把我的活动的 onGroupExpandListener onGroupCollapseListener 来rehighlight正确的观点。任何想法?

I am using a custom expandable list adapter. I highlight a child when the user clicks on it. This works fine until the user opens/collapses a group. Say the user touches Group 2 Item 1. This highlights Group 2 Item 1. Then the user opens Group 1. Now Group 3 Item 2 is highlighted. I have done some testing choosing different items and I can not find a pattern where the highlighted row will jump to. Sometimes its up in the list, sometimes its down. I'm having trouble figuring out the logic to put in my activity's onGroupExpandListener and onGroupCollapseListener to rehighlight the correct view. Any ideas?

编辑:在我的onChildClickListener当前code

Current code inside my onChildClickListener

if (groupPosition == 0){ 
      switch(childPosition) {
      case 0: 
        previouslySelectedView.setBackgroundResource(R.color.transparent);
        currentlySelectedView.setBackgroundResource(R.color.blue); 
        break;

同code所有组/儿童

Same code for all the groups/children

推荐答案

在ExpandableListView种选择项,通过平面列表(绝对positiond)制成。因此,如果打开newlly组是currenet选择之前和具有较少的孩子然后选择将向上移动,反之亦然。我建议设置选择模式,没有和实施的onclick /扩展逻辑来处理专注于你自己 - 实现标签视图,例如,并通过标签设置当前突出显示的项目

Item slection in ExpandableListView is made through flat list (absolute positiond). Thus if the newlly opened group is before the currenet selection and has fewer children then the selection will move up and vice versa. I suggest to set choice mode to none and implement onclick/expand logic to handle focus on your own - implement tags for views, for example, and set the currently highlighted item via the tag.

下面是一些建议:

  1. 在ExpandableListView.OnChildClickListener第一执行ExpandableListView.findViewWithTag(theTag),以检查是否有这样的标签观点和取消标记它们(也setTag(空))和恢复的背景。然后,该项目点击setTag(theTag),改变背景选择。当然,你可以有一些其他的逻辑,并标有多个项目。需要注意的是,一旦视图被破坏,你就失去了选择(例如在膨胀)。
  2. 有一些自定义地图或东西,将持有的观点的一个独特的ID和(联合国)标记的状态。这将允许保持持久的选择跨卷轴和扩大了最佳的解决方案。
  3. 在后备适配器引入标记状态。因此标识将是持久的甚至在应用程序启动/停止。不是一个好方法,但是,因为选择更多的是用户界面行为。

我目前做的ExpandableListView选择与列表中选择模式多。但是,因为,正如我所说的,选择是,每个位置我在功能方面牺牲 - 也就是,我清楚每当操纵的选择。在previous执行是自定义地图保持了所选择的ID和 - 说实话 - 这是比较好的办法

I am currently doing an ExpandableListView selection with Choice Mode Multiple of the list. But since, as I said, the selection is per position I had to sacrifice in terms of functionality - namely, I clear the selection whenever a manipulation is made. The previous implementation was with custom Map holding the selected IDs and - to honest - it was the better approach.

这是我怎么弄所选的ID(还记得我用的选择方式多):

This is how I get selected IDs (remember I use choice mode multiple):

final SparseBooleanArray checkedPositions = expList.getCheckedItemPositions();
final ExpandableListAdapter adapter = expList.getExpandableListAdapter();
List<Long> checkedIds = new ArrayList<Long>();
if (packedPositionType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
    for (int i = checkedPositions.size() - 1; i >= 0; i--) {
        if (checkedPositions.valueAt(i)) {
            checkedIds.add(adapter.getGroupId(checkedPositions.keyAt(i)));
        }
    }
}

在你的情况,但是,你将要检查的儿童挤满位置。另请注意,我的适配器具有稳定的(唯一的)IDS。如果没有稳定的ID,那么你可以依靠ExpandableListView的getPackedPositionForChild()方法,并存储包装的位置标记。

In your case, though, you will want to check for CHILD packed positions. Also note that my adapter has stable (unique) ids. If you do not have stable IDs then you can rely on ExpandableListView's getPackedPositionForChild() method and store the packed position as marked.

这篇关于ExpandableListView开崩溃的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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