3

我有一个带有自定义适配器的 RecyclerView,它扩展了 RecyclerView.Adapter

我在运行时使用 Textview 创建 LinearLayout 并在 RecyclerView 的每一行中对其进行膨胀

例如,RecyclerView 的第一行将在运行时创建 2 或 3 个 Textview,第二行将在运行时创建 2 或 3 个 Textview,第三行将有一些 Textview...等等...

如果我检查我的日志,它的工作几乎是完美的......但是当我向下滚动它时,它只是将一些文本视图放置在错误的位置,这意味着当我向下滚动到错误的位置时我会再次获得以前的文本视图

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    //Movie movie = mItems.get(i);
    hm2 = new HashMap<String, ArrayList<PD_Data>>();
    sub_rows2 = new ArrayList<PD_Data>();
    hm2=categories.get(i);
    String key=hm2.keySet().toArray()[0].toString();
    sub_rows2=hm2.get(key);

    Log.i(LOG_TKT,key);

    viewHolder.textview_category.setText(key);

    LayoutInflater inflater;
    View new_sub_row;

    for(int x=0;x<sub_rows2.size();x++){
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        new_sub_row = inflater.inflate(R.layout.recyclerview_pd, null);
        TextView heading2 = (TextView)new_sub_row.findViewById(R.id.heading2);
        heading2.setText(sub_rows2.get(x).sub_heading);
        Log.i(LOG_TKT,sub_rows2.get(x).sub_heading);
        TextView detail2 = (TextView)new_sub_row.findViewById(R.id.detail2);
        detail2.setText(sub_rows2.get(x).value);
        Log.i(LOG_TKT, sub_rows2.get(x).value);
        viewHolder.linearlayout_recyclerview_pd.addView(new_sub_row);
    }
    //viewHolder.imgThumbnail.setImageResource(movie.getThumbnail());
    hm2 = new HashMap<String, ArrayList<PD_Data>>();
    sub_rows2 = new ArrayList<PD_Data>();
}

我究竟做错了什么 ?

4

2 回答 2

2

从您的问题中可以明显看出您并不熟悉如何使用 RecyclerViews。您需要阅读该主题。是一个好的开始。

基本上,在Bind ViewHolder() 上仅负责将您的数据绑定到您的 viewHolders,这些 viewHolders 保存您通过onCreateViewHolder(). 这样做的原因是 RecyclerView回收您的视图,因此它不必在每次滚动时创建新视图。

在您的情况下,您似乎需要使用一些技术来告诉 RecyclerView 为不同的项目使用不同的 viewHolders。看看你怎么能在这里做到这一点。

于 2015-10-12T20:45:27.210 回答
1

我已经在另一个问题中给出了更好地使用 ViewHolder 绑定数据的答案。您可以在此处查看RecyclerView 导致回收时出现问题,希望这对您有所帮助。这将很好地解决你的问题。 编辑

你的问题解决了吗?如果没有,请考虑我的建议。你知道问题对吗?假设您为项目 0 创建一个 ViewHolder 并在其中添加一些文本。滚动时假设此 ViewHolder 为项目编号 10 回收,然后根据您的代码,它将添加新的文本行以及您为项目编号 0 添加的一些文本行。您可以像这样解决它

LayoutInflater inflater;
View new_sub_row;

//check here if the linear layout already has previusely added child, if  yes remove them   
if(viewHolder.linearlayout_recyclerview_pd.getChildCount()>0){
    viewHolder.linearlayout_recyclerview_pd.removeAllViews();
}
for(int x=0;x<sub_rows2.size();x++){
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    new_sub_row = inflater.inflate(R.layout.recyclerview_pd, null);
    TextView heading2 = (TextView)new_sub_row.findViewById(R.id.heading2);
    heading2.setText(sub_rows2.get(x).sub_heading);
    Log.i(LOG_TKT,sub_rows2.get(x).sub_heading);
    TextView detail2 = (TextView)new_sub_row.findViewById(R.id.detail2);
    detail2.setText(sub_rows2.get(x).value);
    Log.i(LOG_TKT, sub_rows2.get(x).value);
    viewHolder.linearlayout_recyclerview_pd.addView(new_sub_row);
}
于 2016-02-13T18:04:18.873 回答