水平RecyclerView内的垂直RecyclerView在滚动时滞后

时间:2016-03-18 13:08:10

标签: android android-recyclerview

我有一个水平的RecyclerView。每个孩子都包含一个TextView和一个垂直的RecyclerView。垂直RecyclerView的子项仅包含TextView。

垂直滚动非常流畅,但水平滚动滞后很多。 我曾尝试在onBindViewHolder中使用swapAdapter而不是setAdapter,但这并没有解决问题。我也尝试更改数据集并调用notifyDataSetChanged(),但这也无济于事。

修改

我刚刚发现滚动在横向模式下实际上是平滑的。

横向RecyclerView.Adapter

private SparseArray<GroupArrayAdapter> adapters;

private ArrayList<Category> categories;

private Activity context;

public CategoryArrayAdapter(Context context, ArrayList<Category> categories) {
    this.categories = categories;
    this.context = (Activity)context;

    adapters = new SparseArray<GroupArrayAdapter>();
    for (int i = 0; i < categories.size(); i++) {
        adapters.append(i, new GroupArrayAdapter(context, categories.get(i).getGroups()));
    }
}

public class ViewHolder extends RecyclerView.ViewHolder {
    public TextView name;
    public RecyclerView list;

    public ViewHolder(View view) {
        super(view);
        name = (TextView) view.findViewById(R.id.lblCategoryName);
        list = (RecyclerView) view.findViewById(R.id.lstGroups);
        list.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        list.addItemDecoration(new GroupItemDecoration((int)context.getResources().getDimension(R.dimen.gap)));
        //list.setAdapter(new GroupArrayAdapter(context, new ArrayList<Group>()));
    }
}

@Override
public int getItemCount() {
    return categories.size();
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
    Category category = categories.get(position);
    viewHolder.name.setText(category.getName());
    viewHolder.list.swapAdapter(adapters.get(position), false);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_category, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
}

垂直RecyclerView.Adapter

private ArrayList<Group> groups;

public GroupArrayAdapter(Context context, ArrayList<Group> groups) {
    this.groups = groups;
}

public class ViewHolder extends RecyclerView.ViewHolder {
    public TextView name;

    public ViewHolder(View view) {
        super(view);
        name = (TextView) view.findViewById(R.id.lblGroupName);
    }
}

@Override
public int getItemCount() {
    return groups.size();
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
    Group group = groups.get(position);
    viewHolder.name.setText(group.getName());
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = null;
    view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_group_text, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
}

1 个答案:

答案 0 :(得分:1)

尝试使用此

reyclerView.setHasFixedSize(true)