在CardView中滚动RecyclerView非常慢

时间:2016-07-05 07:32:51

标签: android android-recyclerview cardview

我在一个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();
    }

}

Activity.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>

1 个答案:

答案 0 :(得分:2)

通过将setNestedScrollingEnabled设置为false可以解决该问题。 的 recyclerView1.setNestedScrollingEnabled(假);

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