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.
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 :
@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 : I 've seen solutions like this one and this but none have helped much.
Thanks in advance