无法在RecyclerView上选择项目的单个项目和跟踪位置?

时间:2016-12-07 08:14:28

标签: android android-recyclerview recycler-adapter

我想在点击日期时选择并突出显示当前时间的recyclerview行。我可以在点击工作日时显示行。但是无法使用当前时间突出显示或setBackground到行。 将positiononBindHolder()方法中的位置进行比较后,行数为highlighted

请帮帮我。谢谢

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

这行代码setAdapter()并平滑滚动到当前时间位置:

    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);

这是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);

    }
  }
}

2 个答案:

答案 0 :(得分:2)

您可以使用SparseBooleanArray保存当前所选项目。

在RecyclerView Adapter类中执行以下操作:

// 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.
  }
  ...
}

更新

如果您只想选择一个工作日,则可以通过将 SparseBooleanArray 中的所有数据重置为false来执行此操作,但当前所选项目位置除外。然后,您需要致电notifyItemChanged(int position)

这样的事情:

// 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);
    }
  }
}

答案 1 :(得分:1)

在drawable文件夹中创建一个选择器,如下所示,并将其设置为行的LinearLayout的背景:

<?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>