简体   繁体   English

RecyclerView滚动不流畅

[英]RecyclerView isn't scrolling smoothly

I've tried to implement a RecyclerViewAdapter which contains a list of text and image stored in a CardView , however when I try it, it isn't running smoothly neither on the emulator nor on a real device. 我试图实现一个RecyclerViewAdapter ,其中包含存储在CardView中的文本和图像列表,但是当我尝试它时,它既不能在仿真器上也不能在真实设备上流畅运行。

The adapter class : 适配器类

public class MainRecyclerAdapter extends RecyclerView.Adapter<MainRecyclerAdapter.MainRecyclerViewHolder>{
Context context;
ArrayList<MainItem> itemsList;
LayoutInflater inflater;


public MainRecyclerAdapter(Context context) {
    this.context = context;
}

public MainRecyclerAdapter(Context context, ArrayList<MainItem> itemsList) {
    this.context = context;
    this.itemsList = itemsList;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public void add(MainItem item){
    itemsList.add(item);
    notifyDataSetChanged();
}

public void addAll(Collection<MainItem> itemCollection){
    for (MainItem item : itemCollection) itemsList.add(item);
    notifyDataSetChanged();
}

public void removeItem(int position){
    itemsList.remove(position);
    notifyDataSetChanged();
}

public void clear(){
    itemsList.clear();
    notifyDataSetChanged();
}

@Override
public MainRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new MainRecyclerViewHolder(inflater.inflate(R.layout.main_menu_item, parent, false));
}

@Override
public void onBindViewHolder(MainRecyclerViewHolder holder, int position) {

    YoYo.with(Techniques.ZoomIn).playOn(holder.cardView);
    MainItem item = itemsList.get(position);

    holder.textView.setText(item.getText());
    Picasso.with(context)
            .load(item.getImageResourceID())
            .into(holder.imageView);

    if (item.getColor() != null)
        holder.cardView.setCardBackgroundColor(item.getColor());
}

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

public class MainRecyclerViewHolder extends RecyclerView.ViewHolder{

    CardView cardView;
    TextView textView;
    ImageView imageView;

    public MainRecyclerViewHolder(View itemView) {
        super(itemView);
        cardView = itemView.findViewById(R.id.mainItemCardView);
        textView = itemView.findViewById(R.id.mainCardTextView);
        imageView = itemView.findViewById(R.id.mainCardImageView);
    }
}
}

My main activity layout : 我的主要活动布局:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/main_wallpaper"
tools:context="com.r3tr0.weekplanner.MainActivity">

    <android.support.v7.widget.CardView
        android:id="@+id/headMainCardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        app:cardCornerRadius="20dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="12dp"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:layout_marginTop="16dp"
                android:gravity="center"
                android:text="Welcome to the week planner app"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold|italic" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:text="Let's start by making your life a little more organized!"
                android:textSize="18sp" />
        </LinearLayout>

    </android.support.v7.widget.CardView>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/mainRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="16dp"
        />

My OnCreate method : 我的OnCreate方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Palette palette = Palette.from(BitmapFactory.decodeResource(getResources(), R.drawable.main_wallpaper)).generate();
    ((CardView) findViewById(R.id.headMainCardView)).setCardBackgroundColor(palette.getDominantColor(Color.parseColor("#FFFFFF")));
    initializeRecyclerView();
}

void initializeRecyclerView(){
    RecyclerView recyclerView = findViewById(R.id.mainRecyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setItemViewCacheSize(20);
    recyclerView.setDrawingCacheEnabled(true);
    recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    adapter = new MainRecyclerAdapter(this, new ArrayList<MainItem>());
    adapter.add(new MainItem("Coming soon", R.drawable.soon_transparent, Color.parseColor("#91677D")));
    adapter.add(new MainItem("Week's plan", R.drawable.schedule_transparent, Color.parseColor("#95A6C4")));
    adapter.add(new MainItem("My vault", R.drawable.vault_transparent, Color.parseColor("#CCD9E9")));
    adapter.add(new MainItem("My achievements", R.drawable.soon_transparent, Color.parseColor("#E65D6D")));
    adapter.add(new MainItem("Coming soon!", R.drawable.soon_transparent, Color.parseColor("#FC9D94")));
    adapter.add(new MainItem("Exit", R.drawable.x_transparent, Color.parseColor("#BDCCD3")));

    recyclerView.setLayoutManager(new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false));
    recyclerView.setAdapter(adapter);
}

PS : I 've seen solutions like this one and this but none have helped much. PS:我已经喜欢看到解决这个这个 ,但没有帮助很大。

Thanks in advance 提前致谢

Initialize your adapter like below. 如下所示初始化适配器。

void initializeRecyclerView(){
    RecyclerView recyclerView = findViewById(R.id.mainRecyclerView);
    recyclerView.setLayoutManager(new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false));
    recyclerView.setHasFixedSize(true);
    ArrayList list=new ArrayList<MainItem>();
    list.add(new MainItem("Coming soon", R.drawable.soon_transparent, Color.parseColor("#91677D")));
    list.add(new MainItem("Week's plan", R.drawable.schedule_transparent, Color.parseColor("#95A6C4")));
    list.add(new MainItem("My vault", R.drawable.vault_transparent, Color.parseColor("#CCD9E9")));
    list.add(new MainItem("My achievements", R.drawable.soon_transparent, Color.parseColor("#E65D6D")));
    list.add(new MainItem("Coming soon!", R.drawable.soon_transparent, Color.parseColor("#FC9D94")));
    list.add(new MainItem("Exit", R.drawable.x_transparent, Color.parseColor("#BDCCD3")));
    adapter = new MainRecyclerAdapter(this, list);
    recyclerView.setAdapter(adapter);
}

As per Your adapter's code . 按照您的适配器的代码。 Your add and remove method should be as below . 您的添加和删除方法应如下所示。

public void add(MainItem item){
    itemsList.add(item);
    notifyItemInserted(itemsList.size()-1);
  }

public void removeItem(int position){
itemsList.remove(position);
notifyItemRemoved(position);
}

Read RecyclerView.Adapter to more of its view holder pattern. 阅读RecyclerView.Adapter以获得更多视图持有者模式。

Try adding this line to your code 尝试将此行添加到您的代码中

 recyclerView.setNestedScrollingEnabled(false);

Because may be your recyclerView will be instead NestedScrollView 因为可能是您的recyclerView将改为NestedScrollView

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycle2"
    android:nestedScrollingEnabled="false"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

现在,这很有趣...我尝试使用Picasso库而不是Glide ,并将图像的分辨率降低到256X256,现在RecyclerView运行顺利,如果有人需要,我将使用新代码更新适配器类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM