我在ImageView和textview中有一个Layout。我想为每个视图和完整的itemview添加一个onClickListner。我正在使用PagerAdapter。在instantiateItem方法中,我将其称为:
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(context, ProfileActivity.class);
i.putExtra(StaticVariables.INTENT_USERID, v.getId());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ActivityOptionsCompat options = ActivityOptionsCompat.
makeSceneTransitionAnimation(activity, imageView, imageView.getTransitionName());
ActivityCompat.startActivity(activity, i, options.toBundle());
} else {
activity.startActivity(i);
activity.overridePendingTransition(R.anim.gehnachlinks, R.anim.stay);
}
}
});
和textView:
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mLayout.setPanelState(SlidingUpPanelLayout.PanelState.EXPANDED);
}
});
现在我想在完整的itemView中添加一个OnLongClickListner。我尝试将OnLongClickListner实现为完整的适配器,并添加“itemView.setOnLongClickListener(this);”但它没有奏效。我猜它不起作用,因为视图的ClickListeners“剪切”了整个项目的onlongclick。
答案 0 :(得分:0)
尝试在xml
中的viewpager标记中添加android:descendFocusability="blocked"
答案 1 :(得分:0)
您需要通过传递“视图”及其“位置”作为参数,为单击和长按事件创建一个简单的界面。
public static interface ClickListener{
public void onClick(View view,int position);
public void onLongClick(View view,int position);
}
让我们创建一个实现RecyclerView.OnItemTouchListener的内部类。我们还需要在此内部类中使用ClickListener接口。 GestureDetector类用于侦听各种触摸事件。
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中的特定视图组件。我们可以通过findViewById各自的视图来定义该组件。这是给您的代码。
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();
}
}));