1

I'm having trouble with select Radio button within a RecyclerView when I long click on Item

At the moment, I've set up up the RecyclerView, have a default LayoutManager, and have an adapter which displays all the data. I've just recently got the onClickListener() working (although I'm not sure if it should go in the onCreateViewHolder() or onBindViewHolder*(- not sure when onBindViewHolder() is called).

My SingelRow XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp"

>


<ImageView
    android:id="@+id/item_icon"
    android:layout_width="64dp"
    android:layout_height="64dp"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="1dp"
    android:layout_marginBottom="1dp"
    android:contentDescription="icon"
    android:src="@drawable/ic_launcher"
    />


<TextView
    android:id="@+id/item_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/item_icon"
    android:layout_alignBaseline="@+id/item_icon"
    android:textColor="@android:color/darker_gray"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:textSize="22dp" />

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/radioButton"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

My Adapter with single choice mode and ViewHolder

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private static RadioButton lastChecked = null;
private static int lastCheckedPos = 0;
Context context;
private ArrayList<ItemData> itemsDatas;


public MyAdapter(ArrayList<ItemData> itemsDatas) {
    this.itemsDatas = itemsDatas;
}


@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {

    View itemLayoutView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_layout, null);


    ViewHolder viewHolder = new ViewHolder(itemLayoutView);
    return viewHolder;
}


@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {


    if (position == 0 && itemsDatas.get(0).isSelected() && viewHolder.radioButton.isChecked()) {
        viewHolder.radioButton.setActivated(true);
        viewHolder.radioButton.setSelected(true);
        lastChecked = viewHolder.radioButton;
        lastCheckedPos = 0;
    }

    viewHolder.txtViewTitle.setText(itemsDatas.get(position).getTitle1());

    viewHolder.imgViewIcon.setImageResource(itemsDatas.get(position).getImageUrl());

    viewHolder.radioButton.setTag(this);
    viewHolder.radioButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RadioButton cb = (RadioButton) v;


            if (cb.isChecked()) {
                if (lastChecked != null) {
                    lastChecked.setChecked(false);
                    itemsDatas.get(lastCheckedPos).setIsSelected(false);
                }

                lastChecked = cb;
                lastCheckedPos = position;
            } else
                lastChecked = null;

            itemsDatas.get(position).setIsSelected(cb.isChecked());
        }
    });


}


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

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

    public TextView txtViewTitle;
    public ImageView imgViewIcon;
    public RadioButton radioButton;


    public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);
        itemLayoutView.setOnClickListener(this);
        itemLayoutView.setOnLongClickListener(this);
        txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.item_title);
        imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.item_icon);
        radioButton = (RadioButton) itemLayoutView.findViewById(R.id.radioButton);
    }

    @Override
    public void onClick(View v) {
        Log.d("TAG", "onClick " + getPosition() + " " + txtViewTitle.getText().toString());
    }

    @Override
    public boolean onLongClick(View v) {
        Log.d("TAG", "onLongClick " + getPosition() + " " + txtViewTitle.getText().toString());
        return true;
    }
}}

When I click On Radio button it is Work but what I want is when I Long click Anywhere On Item I want to select the Radio button in this Item And when I click anywhere On item I want no use it for a Other jobs

I've tried a lot of solutions But did not succeed

I would appreciate any help. Thank you.

MAJHOOOOOL
  • 25
  • 6

2 Answers2

0

If I mistake not you're asking to where you should set a onLongClickListener,In this case modify onCreateViewHolder method like this:

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {

    View itemLayoutView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_layout, null);

   itemLayoutView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            //do something
            return true;
        }
    });
    ViewHolder viewHolder = new ViewHolder(itemLayoutView);
    return viewHolder;
}
Farshad
  • 3,074
  • 2
  • 30
  • 44
0

this is a revised version of your ViewHolder

 class ViewHolder extends RecyclerView.ViewHolder {

public TextView txtViewTitle;
public ImageView imgViewIcon;
public RadioButton radioButton;


public ViewHolder(View itemLayoutView) {
    super(itemLayoutView);

    .
    txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.item_title);
    imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.item_icon);
    radioButton = (RadioButton) itemLayoutView.findViewById(R.id.radioButton);
    itemLayoutView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        //what ever you want
        }
    });
    itemLayoutView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if(!radioButton.isSelected()){
                radioButton.setSelected(true);
            }else{
                radioButton.setSelected(false);
            }
            return false;
        }
    });
}
Sam
  • 151
  • 7