我创建了一个带有CardView的RecyclerView,其中包含15分钟的时间间隔文本,具体取决于我的服务器给我的内容,例如(0:00,0:15,0:30,... 23:30,23:45) 。现在我想这样做,如果用户输入" 11:00"在之前的活动中,我将获得包含" 11:00"在这项活动中。我现在可以使用getAdapterPosition()来获取位置,但这只是在我滚动到或通过包含" 11:00"的卡片时。如果我没有联系到我的特定卡,我的位置就不清楚了。
我的CardAdapter:
public class SuggestTimeCardAdapter extends RecyclerView.Adapter<SuggestTimeCardAdapter.ViewHolder> {
private Context context;
List<SuggestTimeList> suggestTimeLists;
Integer selected_position = -1;
public SuggestTimeCardAdapter(List<SuggestTimeList> suggestTimeLists, Context context){
super();
this.suggestTimeLists = suggestTimeLists;
this.context = context;
}
@Override
public SuggestTimeCardAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.suggest_time_card, parent, false);
SuggestTimeCardAdapter.ViewHolder viewHolder = new SuggestTimeCardAdapter.ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(final SuggestTimeCardAdapter.ViewHolder holder, final int position) {
final SuggestTimeList suggestTimeList = suggestTimeLists.get(position);
holder.tvSuggestTime.setText(suggestTimeList.getSuggestTime());
if(holder.tvSuggestTime.getText().equals("11:00")){
//position at 11 is holder.getAdapterPosition()
}
}
@Override
public int getItemCount() {
return suggestTimeLists.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView tvSuggestTime;
public CardView suggest_time_card_view;
public ViewHolder(View itemView) {
super(itemView);
tvSuggestTime = (TextView) itemView.findViewById(R.id.tvSuggestTime);
suggest_time_card_view = (CardView) itemView.findViewById(R.id.suggest_time_card_view);
}
}
}
答案 0 :(得分:2)
您可以在公共方法中迭代suggestTimeLists并检查suggestTimeLists.getSuggestTime()。equals(“11:00”)并返回项目位置。
public int getPreferedPosition(){
for(int i=0; i<suggestTimeLists.size(); i++){
if(suggestTimeLists.get(i).getSuggestTime().equals("11:00")){
return i;
}
}
}
只需从你的活动中调用mAdapter.getPreferedPosition();