1

我在一个 Activity 中有 4 个 CardView,每个 CardView 都有一个 RecyclerView。每个 RecyclerView 大约有 8-10 行/项目。此页面/活动的滚动非常缓慢。我怀疑我的 RecyclerView.Adapter 类中存在一些问题。有人可以让我知道我的代码中可能存在什么问题吗?

适配器:

import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

public class JyotishAppAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<DataModel> dataList;
    private int cardView;
    public static final int CARD_2_COL = 0;
    public static final int CARD_3_COL_HEAD = 1;
    public static final int CARD_2_COL2 = 2;

    public static class GenericViewHolder extends RecyclerView.ViewHolder {

        TextView itemName;
        TextView itemDescription;

        public GenericViewHolder (View itemView) {
            super (itemView);
            this.itemName = (TextView) itemView.findViewById(R.id.item_name);
            this.itemDescription = (TextView) itemView.findViewById(R.id.item_description);
        }
    }

    public static class GenericViewHolder2 extends RecyclerView.ViewHolder {

        TextView itemName;
        TextView itemDescription;
        TextView itemStatus;

        public GenericViewHolder2 (View itemView) {
            super (itemView);
            this.itemName = (TextView) itemView.findViewById(R.id.item_name);
            this.itemDescription = (TextView) itemView.findViewById(R.id.item_description);
            this.itemStatus = (TextView) itemView.findViewById(R.id.item_status);
        }
    }

    public JyotishAppAdapter(ArrayList<DataModel> dataList, int cardView) {
        this.dataList = dataList;
        this.cardView = cardView;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        View v;
        if (cardView == CARD_2_COL) {
            v = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.list_item, viewGroup, false);
            return new GenericViewHolder(v);

        } else if (cardView == CARD_2_COL2) {
            v = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.list_item3, viewGroup, false);
            return new GenericViewHolder(v);
        } else {
            v = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.list_item2, viewGroup, false);
            return new GenericViewHolder2(v);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {

        final DataModel dataModel = dataList.get(position);

        if (viewHolder instanceof GenericViewHolder) {
            GenericViewHolder holder = (GenericViewHolder) viewHolder;
            holder.itemName.setText(Html.fromHtml(dataModel.getValue1()));
            holder.itemDescription.setText(Html.fromHtml(dataModel.getValue2()));

            if (dataModel.getValue4() != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Utility.showAlertMessage(view.getContext(), R.string.message_title,
                                Html.fromHtml(dataModel.getValue4()).toString());
                    }
                });
            }
        } else {
            GenericViewHolder2 holder = (GenericViewHolder2) viewHolder;
            holder.itemName.setText(Html.fromHtml(dataModel.getValue1()));
            holder.itemDescription.setText(Html.fromHtml(dataModel.getValue2()));
            holder.itemStatus.setText(Html.fromHtml(dataModel.getValue3()));

            if (dataModel.getValue4() != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Utility.showAlertMessage(view.getContext(), R.string.message_title,
                                Html.fromHtml(dataModel.getValue4()).toString());
                    }
                });
            }
        }
    }

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

}

活动.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/PageContentBackground"
    tools:context=".PanchangaActivity">

    <include android:id="@+id/tool_bar"
        layout="@layout/tool_bar" />

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tool_bar"
        android:orientation="vertical"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:background="@color/PageContentBackground">

        <TextView android:id="@+id/muhurta_zone"
            style="@style/TextStyle"
            android:textSize="@dimen/text_size_horo_data"
            android:ellipsize="none"
            android:scrollHorizontally="false"
            android:layout_marginBottom="5dp"/>
        <TextView android:id="@+id/muhurta_place"
            style="@style/TextStyle"
            android:textSize="@dimen/text_size_horo_data"
            android:ellipsize="none"
            android:scrollHorizontally="false"
            android:layout_marginBottom="10dp"/>

        <ScrollView android:layout_height="wrap_content"
            android:layout_width="match_parent">
            <RelativeLayout android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <android.support.v7.widget.CardView
                    android:id="@+id/cardview4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    card_view:cardElevation="5dp"
                    card_view:cardCornerRadius="5dp"
                    card_view:cardUseCompatPadding="true"
                    card_view:cardBackgroundColor="@color/CardViewColor">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/panchanga_result_1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/CardViewColor"
                        android:paddingLeft="3dp"
                        android:paddingRight="3dp" />
                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:id="@+id/cardview5"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/cardview4"
                    android:layout_marginTop="10dp"
                    card_view:cardElevation="5dp"
                    card_view:cardCornerRadius="5dp"
                    card_view:cardUseCompatPadding="true"
                    card_view:cardBackgroundColor="@color/CardViewColor">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/panchanga_result_2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/CardViewColor"
                        android:paddingLeft="3dp"
                        android:paddingRight="3dp" />
                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:id="@+id/cardview6"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/cardview5"
                    android:layout_marginTop="10dp"
                    card_view:cardElevation="5dp"
                    card_view:cardCornerRadius="5dp"
                    card_view:cardUseCompatPadding="true"
                    card_view:cardBackgroundColor="@color/CardViewColor">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/panchanga_result_3"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/CardViewColor"
                        android:paddingLeft="3dp"
                        android:paddingRight="3dp" />
                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView
                    android:id="@+id/cardview7"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/cardview6"
                    android:layout_marginTop="10dp"
                    card_view:cardElevation="5dp"
                    card_view:cardCornerRadius="5dp"
                    card_view:cardUseCompatPadding="true"
                    card_view:cardBackgroundColor="@color/CardViewColor">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/panchanga_result_4"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/CardViewColor"
                        android:paddingLeft="3dp"
                        android:paddingRight="3dp" />
                </android.support.v7.widget.CardView>

            </RelativeLayout>
        </ScrollView>
    </LinearLayout>
</RelativeLayout>
4

1 回答 1

3

该问题通过将 setNestedScrollingEnabled 设置为 false 来解决。 recyclerView1.setNestedScrollingEnabled(false);

最好使用 NestedScrollView 并将 setNestedScrollingEnabled 设置为 false 以获得更好的性能。

于 2016-07-12T07:35:51.910 回答