I have a layout with CoordinatorLayout
, AppBarLayout
and RecyclerView
. I am facing scroll issue when add RecyclerView
inside AppBarLayout
, RecyclerView
never scroll while AppBarLayout
is expanded or collapsed
Here is my whole project, Where is the issue?
MainActivity
package com.example.coordinatorlayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView rvItems;
private RecyclerView.LayoutManager layoutManager;
private ItemAdapter itemAdapter;
private ItemModel itemModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rvItems = findViewById(R.id.rv_products);
invalidateView( setListItems());
}
private List<ItemModel> setListItems() {
List<ItemModel> itemsList = new ArrayList<>();
itemModel = new ItemModel();
for(int i = 0; i <= 100; i++) {
itemModel = new ItemModel();
itemModel.setName("Name_" + i);
itemModel.setPrice("10");
itemModel.setStock(i + 2);
itemsList.add(i, itemModel);
}
return itemsList;
}
private void invalidateView(List<ItemModel> items) {
layoutManager = new GridLayoutManager(this, 2);
rvItems.setLayoutManager(layoutManager);
rvItems.setNestedScrollingEnabled(true);
itemAdapter = new ItemAdapter(items);
rvItems.setAdapter(itemAdapter);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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:background="@color/colorAccent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<ImageView
android:id="@+id/iv_logo"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_products"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/round_top_corners_rectangle_gray"
tools:listitem="@layout/item_view" />
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
ItemModel
package com.example.coordinatorlayout;
import android.os.Parcel;
import android.os.Parcelable;
public class ItemModel implements Parcelable {
private String name;
private String price;
private int stock;
public String getName() {
return name;
}
public void setName(String iName) {
this.name = iName;
}
public String getPrice() {
return price;
}
public void setPrice(String iPrice) {
this.price = iPrice;
}
public int getStock() {
return stock;
}
public void setStock(int iStock) {
this.stock = iStock;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
dest.writeString(this.price);
dest.writeInt(this.stock);
}
public ItemModel() {
}
protected ItemModel(Parcel in) {
this.name = in.readString();
this.price = in.readString();
this.stock = in.readInt();
}
public static final Creator<ItemModel> CREATOR = new Creator<ItemModel>() {
@Override
public ItemModel createFromParcel(Parcel source) {
return new ItemModel(source);
}
@Override
public ItemModel[] newArray(int size) {
return new ItemModel[size];
}
};
}
ItemAdapter
package com.example.coordinatorlayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.MyViewHolder> {
private List<ItemModel> itemsList;
public ItemAdapter(List<ItemModel> list) {
this.itemsList = list;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_view, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
ItemModel product = itemsList.get(position);
holder.bindData(product);
}
public void setData(List<ItemModel> list) {
this.itemsList = list;
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return itemsList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView tvQuantity;
TextView tvName;
TextView tvPrice;
MyViewHolder(View view) {
super(view);
tvQuantity = view.findViewById(R.id.tv_quantity);
tvName = view.findViewById(R.id.tv_name);
tvPrice = view.findViewById(R.id.tv_price);
}
void bindData(ItemModel item) {
tvQuantity.setText(String.valueOf(item.getStock()));
tvName.setText(item.getName());
tvPrice.setText(item.getPrice());
}
}
}
item_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/layout_main_item_container_product"
android:layout_width="140dp"
android:layout_height="180dp"
android:layout_margin="5dp"
android:background="@android:color/white">
<TextView
android:id="@+id/tv_quantity_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:paddingStart="15dp"
android:paddingLeft="15dp"
android:text="Quantity"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_toEndOf="@+id/tv_quantity_title"
android:layout_toRightOf="@+id/tv_quantity_title"
tools:text="5" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_quantity_title"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:paddingStart="15dp"
android:paddingLeft="15dp"
android:visibility="visible"
tools:text="Product" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/tv_price_currency"
android:layout_below="@+id/tv_name"
android:layout_centerHorizontal="true"
android:layout_marginStart="8dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="8dp"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_price_currency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dp"
android:paddingStart="15dp"
android:paddingLeft="15dp"
android:paddingEnd="20dp"
android:paddingRight="20dp"
tools:text="$" />
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dp"
android:layout_toEndOf="@+id/tv_price_currency"
android:layout_toRightOf="@+id/tv_price_currency"
android:paddingStart="30dp"
android:paddingLeft="30dp"
tools:text="5" />
round_top_corners_rectangle_gray.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:padding="10dp">
<solid android:color="#F9F9F9" />
<corners
android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="33dp"
android:topRightRadius="33dp" />