如何获取 RecyclerView 的 item 视图

Posted

技术标签:

【中文标题】如何获取 RecyclerView 的 item 视图【英文标题】:How to get item 's view of RecyclerView 【发布时间】:2015-11-17 13:53:44 【问题描述】:

如何在 Telerik 的 RadListViewRecyclerView 中获取 item 的视图; 例如:我想获得TextViewImageView 包含的RadListViewRecyclerView.

以前,我可以通过getView() in ListView 得到它,但现在我不能;

我找了很多文件,但没有想法。

以下是我的adapter 代码。

public class ReminderAdapter extends ListViewAdapter 

private List<Map<String, Object>> dataList;
private Context mContext;

public ReminderAdapter(List items, Context context) 
    super(items);
    this.dataList = items;
    mContext = context;


@Override
public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
    View view = LayoutInflater.from(mContext).
            inflate(R.layout.remind_custom_radlistview_item, parent, false);
    return new ReminderViewHolder(view);



@Override
public void onBindViewHolder(ListViewHolder holder, int position) 
    super.onBindViewHolder(holder, position);
    ReminderViewHolder reminderViewHolder = (ReminderViewHolder) holder;
    reminderViewHolder.timeTextView.setText((String) dataList.get(position).get("time"));
    reminderViewHolder.remainTextView.setText((String) dataList.get(position).get("remain"));
    reminderViewHolder.selectImageView.setImageResource((Integer) dataList.get(position).get("pic"));
    if (position == dataList.size() - 1) 
        reminderViewHolder.mView.setVisibility(View.GONE);
    




class ReminderViewHolder extends ListViewHolder 

    TextView timeTextView;

    TextView remainTextView;

    ImageView selectImageView;

    View mView;
    MaterialRippleLayout materialRippleLayout;

    public ReminderViewHolder(View itemView) 
        super(itemView);
        timeTextView = (TextView) itemView.findViewById(R.id.custom_remind_list_timeTextView);
        remainTextView = (TextView) itemView.findViewById(R.id.custom_remind_list_remainTextView);
        selectImageView = (ImageView) itemView.findViewById(R.id.custom_remind_list_selectImage);
        mView = (View) itemView.findViewById(R.id.custom_remind_list_horizontal_line);
        materialRippleLayout = (MaterialRippleLayout) itemView.findViewById(R.id.acustom_remind_list_ripple_layout);
    


【问题讨论】:

【参考方案1】:

我有个主意:

创建一个界面:

public interface RadListViewClickListener 
   public void RadListViewClick(View v,int positon);

在你的活动或片段中实现它

public class RemindActivity extends AppCompatActivity
    implements RadListViewClickListener

     public void RadListViewClick(View v, int positon) 


     

在你的适配器

public class ReminderAdapter extends ListViewAdapter 

private List<Map<String, Object>> dataList;
private Context mContext;
private  RadListViewClickListener mListener;
public ReminderAdapter(List items, Context context,RadListViewClickListener listener) 
    super(items);
    this.dataList = items;
    this.mContext = context;
    this.mListener=listener;

    class ReminderViewHolder extends ListViewHolder implements View.OnClickListener 

    TextView timeTextView;

    TextView remainTextView;

    ImageView selectImageView;

    View mView;

    public ReminderViewHolder(View itemView) 
        super(itemView);

        timeTextView = (TextView) itemView.findViewById(R.id.custom_remind_list_timeTextView);
        remainTextView = (TextView) itemView.findViewById(R.id.custom_remind_list_remainTextView);
        selectImageView = (ImageView) itemView.findViewById(R.id.custom_remind_list_selectImage);
        mView = (View) itemView.findViewById(R.id.custom_remind_list_horizontal_line);
        itemView.setOnclickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 

                mListener.RadListViewClick(itemView,getAdapterPosition());
            
        );
    

    @Override
    public void onClick(View v) 
        if(v instanceof  MaterialRippleLayout)
            mListener.RadListViewClick(selectImageView,this.getPosition());
        
    

这就是全部了,有人有其他好的答案吗?我希望它。太棒了!

【讨论】:

以上是关于如何获取 RecyclerView 的 item 视图的主要内容,如果未能解决你的问题,请参考以下文章

如何实现让RecyclerView有不同尺寸的item

string 获取最后一部分 android 文件下载recycleview item和progressbar RecyclerView.Adapter生命周期 如何取消ctrl+shift加a

如何从RecyclerView项中获取Value

如何在 RecyclerView 的 CardView 中实现对 Item Click 的操作以显示结果

滚动 NestedScrollView 时如何获取 recyclerview 的当前项目位置?

Android教程2020 - RecyclerView显示多种item