从API 27迁移到29后,Android recyclerview的性能不佳

时间:2019-07-08 03:13:06

标签: android android-recyclerview

已迁移的应用程序,以支持最新的API29。除一个令人困惑的问题外,一切进展顺利,应用程序正常运行;

使用SQLite查询填充recyclerview,该查询可以检索500多个记录,并将结果显示在如下所示的明细活动中。

enter image description here

这在API 27上始终可以正常工作,几乎立即打开并填充了详细活动,而sql调用,活动代码和布局在5年内没有变化。但是,在 API 29 版本上,使用相同的代码,该应用程序需要 3+秒才能显示相同的数据(很小的记录集还是很快)。

迁移(包括“迁移到AndroidX”)在应用程序同步/构建时均正常进行,而没有出现错误。只需很少的编辑,Android Studio即可完成所有工作。

下面的OLD API 27 build.gradle(模块:应用)

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:design:27.0.2'
compile 'com.android.support:appcompat-v7:27.0.2'
compile 'com.android.support:cardview-v7:27.0.2'
compile 'com.android.support:recyclerview-v7:27.0.2'
}

下面的新API 29 build.gradle(模块:应用)

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
}

下面的API 29 gradle.properties

android.enableJetifier=true
android.useAndroidX=true

经过大量测试并阅读了Slow Rendering之后,我认为我已将其固定在API 29中的androidx.recyclerview:recyclerview:1.0.0上,花费了更长的时间来为活动UI构建输出,这可能是由于我的编码和/或布局结构。

这是从MainActivity调用的.java活动(无导入和声明)

public class SenderDetailActivity extends AppCompatActivity {

SQLActivity db3;
List<TXTF> dlist = new ArrayList<>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.msgh_detail);

    db3 = new SQLActivity(getApplicationContext());

    dlist.addAll(db3.arrayMSGHmsgsndrdetail("SENT"));

    RecyclerView rv = (RecyclerView) findViewById(R.id.detail_recyclerview);
    setupRecyclerView(rv);

private void setupRecyclerView(RecyclerView recyclerView) {
    recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
    recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(getApplicationContext(), dlist));
}

public static class SimpleStringRecyclerViewAdapter
        extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {

    private final TypedValue mTypedValue = new TypedValue();
    private int mBackground;
    private List<TXTF> mValues;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public String mBoundString;
        public String mTxtsndrString;
        public String mTxtntwkString;
        public String mTxtaddrString;
        public String mTxtstsfString;
        public String mTxtgrpcString;
        public String mTxtacknString;

        public final View mView;
        public final ImageView mImageView;
        public final TextView mTextView;
        public final TextView mDayView;
        public final TextView mTimeView;
        public final CardView mCardView;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            mImageView = (ImageView) view.findViewById(R.id.avatar);
            mDayView = (TextView) view.findViewById(R.id.DayMMdd);
            mTextView = (TextView) view.findViewById(R.id.text2);
            mTimeView = (TextView) view.findViewById(R.id.arrivaltime);
            mCardView = (CardView) itemView.findViewById(R.id.detail_cardview);
        }
    }

    public SimpleStringRecyclerViewAdapter(Context context, List<TXTF> items) {
        context.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
        mBackground = mTypedValue.resourceId;
        mValues = items;
    }

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

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mBoundString = mValues.get(position).getMSGSRCE();
        holder.mBoundString = mValues.get(position).getsMSGSTSC();
        holder.mTxtsndrString = mValues.get(position).getMSGSNDR();
        holder.mTxtntwkString = mValues.get(position).getsMSGNTWK();
        holder.mTxtaddrString = mValues.get(position).getsMSGADDR();
        holder.mTxtgrpcString = mValues.get(position).getsMSGGRPC();
        holder.mTxtstsfString = mValues.get(position).getsMSGSTSF();
        holder.mTxtacknString = mValues.get(position).getsMSGACKN();
        if (mValues.get(position).getMSGRSTAT() != null && mValues.get(position).getMSGRSTAT().equals("M")){
            holder.mCardView.setCardBackgroundColor(Color.parseColor("#ffff99"));
            if (holder.mTxtstsfString.equals("1")){
                holder.mImageView.setImageResource(R.drawable.status_1);
            } else if (holder.mTxtstsfString.equals("2")){
                holder.mImageView.setImageResource(R.drawable.ic_forum_blu_24dp);
            } else {
                holder.mImageView.setImageResource(R.drawable.status_0);
            }

        } else { holder.mCardView.setCardBackgroundColor(Color.parseColor("#ccecf9"));
                 holder.mImageView.setImageResource(R.drawable.status_3);
        }
        holder.mDayView.setText(mValues.get(position).getsMSGDATE().substring(0,10));
        holder.mTimeView.setText(mValues.get(position).getsMSGDATE().substring(11,16));
        holder.mTextView.setText(mValues.get(position).getMSGTEXT());

        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Context context = v.getContext();
                Intent intent = new Intent(context, MessageDetailActivity.class);
                intent.putExtra("MSGSRCE", holder.mBoundString);
                intent.putExtra("MSGSNDR", holder.mTxtsndrString);
                intent.putExtra("MSGNTWK", holder.mTxtntwkString);
                intent.putExtra("MSGADDR", holder.mTxtaddrString);
                intent.putExtra("MSGSTSF", holder.mTxtstsfString);
                intent.putExtra("MSGGRPC", holder.mTxtgrpcString);
                ((Activity) context).startActivityForResult(intent, 62);

            }
        });
    }

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

这是.java中所指的R.layout.msgh_detail

<?xml version="1.0" encoding="utf-8"?>

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_collapseMode="pin" />

</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="75dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

        <include
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            layout="@layout/msgh_detail_list" />

    </LinearLayout>

</androidx.core.widget.NestedScrollView>

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/replyCard"
    android:layout_marginBottom="2dp"
    android:layout_marginLeft="@dimen/card_margin"
    android:layout_marginRight="@dimen/card_margin"
    android:layout_gravity="bottom"
    android:layout_alignParentBottom="true"
    android:touchscreenBlocksFocus="false">

    <LinearLayout
        style="@style/Widget.CardContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine|textCapSentences"
            android:maxLength="250"
            android:ems="10"
            android:imeOptions="actionDone"
            android:id="@+id/msg_reply"
            android:hint="@string/msg_reply_hint" />
    </LinearLayout>

</androidx.cardview.widget.CardView>

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/replyCard"
        app:layout_anchorGravity="top|right|end"
        android:layout_margin="8dp"
        android:src="@drawable/ic_send_white_24dp" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

现在msgh_detail_list在.java中称为R.id.detail_recyclerview

<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/detail_recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

最后,.java R.layout.msgh_detail_list_item中的ViewHolder

<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:gravity="center_vertical"
android:orientation="vertical">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="24dp"
    android:orientation="horizontal">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_weight="0.97"
        android:id="@+id/DayMMdd"
        android:ellipsize="end"
        android:maxLines="1"
        android:hint="@string/day_mth_dte" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/arrivaltime"
        android:layout_marginLeft="8dp"
        android:hint="@string/Hour_min" />

    <ImageView
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:id="@+id/avatar"
        android:src="@drawable/status_1"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:longClickable="false" />

</LinearLayout>


<androidx.cardview.widget.CardView
    android:id="@+id/detail_cardview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="#ccecf9">

    <LinearLayout
        style="@style/Widget.CardContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/text2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?attr/textAppearanceListItem"
            android:autoLink="web"
            android:textColorLink="#009933"
            android:layout_weight="0.97"
            android:text="@string/test_msg" />

    </LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

很明显,这里发生了很多事情,坦率地说,我很惊讶它能如此好地工作,或者至少在androidx和API版本29出现之前,它是如此。.

虽然我确实从该应用程序中发现了几行,但是对logcat用户的使用并不多。除了+ 2s860ms来加载显示UI之外,还没有真正了解它们的含义;

...
07-11 14:39:04.170  7758  7758 I Choreographer: Skipped 156 frames!  The application may be doing too much work on its main thread.
...
07-11 14:39:04.353  2810  2834 I ActivityManager: Displayed com.abc.def.pow/com.support.android.pow.DetailActivity: +2s860ms
...

问题:问题在于该应用在300多个记录集上的运行速度非常慢,我对如何解决它有些困惑,有人能帮助我解决这个问题吗??

最新修复!! :@ martin-zeitler在提示后修订了R.layout.msgh_detail,现在看来可以正常工作并且可以立即加载大量数据。

从包含布局(现已丢弃)中删除了整个androidx.core.widget.NestedScrollView块,并替换为androidx.recyclerview.widget.RecyclerView

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_collapseMode="pin" />

</com.google.android.material.appbar.AppBarLayout>

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/detail_recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="70dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/replyCard"
    android:layout_marginBottom="2dp"
    android:layout_marginLeft="@dimen/card_margin"
    android:layout_marginRight="@dimen/card_margin"
    android:layout_gravity="bottom"
    android:layout_alignParentBottom="true"
    android:touchscreenBlocksFocus="false">

    <LinearLayout
        style="@style/Widget.CardContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine|textCapSentences"
            android:maxLength="250"
            android:ems="10"
            android:imeOptions="actionDone"
            android:id="@+id/msg_reply"
            android:hint="@string/msg_reply_hint" />
    </LinearLayout>

</androidx.cardview.widget.CardView>

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/replyCard"
        app:layout_anchorGravity="top|right|end"
        android:layout_margin="8dp"
        android:src="@drawable/ic_send_white_24dp" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

1 个答案:

答案 0 :(得分:1)

也注意到,曾经,它变得相当呆滞...

问题很可能是NestedScrollView> LinearLayout> RecyclerView

include几乎没有用,只包含一个节点。...您必须像这样嵌套它:

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:isScrollContainer="true"
    android:longClickable="false"
    android:measureAllChildren="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        tools:listitem="@layout/cardview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:color="?android:colorControlHighlight"
        android:fastScrollEnabled="true"
        android:gravity="start"
        android:scrollbarStyle="insideInset"
        android:scrollbars="vertical"
        android:splitMotionEvents="false"
        android:verticalScrollbarPosition="right"/>

</androidx.core.widget.NestedScrollView>