单击OnSwipeListener和ExpandableListView无法识别

时间:2015-01-26 08:32:49

标签: android view expandablelistview gestures

我在识别可扩展列表视图上的onGroup点击时遇到问题。我正在为组视图使用自定义滑动侦听器以进行滑动。现在我想要的是在按下它们的同时扩展组,似乎我找不到在组视图上注册OnTouch和onGroupClick的正确方法。我在不刷卡时调用假,因此可以识别其他手势(例如正常点击和长按),但我的群组没有响应点击,因此没有扩展。

现在我的Swipe监听器工作正常但OnGroupClick没有被调用而且没有扩展。当我从getGroupView()方法中删除我的OnSwipeListener时,实际的扩展正常工作。

我在滑动侦听器上的自定义:

public class OnSwipeListener implements View.OnTouchListener
{
    private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());

    public boolean onTouch(final View v, final MotionEvent event)
    {
        return gestureDetector.onTouchEvent(event);
    }

    private final class GestureListener extends GestureDetector.SimpleOnGestureListener
    {

        private static final int SWIPE_THRESHOLD = 70;
        private static final int SWIPE_VELOCITY_THRESHOLD = 70;

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
        {
            boolean result = false;
            try
            {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY))
                {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD)
                    {
                        if (diffX > 0)
                        {
                            result = onSwipeRight();
                        }
                        else
                        {
                            result = onSwipeLeft();
                        }
                    }
                }
                else
                {
                    if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD)
                    {
                        if (diffY > 0)
                        {
                            result = onSwipeBottom();
                        }
                        else
                        {
                            result = onSwipeTop();
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                exception.printStackTrace();
            }
            return result;
        }
    }

    public boolean onSwipeRight()
    {
        return false;
    }

    public boolean onSwipeLeft()
    {
        return false;
    }

    public boolean onSwipeTop()
    {
        return false;
    }

    public boolean onSwipeBottom()
    {
        return false;
    }
}

我的可扩展列表视图及其在组点击侦听器设置:

expandable_list_view = (ExpandableListView) findViewById(R.id.activity_profile_expandable_listview);
setGroupParents(); setChildData();
ExpandableListAdapter adapter = new ExpandableListAdapter(this, parentItems, childItems);
expandable_list_view.setAdapter(adapter);

expandable_list_view.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener()
{
    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id)
    {
        parent.smoothScrollToPosition(groupPosition);

        if (parent.isGroupExpanded(groupPosition))
        {
            parent.collapseGroup(groupPosition);
        } else {
            parent.expandGroup(groupPosition);
        }

        return false;
    }
});

最后从我的自定义适配器获取getGroupView以获取可扩展列表视图:

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{

if (convertView == null)
{
    convertView = inflater.inflate(R.layout.layout_activity_expandableitem, null);
}

final View converted_view = convertView;

final LinearLayout front_view = (LinearLayout) converted_view.findViewById(R.id.layout_activity_expandableitem_front_view); front_view.setTag(Position.CENTER);
final LinearLayout behind_view = (LinearLayout) converted_view.findViewById(R.id.layout_activity_expandableitem_behind_view);
final View confirmation_view = inflater.inflate(R.layout.slide_profile, null); confirmation_view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
final View cancel_view = inflater.inflate(R.layout.slide_cancel, null); cancel_view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));


//  Set onTouchListener
front_view.setOnClickListener(null);
front_view.setOnTouchListener(new OnSwipeListener()
{
    @Override
    public boolean onSwipeRight()
    {
        Position position = (Position) front_view.getTag();
        if (position == Position.CENTER)
        {
            behind_view.removeAllViews();
            behind_view.addView(confirmation_view);
            showConfirmation(front_view);

            front_view.setTag(Position.RIGHT);
        }
        else if (position == Position.LEFT)
        {
            //behind_view.removeAllViews();
            showCenter(front_view, Position.RIGHT);

            front_view.setTag(Position.CENTER);
        }

        return true;
    }

    @Override
    public boolean onSwipeLeft()
    {
        Position position = (Position) front_view.getTag();
        if (position == Position.CENTER)
        {
            behind_view.removeAllViews();
            behind_view.addView(cancel_view);
            showInfo(front_view);

            front_view.setTag(Position.LEFT);
        }
        else if (position == Position.RIGHT)
        {
            //behind_view.removeAllViews();
            showCenter(front_view, Position.LEFT);

            front_view.setTag(Position.CENTER);
        }

        return true;
    }
});

//  Return the view
return converted_view;

感谢您对如何解决此问题的想法。

2 个答案:

答案 0 :(得分:2)

所以这是正在发生的事情:

  1. 当您设置OnGroupClick时,它会设置为整体 listview(每行listview的实施将“弄清楚”触摸listView
  2. 时点击了哪一行
  3. 设置SwipeListener时,请将其附加到
  4. 当两者都设置时,该行在整个listview之前并且将处理(消耗)触摸事件,而触摸事件又不会到达列表视图(这就是为什么你在单独时让它们工作的原因)。
  5. 您可以选择:

    1. 子类ListView并实施onInterceptTouchEvent()。这样,您的listview将始终能够“看到”触摸事件并对其进行操作。 (请注意,您可能会遇到同时检测到滑动和点击的情况。)
    2. 实施您自己的ListView OnTouch将负责确定哪一行有互动,识别滑动手势(通过您的听众)并点击事件并决定哪些获得先例。

答案 1 :(得分:0)

您可以尝试我自己实施的following解决方案,并确信它有效。我使用了onClickListener而不是SwipeListener。希望它可以帮助您解决问题。