RecyclerView无法顺利滚动

问题描述 投票:0回答:3

我试图实现一个RecyclerViewAdapter,其中包含存储在CardView中的文本和图像列表,但是当我尝试它时,它既不能在模拟器上也不能在真实设备上顺利运行。

适配器类:

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);
    }
}
}

我的主要活动布局:

<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"
        />

我的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);
}

P.S:我看过像this onethis这样的解决方案,但没有一个帮助太多。

提前致谢

java android android-recyclerview recycler-adapter
3个回答
0
投票

尝试将此行添加到您的代码中

 recyclerView.setNestedScrollingEnabled(false);

因为可能是您的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" />

0
投票

如下所示初始化您的适配器。

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);
}

根据您的适配器代码。您的添加和删除方法应如下所示。

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

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

阅读RecyclerView.Adapter更多的视图持有人模式。


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.