Kabindra Shrestha
Kabindra Shrestha

Reputation: 333

Can't select single item and track position of the item on RecyclerView?

I want to select and highlight the the row of the recyclerview with current time on clicking the days. I can show the rows on clicking the week days. But can't highlight or setBackground to the rows with current time. Once I compare the position with the position from the onBindHolder() method the numbers of rows are highlighted.

Please help me with it. Thank You

The week days are clickable and set rows to other <code>recyclerView</code> and should highlight or set custom drawable background with current time.

This line of code setAdapter() and smooth scrolling to the current time position:

    Calendar calendar = Calendar.getInstance();
    Date date = calendar.getTime();

    int onEpgDaysPosition = calendar.get(Calendar.DAY_OF_WEEK);
    int onAirEpgPosition = getpositionofOnAir(epgHash.get(new SimpleDateFormat("EE", Locale.ENGLISH).format(date.getTime())));

    daysRecyclerView.setLayoutManager(new LinearLayoutManager(PlayerNewActivity.this, LinearLayoutManager.VERTICAL, false));

    EpgDaysRecyclerViewAdapter epgDaysListAdapter = new EpgDaysRecyclerViewAdapter(PlayerNewActivity.this, epgHash, epgKey, epgRecyclerView);
    daysRecyclerView.setAdapter(epgDaysListAdapter);


    epgRecyclerView.setLayoutManager(new LinearLayoutManager(PlayerNewActivity.this, LinearLayoutManager.VERTICAL, false));
    EpgDvrRecyclerViewAdapter epgListAdapter = new EpgDvrRecyclerViewAdapter(PlayerNewActivity.this, epgHash, epgKey, new SimpleDateFormat("EE", Locale.ENGLISH).format(date.getTime()));
    epgRecyclerView.setAdapter(epgListAdapter);

    daysRecyclerView.setLayoutManager(new LinearLayoutManagerWithSmoothScroller(PlayerNewActivity.this));
    daysRecyclerView.smoothScrollToPosition(onEpgDaysPosition);

    epgRecyclerView.setLayoutManager(new LinearLayoutManagerWithSmoothScroller(PlayerNewActivity.this));
    epgRecyclerView.smoothScrollToPosition(onAirEpgPosition);

This is the RecyclerViewAdapter:

public class EpgDvrRecyclerViewAdapter extends RecyclerView.Adapter<EpgDvrRecyclerViewAdapter.ViewHolder> {

private Context context;
private HashMap<String, ArrayList<Epg>> epgHash;
private ArrayList<String> epgKey;
private ArrayList<Epg> individualDayEpg;
private String key;


public EpgDvrRecyclerViewAdapter(Context context,
                                 HashMap<String, ArrayList<Epg>> epgHash, ArrayList<String> epgKey, String key) {

    this.context = context;
    this.epgHash = epgHash;
    this.epgKey = epgKey;
    this.key = key;
    individualDayEpg = epgHash.get(key);

}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.epg_individual_dvr, null);
    return new ViewHolder(v);
}

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

    Epg epg = individualDayEpg.get(position);

    holder.programName.setText(epg.getProgramName());
    holder.programTime.setText(epg.getProgramStartTime() + " - " + epg.getProgramEndTime());

    SimpleDateFormat sdf = new SimpleDateFormat("HHmm");
    int s = Integer.parseInt(sdf.format(new Date()));
    if (Integer.parseInt(epg.getProgramStartTime().replace(":", "")) <= s && s <= Integer.parseInt(epg.getProgramEndTime().replace(":", ""))) {
        holder.epgRow.setSelected(true);
    }

}

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

public static class ViewHolder extends RecyclerView.ViewHolder {

    View v;
    private TextView programName, programTime;
    private LinearLayout epgRow;

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

        v = itemView;

        epgRow = (LinearLayout) itemView.findViewById(R.id.epgRow);
        programName = (TextView) itemView.findViewById(R.id.epgProgramName);
        programTime = (TextView) itemView.findViewById(R.id.epgProgramTime);

    }
  }
}

Upvotes: 0

Views: 1736

Answers (2)

ישו אוהב אותך
ישו אוהב אותך

Reputation: 29783

You can use SparseBooleanArray to save the current selected item.

Do the following in your RecyclerView Adapter class:

// Initialize variable to hold the selected item.
SparseBooleanArray mSelectedItems = new SparseBooleanArray();

// When selecting an item, save it
mSelectedItems.put(your_item_position, true); // Set true when selected

// Then in your view binding, set based on the selection saved before
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
  ...
  if(mSelectedItem.get(position)) {
    // set as selected here
  } else {
    // set as not selected here.
    // SparseBoolean always return false if we haven't add the value yet.
  }
  ...
}

UPDATE

If you want to select only one week days, you can do it by resetting to false all the data in SparseBooleanArray except the current selected item position. Then you need to call notifyItemChanged(int position).

Something like this:

// Do this when you selecting an item:
// save it
mSelectedItems.put(your_item_position, true); // Set true when selected

// Then you reset the items check of the other items
final int checkedItemCount = mSelectedItems.size();
for (int i = 0; i < checkedItemCount; i++) {
  int position = checkedItemPositions.keyAt(i); // item position in the list.
  // reset position except the current selected item.
  if (position != your_item_position) { 
    if (checkedItemPositions.get(position)) { 
       // Item is checked. Need to uncheck it.
       mSelectedItems.put(position, false);
       // notify the change to Adapter
       notifyItemChanged(position);
    }
  }
}

Upvotes: 3

Vinodh
Vinodh

Reputation: 1129

Create a selector in your drawable folder like below and set it as background to the LinearLayout of the row:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="false" 
          android:drawable="@android:color/transparent" />
    <item android:state_selected="true"  
          android:drawable="@android:color/background_dark" />
</selector>

Upvotes: 2

Related Questions