关于项目选择的Recyclerview,长按

时间:2017-10-06 15:41:01

标签: android

我有循环器视图,我从jsonArray解析数据并将其显示为lists.now我想在我长时间点击我已经编码的位置进行长时间点击时突出显示该行。如何突出显示所选行。请帮助我这个。

recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new ClickListener() {
                @Override
                public void onClick(View view, int position) {

                }

                @Override
                public void onLongClick(View view, int position) {
                    Toast.makeText(MainActivity.this, "long clicked", Toast.LENGTH_SHORT).show();

                }
            }));

4 个答案:

答案 0 :(得分:2)

当您在适配器中执行 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/activity_restaurant_list_layout"> <android.support.v7.widget.AppCompatImageView android:layout_width="wrap_content" android:layout_height="200dp" android:scaleType="centerCrop" android:id="@+id/activity_restaurant_list_image"/> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="20dp" android:id="@+id/listview_restaurants" android:divider="@android:color/transparent" android:dividerHeight="10dp"> </ListView> </LinearLayout> 时,我会尝试添加longClick listener

onBindViewHolder

答案 1 :(得分:1)

试试这个。在适配器中,在OnBindViewHolder方法下,如:
@Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
    holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            return false;
           //todo to highlight the row
        }
    });
  }

答案 2 :(得分:1)

定义一个类

public class RecyclerViewItemClickListener implements RecyclerView.OnItemTouchListener {

    //GestureDetector to detect touch event.
    private GestureDetector gestureDetector;
    private QueriesListFragment.ClickListener clickListener;

    public RecyclerViewItemClickListener(Context context, final RecyclerView recyclerView, final QueriesListFragment.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) {
                //Find child on x and y position relative to screen
                View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (child != null && clickListener != null) {
                    clickListener.onLongClick(child, recyclerView.getChildLayoutPosition(child));
                }
            }
        });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        //On Touch event
        View child = rv.findChildViewUnder(e.getX(), e.getY());
        if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
            clickListener.onClick(child, rv.getChildLayoutPosition(child));
        }
        return false;
    }

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

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
}

定义界面

public interface ClickListener {
    void onClick(View view, int position);

    void onLongClick(View view, int position);
}

将此分配给回收站视图

myRecyclerView.addOnItemTouchListener(new RecyclerViewItemClickListener(getActivity(), mQueriesListRecyclerView, new ClickListener() {
        @Override
        public void onClick(View view, int position) {
            //triggers when click
        }

        @Override
        public void onLongClick(View view, int position) {
             //triggers when you long press
        }
    }));

答案 3 :(得分:0)

在您的情况下,如果您将longClicked项目位置设为int并将其自身设为view,则可以执行此操作:

            recyclerView.addOnItemTouchListener(new 

                RecyclerTouchListener(getApplicationContext(), recyclerView, new ClickListener() {
                                @Override
                                public void onClick(View view, int position) {

                                }

                                @Override
                                public void onLongClick(View view, int position) {
                                    view.setTag("Selected"); 
                                    recyclerView.getAdapter().notifyItemChanged(position);
                                    return true;
                                }
                            }));

然后在RecyclerView.Adapter shuld中执行此操作:

@Override
public void onBindViewHolder(YourViewHolderClass holder, int position) {
          if(holder.getTag().equals("Selected"){
                    //do highlight
                    //for example whit [RecyclerView.ItemDecoration][1]
                }else{
                    //remove highlight
                    //else is mandatory in order to prevent bugs when view is recycled
                }
    });
}