Recyclerview.addOnItemTouchListener是否支持远程控制输入?

时间:2019-01-29 08:46:29

标签: android android-recyclerview

根据我的问题,我想知道RecyclerView是否可以像正常的触摸事件一样响应遥控器的Enter键。

这是我目前的代码。

lstAmbiance.addOnItemTouchListener(new AmbianceAdapter.RecyclerTouchListener(getApplicationContext(), lstAmbiance, new AmbianceAdapter.ClickListener(){
    @Override
    public void onClick(View view, int position) {
        //some code

    }

    @Override
    public void onDoubleClick(View view, int position) {

    }

    @Override
    public void onLongClick(View view, int position) {

    }
}));

1 个答案:

答案 0 :(得分:0)

让我们创建一个实现RecyclerView.OnItemTouchListener的内部类

class RecyclerTouchListener implements RecyclerView.OnItemTouchListener{

private ClickListener clicklistener;
private GestureDetector gestureDetector;

public RecyclerTouchListener(Context context, final RecyclerView recycleView, final ClickListener clicklistener){

    this.clicklistener=clicklistener;
    gestureDetector=new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            View child=recycleView.findChildViewUnder(e.getX(),e.getY());
            if(child!=null && clicklistener!=null){
                clicklistener.onLongClick(child,recycleView.getChildAdapterPosition(child));
            }
        }
    });
}

@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
    View child=rv.findChildViewUnder(e.getX(),e.getY());
    if(child!=null && clicklistener!=null && gestureDetector.onTouchEvent(e)){
        clicklistener.onClick(child,rv.getChildAdapterPosition(child));
    }

    return false;
}

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {

}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}
}

将ItemTouchListener添加到RecyclerView中,我们将在其中传递RecyclerTouchListener类。这将实现onClick和onLongClick方法。这些方法将侦听视图特定位置上的单击和长按事件。

recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this,
    recyclerView, new ClickListener() {
@Override
public void onClick(View view, final int position) {
    //Values are passing to activity & to fragment as well
    Toast.makeText(MainActivity.this, "Single Click on position        :"+position,
            Toast.LENGTH_SHORT).show();
}

@Override
public void onLongClick(View view, int position) {
    Toast.makeText(MainActivity.this, "Long press on position :"+position,
            Toast.LENGTH_LONG).show();
}

}));

访问RecyclerView项目中的特定视图组件

recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this,
    recyclerView, new ClickListener() {
@Override
public void onClick(View view, final int position) {
    //Values are passing to activity & to fragment as well
    Toast.makeText(MainActivity.this, "Single Click on position :"+position,
            Toast.LENGTH_SHORT).show();
    ImageView picture=(ImageView)view.findViewById(R.id.picture);
    picture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "Single Click on Image :"+position,
                    Toast.LENGTH_SHORT).show();
        }
    });
}

@Override
public void onLongClick(View view, int position) {
    Toast.makeText(MainActivity.this, "Long press on position :"+position,
            Toast.LENGTH_LONG).show();
}

}));