CardStackView is not working with Scrollview? - android

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.

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

updateoptions in firebase recyclerview inside scrollview not working

i have created firebase recyclerview inside nestedscrollview in fragment.it works well.but when i use updateoptions in firebase recyclerview,the recyclerview not working.
I dont know how to resolve this problem.
initially the fragment created it loaded with items from firebase.the recyclerview not work when i click the button to updateoptions
this is my firebaserecyclerview
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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="#drawable/fragment_bg"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:orientation="vertical"
android:layout_marginBottom="50dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/montserrat"
android:text="Hi, Aashik"
android:textColor="#color/colorPrimary"
android:layout_marginHorizontal="20dp"
android:textSize="25dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/montserrat_bold"
android:layout_marginHorizontal="20dp"
android:text="Welcome Back"
android:textColor="#color/colorPrimary"
android:textSize="30dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:fontFamily="#font/montserrat_bold"
android:text="Category"
android:textColor="#color/colorPrimaryDark"
android:textSize="20sp" />
<Button
android:id="#+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/montserrat_bold"
android:text="Products"
android:layout_marginVertical="10dp"
android:layout_marginHorizontal="20dp"
android:textColor="#color/colorPrimaryDark"
android:textSize="20sp" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/productsRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="100dp"
android:layoutAnimation="#anim/layoutanimation"
/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
and the code is
productsRef = FirebaseDatabase.getInstance().getReference().child("products");
recyclerView = (RecyclerView) root.findViewById(R.id.productsRecyclerView);
btn= (Button) root.findViewById(R.id.btn);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
FirebaseRecyclerOptions<Products> options=
new FirebaseRecyclerOptions.Builder<Products>()
.setQuery(productsRef, Products.class)
.build();
adapter = new FirebaseRecyclerAdapter<Products, ProductViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull final ProductViewHolder holder, final int position, #NonNull final Products model) {
holder.productNameTv.setText(model.getName());
Picasso.get().load(model.getImageurl()).fit().into(holder.productImageIv);
holder.cardcontainer.setAnimation(AnimationUtils.loadAnimation(getActivity(),R.anim.cardviewanimation));
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), ProductDetailsActivity.class);
intent.putExtra("imageurl",model.getImageurl());
intent.putExtra("name",model.getName());
intent.putExtra("desc",model.getDesc());
intent.putExtra("mrp",model.getMrp());
intent.putExtra("availability",model.getAvailability());
intent.putExtra("pid",model.getPid());
startActivity(intent);
}
});
}
#NonNull
#Override
public ProductViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_items_layout, parent, false);
ProductViewHolder holder = new ProductViewHolder(view);
return holder;
}
};
recyclerView.setAdapter(adapter);
adapter.startListening();
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Query query = productsRef.orderByChild("category").equalTo(catName);
FirebaseRecyclerOptions<Products> newOptions=
new FirebaseRecyclerOptions.Builder<Products>()
.setQuery(query, Products.class)
.build();
adapter.updateOptions(newOptions);
);
recylerview layout is
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_marginVertical="10dp"
app:cardCornerRadius="2dp"
app:cardElevation="5dp"
android:layout_marginHorizontal="20dp"
android:id="#+id/cardcontainer"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/productImageIv"
android:layout_width="100dp"
android:layout_height="match_parent"
android:src="#drawable/ic_topsym"
android:layout_margin="10dp"
>
</ImageView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/productNameTv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp"
android:text="#string/welcome"
android:textColor="#color/colorBlack"
android:fontFamily="#font/arimo_bold"
android:padding="20dp">
</TextView>
</RelativeLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
Thank in advance.
Please use swipe to refresh component or use notifiedDatasetChanged() in your adapter

Why Recyclerview doesn't scroll inside AppBarLayout

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.

ViewPager in moto x4 not working properly

I am using viewpager for displaying 3 screens like introductory screens. It is working fine on all phones, but not working properly in Moto X4. These are the screenshots from Moto X4 :
This is the xml code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical"
android:visibility="visible"
android:weightSum="1">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="0.7">
<LinearLayout
android:id="#+id/viewpager_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/login_slide_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<android.support.design.widget.TabLayout
android:id="#+id/login_tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/viewpager_container"
app:tabBackground="#drawable/login_dot_selector"
app:tabGravity="center"
app:tabIndicatorHeight="0dp"
app:tabRippleColor="#null"
app:tabPaddingStart="7dp"
app:tabPaddingEnd="7dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:orientation="vertical"
>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/warm_grey" />
<LinearLayout
android:id="#+id/login_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="visible"
android:orientation="vertical">
<LinearLayout
android:id="#+id/bt_fb_login"
android:layout_width="232dp"
android:layout_height="53dp"
android:background="#drawable/facebook_button_background"
android:gravity="center_vertical"
android:orientation="horizontal">
<com.sharesmile.share.views.MRTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/logn_with_fb"
android:textAllCaps="true"
android:textColor="#color/white_95"
android:textSize="12.5sp" />
<ImageView
android:layout_width="47dp"
android:layout_height="47dp"
android:src="#drawable/logo_fb" />
</LinearLayout>
<LinearLayout
android:id="#+id/bt_google_login"
android:layout_width="232dp"
android:layout_height="53dp"
android:layout_marginTop="13dp"
android:background="#drawable/google_button_background"
android:gravity="center_vertical"
android:orientation="horizontal">
<com.sharesmile.share.views.MRTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/logn_with_google"
android:textAllCaps="true"
android:textColor="#color/white_95"
android:textSize="13.4sp" />
<ImageView
android:layout_width="47dp"
android:layout_height="47dp"
android:background="#drawable/login_google"
android:src="#drawable/google_icon"
/>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:id="#+id/progress_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:visibility="gone">
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
Java code :
public class LoginSliderAdapter extends PagerAdapter {
public int slider_videos[] = {
R.raw.v1,
R.raw.v2,
R.raw.v3
};
#BindView(R.id.iv_slider_image)
VideoView sliderImage;
#BindView(R.id.tv_slider_title)
TextView sliderTitle;
#BindView(R.id.tv_slider_description)
TextView sliderDescription;
Context context;
LayoutInflater layoutInflater;
public LoginSliderAdapter(Context context) {
this.context = context;
}
#Override
public int getCount() {
return 3;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object o) {
return view == o;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.login_slider, container, false);
ButterKnife.bind(this, view);
setVideoToVideoPlayer(position);
sliderTitle.setText(context.getResources().getStringArray(R.array.login_tiles)[position]);
sliderDescription.setText(context.getResources().getStringArray(R.array.login_desc)[position]);
container.addView(view);
return view;
}
private void setVideoToVideoPlayer(int position) {
Uri uri = Uri.parse("android.resource://com.sharesmile.share/"
+ slider_videos[position]);
sliderImage.setBackgroundColor(Color.TRANSPARENT);
sliderImage.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
sliderImage.setVideoURI(uri);
sliderImage.requestFocus();
sliderImage.setZOrderOnTop(true);
sliderImage.start();
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((LinearLayout) object);
}
}
So what is this issue, not able to figure out and never so this issue.

Change RecycleView item width?

I know there is probably a very simple solution out there, but for the life of me I can't seem to find it. How do I change the width of the RecycleView item? For some reason, it's displaying the first item full width of the parent... It's displaying a lot of white space in between each item.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_wrapper"
android:background="#color/colorPrimary"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:padding="20dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Categories"
android:textColor="#ffffff"
android:layout_marginBottom="10dp"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cats"
></android.support.v7.widget.RecyclerView>
</LinearLayout>
</LinearLayout>
CategoryAdapter:
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {
private ArrayList<Category> categories;
public CategoryAdapter(ArrayList<Category> categories) {
this.categories = categories;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_cat, parent, false);
return new ViewHolder(v);
}
#Override public void onBindViewHolder(ViewHolder holder, int position) {
Category cat = this.categories.get(position);
holder.text.setText(cat.getText().toString());
}
#Override public int getItemCount() {
return categories.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView text;
public ViewHolder(View itemView) {
super(itemView);
text = (TextView) itemView.findViewById(R.id.cat_title);
}
}
}
single_cat.xml
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:text="Cat"
android:background="#drawable/cat_single_shape"
android:padding="10dp"
android:gravity="center"
android:id="#+id/cat_title"
android:textSize="13sp"
android:fontFamily="#font/open_sans_bold"
android:textColor="#color/colorPrimary"/>
1)in your fragment do :
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, true));
2)for recyclerView single item .xml do :
<?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"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_margin="4dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
</LinearLayout>
3) and change RecyclerView to :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_wrapper"
android:background="#color/colorPrimary"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.2"
android:text="Categories"
android:textColor="#ffffff"
android:layout_marginBottom="10dp"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:id="#+id/cats"/>
</LinearLayout>
i would change single cat to
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#drawable/cat_single_shape"
android:padding="10dp"
android:gravity="center"
android:id="#+id/cat_title"
android:textSize="13sp"
android:fontFamily="#font/open_sans_bold"
android:textColor="#color/colorPrimary"/>
Finally, after 2 days and looking all over the internet, I figured it out myself.
Make sure your Constraint Layout is wrap_content. This was the issue all along!

Categories

Resources