RecyclerView在某些设备上非常慢(不是全部)

时间:2018-03-10 08:56:42

标签: java android android-recyclerview

在一些设备中滚动RecyclerView非常慢,而在其他设备中滚动正常 我面临这个问题形式3天,我尝试了许多解决方案形成互联网和stackoverflow但他们都没有解决这个问题
我想多次添加xml布局到ViewHolder的膨胀布局 我使用xml布局在onBindViewHolder中进行循环以测试滚动 但是在某些设备中滚动速度非常慢,并且可以找到其他部分 这是我的循环器视图适配器代码

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>

1 个答案:

答案 0 :(得分:0)

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>

这里使用嵌套滚动行为,例如在循环视图中添加以下行 应用:layout_behavior = “@串/ appbar_scrolling_view_behavior”