Why Recyclerview doesn't scroll inside AppBarLayout - android

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

Put RecyclerView outside of AppBarLayout.
<?xml version="1.0" encoding="utf-8"?>
<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:background="#color/colorAccent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/layout_sticky_header_store_details"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:text="Name" />
</RelativeLayout>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="#+id/layout_header"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:fitsSystemWindows="true">
<RelativeLayout
android:id="#+id/layout_scrollable_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#201A98E5"
android:paddingStart="10dp"
android:paddingLeft="10dp"
android:paddingEnd="20dp"
android:paddingRight="20dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<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" />
<TextView
android:id="#+id/tv_name_label_scrollable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/iv_logo"
android:layout_toLeftOf="#+id/iv_logo"
android:text="Name: "
android:textStyle="bold" />
<TextView
android:id="#+id/tv_name_scrollable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_toStartOf="#+id/tv_name_label_scrollable"
android:layout_toLeftOf="#+id/tv_name_label_scrollable"
android:paddingBottom="5dp"
android:text="Name" />
<TextView
android:id="#+id/tv_quantity_label_scrollable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_name_label_scrollable"
android:layout_toStartOf="#+id/iv_logo"
android:layout_toLeftOf="#+id/iv_logo"
android:text="Quantity: "
android:textStyle="bold" />
<TextView
android:id="#+id/tv_quantity_scrollable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_name_label_scrollable"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_toStartOf="#+id/tv_quantity_label_scrollable"
android:layout_toLeftOf="#+id/tv_quantity_label_scrollable"
android:text="22" />
<TextView
android:id="#+id/tv_call_label_scrollable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_quantity_label_scrollable"
android:layout_marginBottom="10dp"
android:layout_toStartOf="#+id/iv_logo"
android:layout_toLeftOf="#+id/iv_logo"
android:text="Call us"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_call_scrollable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_quantity_label_scrollable"
android:layout_centerHorizontal="true"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_toStartOf="#+id/tv_call_label_scrollable"
android:layout_toLeftOf="#+id/tv_call_label_scrollable"
android:text="0123456789" />
<TextView
android:id="#+id/tv_description_scrollable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_call_label_scrollable"
android:layout_toStartOf="#+id/iv_logo"
android:layout_toLeftOf="#+id/iv_logo"
android:maxLines="2"
android:paddingBottom="16dp"
android:scrollbars="vertical"
android:text="Description description description description description description description description description description description" />
</RelativeLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_products"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/round_top_corners_rectangle_gray"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:listitem="#layout/item_view" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>

UPDATE: You need to use the RecyclerView inside the Coordinatorlayout and outside of the AppBarLayout:
<?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">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="250dp"
android:fitsSystemWindows="true"
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:src="#mipmap/ic_launcher" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_products"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:listitem="#layout/prof_item" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
The first layout you've added to the question has many issues. First, CoordintorLayout should be the root of the layout.
Second, if you're trying to add some content at the top of the layout, you'll need to use the RelativeLayout inside the AppBarLayout or using CollapsingToolbarLayout inside the AppBarLayout.
Another problem is that you've added:
app:layout_behavior="#string/appbar_scrolling_view_behavior"
To the RecyclerView which is only used outside of AppBarLayout to notify the AppBarLayout when scroll events occur on this particular view. So remove this attribute.

Related

Constraintlayout does not behave well as recyclerview 's item

I have a recyclerview whose items look like this:
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_food_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="4dp"
app:cardCornerRadius="4dp">
<LinearLayout
android:id="#+id/linear_food_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start"
android:orientation="horizontal"
android:padding="4dp">
<com.mikhaellopez.circleview.CircleView
android:id="#+id/food_image"
android:layout_width="80dp"
android:layout_height="80dp"
app:cv_color="#color/colorAccent" />
<LinearLayout
android:id="#+id/food_contents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:orientation="vertical">
<TextView
android:id="#+id/food_title_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:fontFamily="#font/sans_regular"
android:text="عنوان غذا"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:id="#+id/ingredients_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:fontFamily="#font/sans_regular"
android:text="مواد اولیه"
android:textAppearance="#style/TextAppearance.MaterialComponents.Subtitle1" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
I'm trying to convert this into a flat hierarchy using Constraintlayout.
Here's how it looks:
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_food_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="4dp"
app:cardCornerRadius="4dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/linear_food_item"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/food_title_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:text="عنوان غذا"
android:textAppearance="#style/TextAppearance.AppCompat.Title"
app:layout_constraintStart_toEndOf="#+id/food_image"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/ingredients_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="مواد اولیه"
android:textAppearance="#style/TextAppearance.MaterialComponents.Subtitle1"
app:layout_constraintStart_toEndOf="#+id/food_image"
app:layout_constraintTop_toBottomOf="#+id/food_title_homepage" />
<com.mikhaellopez.circleview.CircleView
android:id="#+id/food_image"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
app:cv_color="#color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Here's where my Recyclerview is inserted:
<androidx.constraintlayout.widget.ConstraintLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main.homepage.HomepageFragment">
<ProgressBar
android:id="#+id/homepage_progressbar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recipes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scrollbars="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/homepage_toolbar"
tools:listitem="#layout/fragment_homepage_food_item" />
<include
android:id="#+id/homepage_toolbar"
layout="#layout/fragment_homepage_toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here's how each item should be: https://i.stack.imgur.com/AcfAm.png
But I don't get the same results in the Recyclerview: https://i.stack.imgur.com/jpUG4.png
I didn't have the problem before I converted the view into Constraintlayout!! Everything was working just fine until now that I converted them. They all become left-aligned in the Recyclerview!!
Does anyone know what the problem is?? Thanks in advance
-- Updated --
How it looks like on an android emulator or any other devices: https://i.stack.imgur.com/tvGz4.png
When I scroll down to the bottom (I update the list after each scroll) some items become right-aligned (they somehow get fixed) but the rest stays the same (still left aligned)
-- Java Code --
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
viewModel = new ViewModelProvider(this).get(HomepageViewModel.class);
getData(0);
setRecyclerView(linearLayoutManager);
EndlessRecyclerViewScrollListener scrollListener = new EndlessRecyclerViewScrollListener(linearLayoutManager) {
#Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
getData(page);
}
};
recyclerView.addOnScrollListener(scrollListener);
private void getData(int offset) {
viewModel.init(this, offset);
viewModel.getFoodsLiveData().observe(getViewLifecycleOwner(), foodList -> {
if (foodList != null) {
foods.addAll(foodList);
foodList.clear();
foodAdapter.notifyDataSetChanged();
}
});
}
private void setRecyclerView(LinearLayoutManager linearLayoutManager) {
foodAdapter = new FoodAdapter(foods);
recyclerView.setAdapter(foodAdapter);
recyclerView.setLayoutManager(linearLayoutManager);
}
My adapter:
private void getData(int offset) {
viewModel.init(this, offset);
viewModel.getFoodsLiveData().observe(getViewLifecycleOwner(), foodList -> {
if (foodList != null) {
foods.addAll(foodList);
foodList.clear();
foodAdapter.notifyDataSetChanged();
}
});
}
private void setRecyclerView(LinearLayoutManager linearLayoutManager) {
foodAdapter = new FoodAdapter(foods);
recyclerView.setAdapter(foodAdapter);
recyclerView.setLayoutManager(linearLayoutManager);
}
My adapter:
private List<Food> foods;
FoodAdapter(List<Food> foods) {
this.foods = foods;
}
static class FoodViewHolder extends RecyclerView.ViewHolder {
CircleView foodImage;
TextView foodTitle, ingredients;
CardView foodItem;
FoodViewHolder(View itemView) {
super(itemView);
foodImage = itemView.findViewById(R.id.food_image);
foodTitle = itemView.findViewById(R.id.food_title_homepage);
ingredients = itemView.findViewById(R.id.ingredients_homepage);
foodItem = itemView.findViewById(R.id.card_food_item);
}
}
#NonNull
#Override
public FoodViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_homepage_food_item, parent, false);
return new FoodViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull FoodViewHolder holder, int position) {
final Food currentFood = foods.get(position);
StringBuilder food_ingredient_str = new StringBuilder();
int ingredient_position = 0;
int ingredients_size = currentFood.getFood_ingredients().size();
for (Food.FoodIngredient foodIngredient : currentFood.getFood_ingredients()) {
food_ingredient_str.append(foodIngredient.getIngredient());
if (ingredient_position != ingredients_size / 3)
food_ingredient_str.append(", ");
if (ingredient_position == ingredients_size / 3) {
food_ingredient_str.append(" و ...");
break;
}
ingredient_position += 1;
}
holder.foodTitle.setText(currentFood.getName());
holder.ingredients.setText(food_ingredient_str);
holder.foodItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putSerializable("current_food", currentFood);
Navigation.findNavController(v).navigate(R.id.action_navigation_home_to_navigation_food, bundle);
}
});
}
#Override
public int getItemCount() {
return foods.size();
}
If you want to create a list using recyclerview and each item to be right aligned as in Arabic.. then try these approaches
replace your "recycle_view_item_layout.xml" with this
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_food_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="4dp"
app:cardCornerRadius="4dp">
<LinearLayout
android:id="#+id/linear_food_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="4dp">
<LinearLayout
android:id="#+id/food_contents"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/food_title_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:ellipsize="start"
android:fontFamily="#font/sans_regular"
android:singleLine="true"
android:text="عنوان غذا"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:id="#+id/ingredients_homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:ellipsize="start"
android:fontFamily="#font/sans_regular"
android:singleLine="true"
android:text="مواد اولیه"
android:textAppearance="#style/TextAppearance.MaterialComponents.Subtitle1" />
</LinearLayout>
<com.mikhaellopez.circleview.CircleView
android:id="#+id/food_image"
android:layout_width="80dp"
android:layout_height="80dp"
app:cv_color="#color/colorAccent" />
</LinearLayout>
</androidx.cardview.widget.CardView>
replace your "recycle view" with this
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recipes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#id/homepage_toolbar"
tools:listitem="#layout/fragment_homepage_food_item" />
and attach a layout manager like this (important)
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
setRecyclerView(layoutManager);
and rest of your code seems okay

CardStackView is not working with Scrollview?

I am using CardStackView library but I also want to scroll its item and the only swap left and right
public class CardFragment extends Fragment implements CardStackListener {
private View view;
FragmentCardBinding binding;
CardStackLayoutManager manager;
CardStackAdapter cardStackAdapter;
private List<CardModel> listCard = new ArrayList<CardModel>();
public static MyAppAdapter myAppAdapter;
public static ViewHolder viewHolder;
private ArrayList<Data> array;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(
inflater, R.layout.fragment_card, container, false);
manager = new CardStackLayoutManager(getContext());
listCard.add(new CardModel("MaryBurgess", "steattle, USA", "USA", R.drawable.imggirl));
listCard.add(new CardModel("MaryBurgess", "steattle, USA", "USA", R.drawable.imggirl));
listCard.add(new CardModel("MaryBurgess", "steattle, USA", "USA", R.drawable.imggirl));
listCard.add(new CardModel("MaryBurgess", "steattle, USA", "USA", R.drawable.imggirl));
manager.setStackFrom(StackFrom.None);
manager.setVisibleCount(3);
manager.setTranslationInterval(8.0f);
manager.setScaleInterval(0.95f);
manager.setSwipeThreshold(0.3f);
manager.setMaxDegree(20.0f);
manager.setDirections(Direction.HORIZONTAL);
manager.setCanScrollHorizontal(true);
manager.setCanScrollVertical(true);
binding.cardStackView.setLayoutManager(manager);
cardStackAdapter = new CardStackAdapter(listCard, getActivity());
binding.cardStackView.setAdapter(cardStackAdapter);
binding.imgFilter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), LookingFor.class);
startActivity(intent);
getActivity().finish();
}
});
myAppAdapter = new MyAppAdapter(listCard, getContext());
return view = binding.getRoot();
}
#Override
public void onCardDragging(Direction direction, float ratio) {
}
#Override
public void onCardSwiped(Direction direction) {
Log.d("CardStackView", String.valueOf(direction));
}
#Override
public void onCardRewound() {
}
#Override
public void onCardCanceled() {
}
here is adapter class
public class CardStackAdapter extends RecyclerView.Adapter<CardStackAdapter.ViewHolder> {
private List<CardModel> listCard = new ArrayList<CardModel>();
private Context c;
public CardStackAdapter(List<CardModel> listCard,Context c ) {
this.listCard = listCard;
this.c = c;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.username.setText(listCard.get(position).getName());
holder.cityName.setText(listCard.get(position).getCity());
Glide.with(holder.item_image)
.load(listCard.get(position).getUserImage())
.into(holder.item_image);
}
#Override
public int getItemCount() {
return listCard.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView username, cityName;
RoundedImageView item_image;
public ViewHolder(#NonNull View itemView) {
super(itemView);
username = itemView.findViewById(R.id.user_name);
cityName = itemView.findViewById(R.id.city_name);
item_image = itemView.findViewById(R.id.item_image);
}
}
}
and adapter XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
android:foreground="?attr/selectableItemBackground"
app:cardBackgroundColor="#android:color/white"
app:cardCornerRadius="8dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true">
<com.makeramen.roundedimageview.RoundedImageView
android:id="#+id/item_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/imggirl"
app:riv_corner_radius="8dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="20dp"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.bugfreecode.anotherchanz.fonts.GothamBookBold
android:id="#+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MaryBurgess"
android:textColor="#android:color/white"
android:textSize="26sp"
android:textStyle="bold" />
<ImageView
android:layout_width="18dp"
android:layout_height="18dp"
android:src="#drawable/verified" />
</LinearLayout>
<TextView
android:id="#+id/city_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Steattle, USA"
android:textColor="#android:color/white"
android:textSize="16dp"
android:textStyle="bold" />
</LinearLayout>
<FrameLayout
android:id="#+id/left_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:src="#drawable/dislike" />
<FrameLayout
android:id="#+id/bottom_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</FrameLayout>
<FrameLayout
android:id="#+id/right_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:src="#drawable/like" />
</FrameLayout>
<FrameLayout
android:id="#+id/top_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:id="#+id/lytChips"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:layout_marginRight="15dp"
android:orientation="vertical">
<com.google.android.material.chip.ChipGroup
android:id="#+id/choice_chip_group"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.chip.Chip
card_view:iconStartPadding="10dp"
card_view:chipIconSize="15dp"
android:drawablePadding="5dp"
android:id="#+id/choice_chip5"
android:textColor="#color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Architecture/Interior design"
android:textAppearance="#style/AppTheme.GenderChip"
android:theme="#style/Theme.MaterialComponents.Light"
app:chipStartPadding="8dp"
app:chipStrokeWidth="1dp" />
</com.google.android.material.chip.ChipGroup>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
fragment XML
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/gradiant_back">
<com.bugfreecode.anotherchanz.fonts.GothamBookBold
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="30dp"
android:gravity="center"
android:letterSpacing="0.23"
android:singleLine="true"
android:text="Anotherchanz"
android:textAllCaps="true"
android:textColor="#color/white"
android:textSize="23sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/imgFilter"
android:layout_marginEnd="20dp"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:src="#drawable/ic_filler" />
</LinearLayout>
<LinearLayout
android:background="#drawable/back_full"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- <com.lorentzos.flingswipe.SwipeFlingAdapterView
android:id="#+id/flingContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradiant_back"
app:rotation_degrees="15.5" />-->
<com.yuyakaido.android.cardstackview.CardStackView
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"
android:id="#+id/card_stack_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.yuyakaido.android.cardstackview.CardStackView>
</LinearLayout>
</LinearLayout>
Card swap is working perfectly but I want to make card scroll vertically also I have used scroll view in adapter items but it is not working its only working for swap, please help me I would appreciate every answer.

Android TableLayout - add buttons dynamically at center of screen

Please see below screenshot for different results on tablet and on phones.
activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="#+id/frameLayout1"
android:weightSum="4"
android:padding="0dp"
android:orientation="vertical">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:src="#drawable/crossword_background"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:orientation="vertical"
android:id="#+id/frameLayout"
android:weightSum="4"
>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#android:color/transparent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvTitle"
style="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="100dp"
android:layout_height="24dp"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="4dp"
android:background="#drawable/count_frame"
android:fontFamily="#font/roboto"
android:gravity="center"
android:textColor="#5A0FC8"
android:textSize="13sp"
android:textStyle="bold"
tools:text="4" />
<TextView
android:id="#+id/menu_item_score"
style="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="100dp"
android:layout_height="24dp"
android:layout_margin="4dp"
android:background="#drawable/count_frame"
android:gravity="right"
android:layout_alignParentRight="true"
android:textColor="#5A0FC8"
android:textSize="13sp"
android:textStyle="bold"
android:textAlignment="center"
tools:text="4" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
<LinearLayout
android:id="#+id/rightLayout"
android:layout_width="40dp"
android:layout_height="fill_parent"
android:layout_alignParentEnd="true"
android:layout_below="#id/horizontal_divider"
android:orientation="vertical" >
<LinearLayout
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="match_parent"
android:weightSum="4"
android:orientation="vertical" >
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_action_360"
android:background="#null"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_action_360"
android:background="#null"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_action_360"
android:background="#null"/>
<ImageButton
android:id="#+id/ShowHint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_action_360"
android:background="#null"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/leftLayout"
android:layout_width="40dp"
android:layout_height="fill_parent"
android:layout_alignParentStart="true"
android:layout_below="#id/horizontal_divider"
android:orientation="vertical" >
<LinearLayout
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="match_parent"
android:weightSum="4"
android:orientation="vertical" >
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_action_360"
android:background="#null"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_action_360"
android:background="#null"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_action_360"
android:background="#null"/>
<ImageButton
android:id="#+id/ShuffleButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_action_360"
android:background="#null"/>
</LinearLayout>
</LinearLayout>
<View
android:id="#+id/horizontal_divider"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_centerVertical="true" />
<RelativeLayout
android:id="#+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:layout_centerInParent="true"
android:layout_margin="0dp"
android:layout_above="#+id/horizontal_divider"
android:layout_below="#id/toolbar"
android:gravity="center">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tblLayout"
android:gravity="center"
android:padding="0dip"
android:shrinkColumns="*"
android:layout_margin="0dip"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
</TableLayout>
</RelativeLayout >
<LinearLayout
android:id="#+id/secondLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/horizontal_divider"
android:layout_toStartOf="#id/rightLayout"
android:layout_toEndOf="#id/leftLayout"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
>
</FrameLayout>
</LinearLayout>
</RelativeLayout>
background_ic_btn_bonus.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/ic_btn_bonus"
android:tileMode="disabled" android:gravity="center" >
</bitmap>
background_ic_btn_default.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/ic_btn_default"
android:tileMode="disabled" android:gravity="center" >
</bitmap>
Corresponding Java Code
String[] iLayoutMap = getTableLayout();
/*
iLayoutMap contains split rows removing 2
2,0,0,0,0,0,0,2,0,0,0,0,0,0,2,0,0,1,1,1,0,2,0,0,0,1,0,0,2,0,0,0,0,0,0,2,0,0,0,0,0,0*/
TableLayout tableLayout = (TableLayout) findViewById(R.id.tblLayout);
for(int i=1;i<iLayoutMap.length;i++){
TableRow NewRow1 =new TableRow(this);
NewRow1.setPadding(0, 0, 0, 0);
NewRow1.setGravity(Gravity.CENTER);
NewRow1.setHorizontalGravity(Gravity.CENTER);
NewRow1.setVerticalGravity(Gravity.CENTER);
String [] items = iLayoutMap[i].split("\\s*,\\s*");
NewRow1.setWeightSum(items.length-1);
for(int j = 0; j < items.length;j++) {
if(items[j].equalsIgnoreCase("0")){
Button btnAdd = new Button(context);
btnAdd.setGravity(Gravity.CENTER);
btnAdd.setTextSize(25);
btnAdd.setTextColor(Color.WHITE);
//btnAdd.setPadding(10 ,10,10,10);
//btnAdd.setBackgroundResource(R.drawable.ic_btn_default);
btnAdd.setBackgroundResource(R.drawable.background_ic_btn_default);
btnAdd.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT ,1));
NewRow1.addView(btnAdd);
}else if(items[j].equalsIgnoreCase("1")){
//https://stackoverflow.com/questions/3404582/adding-text-to-imageview-in-android
Button btnAdd = new Button(context);
String strId = Integer.toString(i) + Integer.toString(j-1);
//btnAdd.setWidth(iObjectWidth);
//btnAdd.setHeight(iObjectHeight);
btnAdd.setId(Integer.valueOf(strId));
btnAdd.setGravity(Gravity.CENTER);
btnAdd.setTextSize(25);
btnAdd.setTextColor(Color.WHITE);
//btnAdd.setPadding(10 ,10,10,10);
//btnAdd.setBackgroundResource(R.drawable.ic_btn_default);
btnAdd.setBackgroundResource(R.drawable.background_ic_btn_bonus);
btnAdd.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT,1 ));
NewRow1.addView(btnAdd);
}
}
NewRow1.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT,items.length-1));
tableLayout.addView(NewRow1);
}
Function
public String[] getTableLayout() {
if (lvlinfo != null) {
return lvlinfo.getLayout().split("2");
}
//Return this as default in case of failure
String arr = "2,0,0,0,0,0,0,2,0,0,0,0,0,0,2,0,0,1,1,1,0,2,0,0,0,1,0,0,2,0,0,0,0,0,0,2,0,0,0,0,0,0";
//String arr = "2,0,0,0,0,0,0,2,0,0,0,0,0,0,2,0,0,1,1,1,0,2,0,0,0,1,0,0,2,0,0,0,0,0,0,2,0,0,0,0,0,0";
return arr.split("2");
}
Expected Result ( Working correctly on Tablet with Android 4.4.2 with below code)
Actual Results ( Tested on multiple phones with OS >= 8.1.0)
I Have tried your code but unable to reproduce this output
For me your code is working fine check the output
Result in Oreo
Result in android pie
Result in android Q
as per my opinion you should use
You should use RecyclerView with GridLayoutManager
Follow this sample code
Add one recyclerview in your activity layout xml file
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:gravity="center">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="wrap_content"
android:id="#+id/gridRecyclerView"
android:layout_centerInParent="true"
android:layout_height="wrap_content"/>
</RelativeLayout>
Now inside your activity set GridLayoutManager to your RecyclerView
GridLayoutManager (Context context,
int spanCount)
Creates a vertical GridLayoutManager
Parameters
context Context: Current context, will be used to access resources.
spanCount int: The number of columns in the grid
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class JavaActivity extends AppCompatActivity {
RecyclerView myRecyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_java);
myRecyclerView = findViewById(R.id.gridRecyclerView);
GridLayoutManager gridLayoutManager = new GridLayoutManager(JavaActivity.this, 6);
myRecyclerView.setLayoutManager(gridLayoutManager);
myRecyclerView.setAdapter(new MyAdapter(this));
}
}
Create one adapter class RecyclerView like this
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context context;
public MyAdapter(Context context) {
this.context = context;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.row_list_item, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
if (position%2==0){
holder.imgBanner.setBackgroundColor(Color.BLUE);
}else {
holder.imgBanner.setBackgroundColor(Color.GREEN);
holder.imgBanner.setImageResource(R.drawable.red_heart);
}
}
#Override
public int getItemCount() {
return 36;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
ImageView imgBanner;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
imgBanner = itemView.findViewById(R.id.imgBanner);
}
}
}
Create one layout file for RecyclerView item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="2dp"
android:orientation="vertical">
<ImageView
android:id="#+id/imgBanner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
/>
</RelativeLayout>
Try with a padding like this:
<RelativeLayout
android:id="#+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="someDp" ->>>> Padding
android:paddingRight="someDp" ->>>> Padding
android:layout_above="#+id/horizontal_divider"
android:layout_below="#id/toolbar"
android:gravity="center">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tblLayout"
android:gravity="center"
android:shrinkColumns="*"
android:padding="0dip"
android:layout_margin="0dip"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"></TableLayout>
</RelativeLayout >

RecyclerView not showing up properly (scrunched up)

I've been trying to implement multiple RecyclerView within a layout that is part of a collapsing tab. However, my RecyclerView hasn't been working and I don't know what I did wrong in my code. Please help me!
Here is the Github Link: github.com/arxbombus/RecipeDetails
Here is the desired view: image
Here is what I'm getting for some reason: image
As you can see, everything is all scrunched up. :(
Below I've included the layouts for my MainActivity and also the Java files for my Adapters.
Here is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="400dp"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsingToolbarLayout"
android:layout_width="match_parent"
android:layout_height="400dp"
app:title="Some Randome Recipe"
app:titleEnabled="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="#+id/ivParallax"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/food"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
<android.support.v7.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"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="#+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtAbout"
android:text="About this recipe"
android:textStyle="bold"
android:textSize="13sp"
android:padding="15dp"
android:layout_marginBottom="-25dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtRecipeDescription"
android:text="#string/recipe_description"
android:padding="15dp"
android:layout_marginBottom="-5dp"
android:textSize="12sp"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorDivider"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtInfo"
android:text="Info"
android:textStyle="bold"
android:textSize="13sp"
android:padding="15dp"
android:layout_marginBottom="-20dp"
android:layout_marginTop="-5dp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvRecipeInfo"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_gravity="center_vertical"
android:orientation="horizontal"
android:padding="15dp"></android.support.v7.widget.RecyclerView>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorDivider"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtIngredient"
android:text="Ingredients"
android:textStyle="bold"
android:textSize="13sp"
android:padding="15dp"
android:layout_marginBottom="-20dp"
android:layout_marginTop="-5dp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvRecipeIngredient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="15dp"></android.support.v7.widget.RecyclerView>
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorDivider"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
walking
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtProcedures"
android:text="Procedures"
android:textStyle="bold"
android:textSize="13sp"
android:padding="15dp"
android:layout_marginBottom="-20dp"
android:layout_marginTop="-5dp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvRecipeProcedure"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="15dp"></android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
And my CardView
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recipeInfoCards"
android:layout_width="81dp"
android:layout_height="75dp"
app:cardCornerRadius="6dp"
android:elevation="15dp"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorRecipeInfoCardBG"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/txtRecipeInfoCardTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cooking Time:"
android:textSize="11sp"
android:textColor="#android:color/black"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_marginTop="5dp"
/>
<TextView
android:id="#+id/txtRecipeInfoCardDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="20 Minutes"
android:textSize="11sp"
android:textColor="#color/colorTextSecondary"
android:layout_gravity="center_horizontal"
android:gravity="center"/>
</LinearLayout>
</android.support.v7.widget.CardView>
And here are my MainActivity and Adapters respectiviely
public class MainActivity extends AppCompatActivity {
ArrayList < Recipe > recipeData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
recipeData = new ArrayList < Recipe > ();
createData();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
RecyclerView recipeInfoCardRV = (RecyclerView) findViewById(R.id.rvRecipeInfo);
recipeInfoCardRV.setHasFixedSize(true);
recipeInfoCardRV.setNestedScrollingEnabled(false);
RecipeInfoAdapter recipeInfoAdapter = new RecipeInfoAdapter(this, recipeData);
recipeInfoCardRV.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
recipeInfoCardRV.setAdapter(recipeInfoAdapter);
RecyclerView recipeIngredientRV = (RecyclerView) findViewById(R.id.rvRecipeIngredient);
recipeIngredientRV.setHasFixedSize(true);
recipeIngredientRV.setNestedScrollingEnabled(false);
recipeIngredientRV.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
RecipeIngredientAdapter recipeIngredientAdapter = new RecipeIngredientAdapter(this, recipeData);
recipeIngredientRV.setAdapter(recipeIngredientAdapter);
}
public void createData() {
ArrayList < RecipeInfoCard > recipeInfoCards = new ArrayList < RecipeInfoCard > ();
recipeInfoCards.add(new RecipeInfoCard("Cooking Time", "20 Minutes"));
recipeInfoCards.add(new RecipeInfoCard("Calories", "3501"));
recipeInfoCards.add(new RecipeInfoCard("Procedures", "Three"));
ArrayList < RecipeIngredient > recipeIngredients = new ArrayList < RecipeIngredient > ();
for (int i = 1; i <= 10; i++) {
recipeIngredients.add(new RecipeIngredient("Ingredient " + i, String.valueOf(i), "grams"));
}
Recipe dm = new Recipe(recipeInfoCards, recipeIngredients);
recipeData.add(dm);
}
}
My adapter for my CardView
public class RecipeInfoAdapter extends RecyclerView.Adapter < RecipeInfoAdapter.RecipeInfoCardItemRowHolder > {
private Context mContext;
private ArrayList < Recipe > recipeData;
public RecipeInfoAdapter(Context mContext, ArrayList < Recipe > recipeData) {
this.mContext = mContext;
this.recipeData = recipeData;
}
#Override
public RecipeInfoCardItemRowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recipe_info_card_view, null);
return new RecipeInfoCardItemRowHolder(v);
}
#Override
public void onBindViewHolder(RecipeInfoCardItemRowHolder recipeInfoCardItemRowHolder, int position) {
Recipe recipe = recipeData.get(position);
recipeInfoCardItemRowHolder.infoCardTitle.setText(recipe.getRecipeInfoCards().get(position).getRecipeInfoCardTitle());
recipeInfoCardItemRowHolder.infoCardDescription.setText(recipe.getRecipeInfoCards().get(position).getRecipeInfoCardDescription());
}
#Override
public int getItemCount() {
return (null != recipeData ? recipeData.size() : 0);
}
public class RecipeInfoCardItemRowHolder extends RecyclerView.ViewHolder {
protected TextView infoCardTitle;
protected TextView infoCardDescription;
public RecipeInfoCardItemRowHolder(View view) {
super(view);
this.infoCardTitle = (TextView) view.findViewById(R.id.txtRecipeInfoCardTitle);
this.infoCardDescription = (TextView) view.findViewById(R.id.txtRecipeInfoCardDescription);
}
}
}
I didn't put all my code here because I think the question is long enough but I would really appreciate if someone helped me. Thank you!
Try to change your rvRecipeInfo RecyclerView height.. becasue your hardicoading it to 75 dp which is wrong.
<android.support.v7.widget.RecyclerView
android:id="#+id/rvRecipeInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal"
android:padding="15dp"></android.support.v7.widget.RecyclerView>
similer to cardview as well
< ?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recipeInfoCards"
android:layout_width="100dp"
android:layout_height="75dp"
app:cardCornerRadius="6dp"
android:elevation="15dp"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorRecipeInfoCardBG"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/txtRecipeInfoCardTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cooking Time:"
android:textSize="11sp"
android:textColor="#android:color/black"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_marginTop="5dp"
/>
<TextView
android:id="#+id/txtRecipeInfoCardDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="20 Minutes"
android:textSize="11sp"
android:textColor="#color/colorTextSecondary"
android:layout_gravity="center_horizontal"
android:gravity="center"/>
</LinearLayout>
</android.support.v7.widget.CardView>

CardView ClickListener Android

I have a cardView with an adapter and also a Navigation Drawer. What i want to do is that when a card is clicked, open the drawer and display the card inside the drawer.
Here is what i have tryied so far, but the listener is not working:
CardViewLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="1dp"
android:foreground="?android:attr/selectableItemBackground"
>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cv"
card_view:cardCornerRadius="0dp"
card_view:cardElevation="#dimen/cardview_default_elevation"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/extension_photo"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginEnd="16dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/file_name"
android:layout_toEndOf="#+id/extension_photo"
android:text="archivo.txt"
android:layout_centerVertical="true"
android:textSize="20sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/file_size"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:text="50kb"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
DrawerLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="#dimen/header_height">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/right_drawe_card_view"
card_view:cardCornerRadius="0dp"
card_view:cardElevation="#dimen/cardview_default_elevation"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/extension_photo"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginEnd="16dp"
android:src="#mipmap/ic_insert_drive_file_black_24dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/file_name"
android:layout_toEndOf="#+id/extension_photo"
android:text="archivo.txt"
android:layout_centerVertical="true"
android:textSize="20sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/file_size"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:text="50kb"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="10dp"
android:paddingTop="20dp">
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/right_drawer_download_button"
android:contentDescription="downloadButton"
android:src="#mipmap/ic_file_download_black_24dp"
style="?android:attr/borderlessButtonStyle"
android:background="?android:selectableItemBackgroundBorderless"
android:layout_weight="1"
android:clickable="true" />
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/right_drawer_share_button"
android:contentDescription="shareButton"
android:src="#mipmap/ic_share_black_24dp"
style="?android:attr/borderlessButtonStyle"
android:background="?android:selectableItemBackgroundBorderless"
android:layout_weight="1"
android:clickable="true" />
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/right_drawer_delete_button"
android:contentDescription="deleteButton"
android:src="#mipmap/ic_delete_black_24dp"
style="?android:attr/borderlessButtonStyle"
android:background="?android:selectableItemBackgroundBorderless"
android:layout_weight="1"
android:clickable="true"
android:contextClickable="true" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
Adapter
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.FileViewHolder> {
public static class FileViewHolder extends RecyclerView.ViewHolder{
CardView cardView;
ImageView icon;
TextView namePlusExtension;
TextView size;
FileViewHolder(final View itemView,View.OnClickListener newListener){
super(itemView);
icon = (ImageView) itemView.findViewById(R.id.extension_photo);
namePlusExtension = (TextView) itemView.findViewById(R.id.file_name);
size = (TextView) itemView.findViewById(R.id.file_size);
itemView.setOnClickListener(newListener);
}
}
List<FileCard> files;
View.OnClickListener listener;
CardAdapter(View.OnClickListener newListener,List<FileCard> newFiles){
this.listener = newListener;
this.files = newFiles;
}
#Override
public int getItemCount(){
return this.files.size();
}
#Override
public FileViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.file_card_layout, viewGroup, false);
FileViewHolder fileViewHolder = new FileViewHolder(v,this.listener);
return fileViewHolder;
}
#Override
public void onBindViewHolder(FileViewHolder fileViewHolder, int i) {
List<String> imageExtensionList = Arrays.asList(".jpg",".bmp",".gif",".png",".psd",".pspimage",".thm",".tiff",".yuv");
List<String> sourceExtensionList = Arrays.asList(".c",".cpp",".java",".py",".sh",".pl");
List<String> musicExtensionList = Arrays.asList(".mp3",".wav",".mid",".wma");
List<String> videoExtensionList = Arrays.asList(".3gp",".avi",".mp4",".mkv",".3g2",".asf",".asx",".mov",".mpg",".wmv");
fileViewHolder.namePlusExtension.setText(files.get(i).name + files.get(i).extension);
fileViewHolder.size.setText(files.get(i).size);
String extension = files.get(i).extension;
int resource = R.mipmap.ic_insert_drive_file_black_24dp;
if( imageExtensionList.contains(extension) ) {
resource = R.mipmap.ic_photo_black_24dp;
}else if( sourceExtensionList.contains(extension)){
resource = R.mipmap.ic_code_black_24dp;
}else if( musicExtensionList.contains(extension)){
resource = R.mipmap.ic_music_note_black_24dp;
}else if( videoExtensionList.contains(extension)){
resource = R.mipmap.ic_videocam_black_24dp;
}
fileViewHolder.icon.setImageResource(resource);
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
Activity
onCreate(){
...
...
DrawerLayout drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
NavigationView rightDrawerView = (NavigationView)findViewById(R.id.right_drawer_view);
...
...
this.recyclerView = (RecyclerView) findViewById(R.id.recycler_files_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
this.recyclerView.setLayoutManager(layoutManager);
this.fileCards = new ArrayList<>();
fileCards.add(new FileCard("archivo1",".jpg","50kb"));
fileCards.add(new FileCard("archivo2",".cpp","100kb"));
fileCards.add(new FileCard("archivo3", ".txt", "200kb"));
updateFileCards();
}
private void updateFileCards(){
View.OnClickListener l = new View.OnClickListener() {
#Override
public void onClick(View v) {
CardView c = (CardView) rightDrawerView.findViewById(R.id.right_drawe_card_view);
TextView t1 = (TextView)c.findViewById(R.id.file_size);
TextView t2 = (TextView)c.findViewById(R.id.file_name);
ImageView t3 =(ImageView) c.findViewById(R.id.extension_photo);
TextView a1 = (TextView)v.findViewById(R.id.file_size);
TextView a2 = (TextView)v.findViewById(R.id.file_name);
ImageView a3 =(ImageView) v.findViewById(R.id.extension_photo);
a1.setText(t1.getText().toString());
a2.setText(t2.getText().toString());
a3.setImageDrawable(t3.getDrawable());
drawerLayout.openDrawer(rightDrawerView);
}
};
CardAdapter adapter = new CardAdapter(l,this.fileCards);
this.recyclerView.setAdapter(adapter);
}
Any idea how i can do that?

Categories

Resources