天天看點

記錄RecyclerView使用時item使用問題解決方式題記:使用過程中需要擷取item中單個控件的text。但擷取錯位。

題記:使用過程中需要擷取item中單個控件的text。但擷取錯位。

解決方式挺簡單的:

首先初始化Recyclerview,設定其點選事件後,

使用rv.getChildAt(postion).findViewById(R.id.tv_news_title);即可擷取相應内容而不會錯誤。                後來發現除了前三個沒有,其餘的點選會找不到。      

是以找到了這個方法

View view = layoutManager.findViewByPosition(postion);//position為控件位置
CardView layout = (CardView) view;//CardView為item的根布局

tv_name = layout.findViewById(R.id.tv_news_title);//這裡就可以找到相應控件