Android ExpandedListView希望SwipeToDismiss仅适用于组

时间:2014-08-01 09:32:58

标签: java android

我从这里得到了似乎是SwipeToDismiss实现的事实版本:google code / Roman Nurik

它很棒。但是,我在ExpandedListView上使用它,我只想让群组被解雇。

在TouchListener事件中,似乎有两个地方我可以进行干预而不允许解雇:

mListView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    // return false if not on a group ???????
    return mSwipeDismissTouchListener.onTouch(view, motionEvent);
 ..

或在SwipeDismissListViewTouchListener事件中:

mSwipeDismissTouchListener = new SwipeDismissListViewTouchListener(
        mListView,
        new SwipeDismissListViewTouchListener.DismissCallbacks() {
            public boolean canDismiss(int position) {
                // return false if not on a group ???????
            }
            public void onDismiss(ListView listView, int[] reverseSortedPositions) {
            }
        }
);

但我无法确定如何判断点击/触摸事件是否发生在群组或儿童身上。

任何想法?

TIA。

2 个答案:

答案 0 :(得分:2)

您需要使用适配器类ViewHolder Pattern,但需要进行一些修改:将其公开,让您的字段公开,就像这样

public static class ViewHolderGroup {
    TextView tvName;
    ImageView ivLogo;
    public boolean isGroup;
}

并将一些变量设置为Tag,这将定义wheater

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    ViewHolderGroup holder;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.row_group_operator, null);
        holder = new ViewHolderGroup();
        holder.tvName = (TextView) convertView.findViewById(R.id.layout_row_group_operator_tv_Name);
        holder.ivLogo = (ImageView) convertView.findViewById(R.id.layout_row_group_operator_iv_Logo);
        holder.ivLogo.setVisibility(View.GONE);
        holder.isGroup = true;  // Here we setting field to know this is group
        convertView.setTag(holder);

    } else {
        holder = (ViewHolderGroup) convertView.getTag();
    }
   ....
 }

然后在前面描述的代码中获取此标记,如此

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    fa = this;
    View rootView = inflater.inflate(R.layout.fg_elist, container, false);

    final ExpandableListView elMain = (ExpandableListView) rootView.findViewById(R.id.layout_fg_elist_elv_Main);
    RouteExpandableListAdapter routesAdapter = new RouteExpandableListAdapter(Rendis.mContext, Rendis.dates, Rendis.routes);
    elMain.setAdapter(routesAdapter);
    registerForContextMenu(elMain);
    elMain.setOnChildClickListener(this);

    elMain.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent motionEvent) {

            Rect rect = new Rect();
            int childCount = elMain.getChildCount();
            int[] listViewCoords = new int[2];
            elMain.getLocationOnScreen(listViewCoords);
            int x = (int) motionEvent.getRawX() - listViewCoords[0];
            int y = (int) motionEvent.getRawY() - listViewCoords[1];
            View child;
            for (int i = 0; i < childCount; i++) {
                child = elMain.getChildAt(i);
                child.getHitRect(rect);

                if (rect.contains(x, y)) {
                    RouteExpandableListAdapter.ViewHolderGroup holder  = (RouteExpandableListAdapter.ViewHolderGroup) child.getTag();
                    if(holder.isGroup){
                        // DO WHAT YOU WANT TO DO
                    }
                    break;
                }
            }

            return false;
        }
    });
    return rootView;
}

希望这会有所帮助

答案 1 :(得分:0)

好的,所以我想我已经解决了,虽然感觉有点hacky:

在适配器中,我为getChildView和getGroupViews添加了一个触摸监听器,这允许我保留当前所选项目的类型。

然后在我的视图中包含事件处理程序中的扩展列表视图我只询问列表适配器当前触摸的事物是否为组,如果是,则可以允许滑动。