使用自定义GestureDetector.SimpleOnGestureListener获取列表视图中项目的位置

时间:2017-03-25 21:01:51

标签: android listview onclicklistener swipe android-gesture

我有一个自定义列表适配器,使用ViewHolder模式设置OnTouchListener将事件传递给我的自定义手势监听器,扩展SimpleOnGestureListener

public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;

        if(convertView == null){
            convertView = inflator.inflate(R.layout.list_item_swipeable, null);
            holder = new ViewHolder();

//            holder.container = (LinearLayout) convertView.findViewById(R.id.container);
            holder.container = (FrameLayout) convertView.findViewById(R.id.container);

            holder.created_at = (TextView) convertView.findViewById(R.id.list_item_created_at);
            holder.link = (TextView) convertView.findViewById(R.id.list_item_link);
            holder.origin = (TextView) convertView.findViewById(R.id.list_item_origin);
            holder.mDetector = new GestureDetectorCompat(mContext, new MyGestureListener (mContext, convertView));

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.created_at.setText( mData.get(position).getCreated_at());
        holder.link.setText( mData.get(position).getLink());
        holder.origin.setText( mData.get(position).getOrigin());


        holder.container.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                holder.mDetector.onTouchEvent(event);
                return true;
            }
        });

        return convertView;

    }

手势监听器覆盖OnFling以检测每行上的滑动,使内容不可见并显示一些按钮。

public class MyGestureListener extends GestureDetector.SimpleOnGestureListener {

    private static final int MIN_DISTANCE = 50;
    private static final String TAG = "MyGestureListener";
    private RelativeLayout backLayout;
    private LinearLayout frontLayout;
    private Animation inFromRight,outToRight,outToLeft,inFromLeft;
    private ViewGroup.LayoutParams frontParams;
    private ViewGroup.LayoutParams backParams;

    public MyGestureListener(Context ctx, View convertView) {
        backLayout = (RelativeLayout) convertView.findViewById(R.id.layout_back);
        frontLayout = (LinearLayout) convertView.findViewById(R.id.layout_front);
        inFromRight = AnimationUtils.loadAnimation(ctx, R.anim.in_from_right);
        outToRight = AnimationUtils.loadAnimation(ctx, R.anim.out_to_right);
        outToLeft = AnimationUtils.loadAnimation(ctx, R.anim.out_to_left);
        inFromLeft = AnimationUtils.loadAnimation(ctx, R.anim.in_from_left);    
    }  
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                           float velocityY) {
        float diffX = e2.getX() - e1.getX();
        float diffY = e2.getY() - e1.getY();
        if (Math.abs(diffX) > Math.abs(diffY)) {
            if (Math.abs(diffX) > MIN_DISTANCE) {
                if(diffX<0){
                    Log.v(TAG, "Swipe Right to Left");
                    if(backLayout.getVisibility()==View.GONE){

                        frontParams = frontLayout.getLayoutParams();
                        backParams = backLayout.getLayoutParams();
                        Log.d("HEIGHT FRONT ", ": "+frontParams.height ); // why -1?
                        Log.d("HEIGHT BACK ", ": "+backParams.height ); // why -1?

                        frontLayout.startAnimation(outToLeft);
                        backLayout.setVisibility(View.VISIBLE);
                        backLayout.startAnimation(inFromRight);
                        frontLayout.setVisibility(View.INVISIBLE);
                    }
                }else{
                    Log.v(TAG, "Swipe Left to Right");
                    if(backLayout.getVisibility()!=View.GONE){
                        backLayout.startAnimation(outToRight);
                        backLayout.setVisibility(View.GONE);
                        frontLayout.setVisibility(View.VISIBLE);
                        frontLayout.startAnimation(inFromLeft);
                    }
                }
            }
        }    
        return true;
    }    
}

不幸的是,在我的主要活动中,我在列表视图本身上使用了setOnItemClickListener,以便获取被点击元素的位置,从我的容器(Arraylist)中检索相应的对象并访问到其id字段,这对于查询数据库以删除或更新它是至关重要的。

lv = (ListView) findViewById(R.id.list);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //String p = (String) parent.getItemAtPosition(position);

        String link_id = linkList.get(position).getId();

        Toast.makeText(getApplicationContext(),
                "item clicked: "+position+"\n link id: "+link_id, Toast.LENGTH_SHORT).show();
    }
});

这不再适用了,在看了这么多关于SO的例子后,我的思绪完全被关闭了。 请您解释如何通过此特定设置从我的主要活动中获取被点击元素的位置?

0 个答案:

没有答案