Recyclerview单行可选

时间:2015-12-31 09:46:53

标签: android android-recyclerview

我有RecyclerView可点击的功能,但我希望它应该是单行可选的,就像ListView我们有选择模式一样。

@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.player_row, null);
    RecyclerViewHolder viewHolder = new RecyclerViewHolder(view);
    return viewHolder;
}


@Override
public void onBindViewHolder(final RecyclerViewHolder holder, final int position) {
    final Players player = mPlayerList.get(position);
    holder.name.setText(player.getSquadPlayerName());

    holder.itemView.setTag(player);


}


@Override
public int getItemCount() {
    return mPlayerList.size();
}


public class RecyclerViewHolder extends RecyclerView.ViewHolder {

    public CustomTextView name;


    public RecyclerViewHolder(final View itemView) {
        super(itemView);
        name = (CustomTextView) itemView.findViewById(R.id.name);




    }


}

可以点击完整行。如何实现单一选择?

2 个答案:

答案 0 :(得分:0)

    recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new ClickListener() {
                    @Override
                    public void onClick(View view, int position) {
                        drawerListener.onDrawerItemSelected(view, position);
                        mDrawerLayout.closeDrawer(containerView);
                    }

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

                    }
                }));
public interface FragmentDrawerListener {
        void onDrawerItemSelected(View view, int position);
    }

static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

        private GestureDetector gestureDetector;
        private ClickListener clickListener;

        public RecyclerTouchListener(Context context,
                final RecyclerView recyclerView,
                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 = recyclerView.findChildViewUnder(
                                    e.getX(), e.getY());
                            if (child != null && clickListener != null) {
                                clickListener.onLongClick(child,
                                        recyclerView.getChildPosition(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.getChildPosition(child));
            }
            return false;
        }

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

答案 1 :(得分:0)

//DM Create event to make controls visible if check box is selected
        public void UpdateOptions(object update, EventArgs e)
        {
            if(NewUser.Checked == true)
            {
                ConfirmEmailLabel.Visible = true;
                TextBox3.Visible = true;
                ConfirmPasswordLabel.Visible = true;
                TextBox4.Visible = true;
                Firstname.Visible = true;
                TextBox1.Visible = true;
                Lastname.Visible = true;
                TextBox2.Visible = true;
            }
            else
            {
                ConfirmEmailLabel.Visible = false;
                TextBox3.Visible = false;
                ConfirmPasswordLabel.Visible = false;
                TextBox4.Visible = false;
                Firstname.Visible = false;
                TextBox1.Visible = false;
                Lastname.Visible = false;
                TextBox2.Visible = false;
            }
        }