2016-07-05 34 views
0

Ich habe 4 CardViews in einer Aktivität und jeder CardView hat einen RecyclerView. Jeder RecyclerView hat etwa 8-10 Zeilen/Artikel. Scrollen dieser Seite/Aktivität ist sehr langsam. Ich vermute ein Problem in meiner RecyclerView.Adapter-Klasse. Kann mir jemand bitte mitteilen, was das Problem in meinem Code sein könnte?Scrolling RecyclerView in CardView ist sehr langsam

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> 
+2

Versuchen Sie, android.support.v4.widget.NestedScrollView anstelle von einfachen ScrollView, –

+0

Da ich nicht mehrere ScrollView verwenden, wird NestedScrollView nicht benötigt, denke ich. Außerdem habe ich recycleView.setHasFixedSize (true) gesetzt; NestedScrollView wird hier möglicherweise nicht benötigt. –

+0

ohh tolle Arbeit ... –

Antwort

1

Das Problem wird durch Einstellung setNestedScrollingEnabled auf false gelöst. recyclerView1.setNestedScrollingEnabled (false);

Es ist besser, NestedScrollView zu verwenden und setNestedScrollingEnabled für eine bessere Leistung auf false festzulegen.