【问题标题】:RecyclerView very slow in some device (not all)RecyclerView 在某些设备上非常慢(不是全部)
【发布时间】:2018-03-10 18:10:50
【问题描述】:

RecyclerView 的滚动在某些设备上非常慢,而在其他设备上则正常 我在 3 天后面临这个问题,我尝试了许多互联网和 stackoverflow 的解决方案,但都没有解决这个问题
我想多次将 xml 布局添加到 ViewHolder 的膨胀布局中 我使用 xml 布局在 onBindViewHolder 内制作 for 循环以测试滚动 但是在某些设备中滚动速度很慢,并且可以与其他设备一起使用 这是我的回收站视图适配器代码

public class ExamQuestionsListAdapter extends RecyclerView.Adapter<ExamQuestionsListAdapter.ViewHolder> {
public ExamQuestionsListAdapter(Context ctx, List<ExamQuestionsList> ex_Q_List) {
    this.ctx = ctx;
    this.ex_Q_List = ex_Q_List;


    dbManger=new DataBaseManager(ctx);
    db=dbManger.getWritableDatabase();


    shared = ctx.getSharedPreferences("examsShared", Context.MODE_PRIVATE);

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new ViewHolder(LayoutInflater.from(ctx).inflate(R.layout.do_exam_card_view,parent,false));
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    for(int i=0;i<30;i++){
        View v=LayoutInflater.from(ctx).inflate(R.layout.c_m_layout,null);
        holder.questionsLayout.addView(v);
    }

}

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

class ViewHolder extends RecyclerView.ViewHolder{

    LinearLayout questionsLayout;

    public ViewHolder(View itemView) {
        super(itemView);
        questionsLayout=(LinearLayout)itemView.findViewById(R.id.q_layout);


    }
}
}

这是在方法 onCreateViewHolder 中膨胀的布局的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:padding="10dp"

    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/q_layout"
        android:background="@color/orange"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/question_tumber"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/question_title"

            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/res"
            />
    </LinearLayout>




</android.support.v7.widget.CardView>

这是我在 for 循环中使用的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="rtl"
android:background="#FFF"
>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:id="@+id/c_m_q"
    />
<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RadioButton
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        android:id="@+id/first_choice"

        />
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"

            android:id="@+id/second_choice"

            />
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"

            android:id="@+id/third_choice"

            />
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"

            android:id="@+id/fourth_choice"

            />

</RadioGroup>
</LinearLayout>

【问题讨论】:

  • 这条线花了你 -> 查看 v=LayoutInflater.from(ctx).inflate(R.layout.c_m_layout,null); holder.questionsLayout.addView(v);
  • 我必须在持有人内添加布局,因为每个持有人都有子项目应该在单独的布局中
  • 无需将布局膨胀 30 次,只需再创建一个 ViewHolder 类并为其分配数据。
  • 我循环只是为了测试,你能不能等几分钟,我会试试你的解决方案
  • 您错误地使用了recyclerview。视图的成本非常高,我建议您制作一个哈希图来将子项目链接到它们的等价项。

标签: java android android-recyclerview


【解决方案1】:

NestedScrollView,顾名思义,用于在另一个滚动视图中需要滚动视图时使用。通常这很难实现,因为系统无法决定滚动哪个视图。

这就是 NestedScrollView 的用武之地。

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/keyline_1">

    </RelativeLayout>

    <View
        android:id="@+id/separator"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#e5e5e5" />

   <android.support.v7.widget.RecyclerView
    android:id="@+id/conversation"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

</android.support.v4.widget.NestedScrollView>

在这里您使用嵌套滚动行为,例如在回收视图中添加以下行 app:layout_behavior="@string/appbar_scrolling_view_behavior"

【讨论】:

    猜你喜欢
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多