Create the Recycler view with fixed item count in horizontal scroll - java

Please help Me,
I am trying to create a specific type of recycler view which can have always 3 items in all kind of mobile phone. Whether that mobile is having 4 inch display or 6.5 inch display.
Please tell me if that is possible or not.
my code of xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:id="#+id/recomended"
android:layout_margin="6dp"
android:background="#drawable/graycurverdbtn"
android:layout_height="170dp">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="85dp"
android:scaleType="fitCenter"
android:src="#drawable/mountain">
</ImageView>
<LinearLayout
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:gravity="center"
>
<TextView
android:id="#+id/pinkbtn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#drawable/pinkcurvedbtn"
android:gravity="center"
android:padding="3dp"
android:text="Recomended"
android:textColor="#color/white"
android:textSize="9dp">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_below="#id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="8dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="normal"
android:layout_margin="6dp"
android:layout_gravity="center"
android:textSize="10sp"
android:textColor="#color/black"
android:text="Pest control">
</TextView>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/grey"></View>
<TextView
android:layout_margin="8dp"
android:id="#+id/shopname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10dp"
android:textStyle="bold"
android:textColor="#color/black"
android:text="K02BgbhdAf">
</TextView>
<TextView
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/graylocation"
android:text="Airport Road"
android:id="#+id/address"
android:textSize="18sp"
android:textStyle="normal">
</TextView>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
This is my Adapter code
public class OurrecomendedAdapter extends RecyclerView.Adapter<OurrecomendedAdapter.MyViewHolder> {
private List<HomeResponse.Recommend> ourrecomendedlist = new ArrayList<>();
private Context context;
public static int recomendedid=0;
public OurrecomendedAdapter(List<HomeResponse.Recommend> list, Context context) {
this.ourrecomendedlist = list;
this.context = context;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recomemdedservices_item, parent, false);
// itemView.setLayoutParams(new ViewGroup.LayoutParams((int) (parent.getWidth() * 0.3),ViewGroup.LayoutParams.MATCH_PARENT));
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, #SuppressLint("RecyclerView") int position) {
holder.title.setText(ourrecomendedlist.get(position).getCategory().getSlug());
holder.shopname.setText(ourrecomendedlist.get(position).getCategory().getName());
Picasso.get().load(ourrecomendedlist.get(position).getImage()).into(holder.image, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
}
});
holder.recomended.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(ourrecomendedlist.get(position).getCategory().getName().toString().equalsIgnoreCase("salon for women") || ourrecomendedlist.get(position).getCategory().getName().toString().equalsIgnoreCase("salon for men")){
AppCompatActivity activity = (AppCompatActivity) context;
WomensalonlistFragment subc = new WomensalonlistFragment();
Bundle args = new Bundle();
args.putString("id", ourrecomendedlist.get(position).getProductcategoryId().toString());
args.putString("title", ourrecomendedlist.get(position).getName());
subc.setArguments(args);
activity.getSupportFragmentManager().beginTransaction().addToBackStack(null).
replace(R.id.nav_host_fragment, subc).commit();
}
else
{
typeListid= ourrecomendedlist.get(position).getId().toString();
Intent intent= new Intent(context, ProductdetailsActivity.class);
intent.putExtra("productId",typeListid);
context.startActivity(intent);
HomeFragment.vendormainid=ourrecomendedlist.get(position).getId().toString();
}
}
});
}
#Override
public int getItemCount() {
return this.ourrecomendedlist.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView name,title,shopname,address,pinkbtn;
Button addtocart;
ImageView image;
RelativeLayout recomended;
MyViewHolder(View itemView) {
super(itemView);
addtocart=itemView.findViewById(R.id.addtocart);
image=itemView.findViewById(R.id.image);
title=itemView.findViewById(R.id.title);
shopname=itemView.findViewById(R.id.shopname);
address=itemView.findViewById(R.id.address);
recomended=itemView.findViewById(R.id.recomended);
name=itemView.findViewById(R.id.name);
pinkbtn=itemView.findViewById(R.id.pinkbtn);
}
}
}
in this i need to display our output as 3 items in each aspect-ratio mobile.
but I'm unable to achieve that. If I'm opening in big screen mobile then it's showing 3.5 items and in small device some times it's showing 2.75 items.
Kindly help me in that how to achieve that exact output.

Related

buttons in Onclick Recycler View doesn't work

when Click on Button it suppose to Open a browser but it didn't get to the code to start the Activity
public class RecyclerViewAdapterDetailsScreen extends RecyclerView.Adapter<RecyclerViewAdapterDetailsScreen.ViewHolder> {
private Context mContext;
private int numberOfButtons;
private String [] trailers;
public RecyclerViewAdapterDetailsScreen(Context mContext, int numberOfButtons,String [] trailers) {
this.numberOfButtons=numberOfButtons;
this.mContext = mContext;
this.trailers=trailers;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem2, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
/*** Toast.makeText(mContext, trailers[position]+" From OnBindViewHolder", Toast.LENGTH_SHORT).show();
* Toast is working from Here but no sign from on click
*/
holder.parentlayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
// TOAST IS NOT WORKING SO THE CODE DOESNT gett HERE...............................
Toast.makeText(mContext, trailers[position], Toast.LENGTH_SHORT).show();
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(trailers[position]));
mContext.startActivity(browserIntent);
}
});
}
#Override
public int getItemCount() {
return trailers.length;
}
public class ViewHolder extends RecyclerView.ViewHolder
{
Button mbutton;
RelativeLayout parentlayout;
public ViewHolder(#NonNull View itemView) {
super(itemView);
mbutton=itemView.findViewById(R.id.button);
parentlayout = itemView.findViewById(R.id.parent_layout2);
}
}
}
layout_listitem2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/parent_layout2"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
/>
</RelativeLayout>
activity_details_screen.xml that has the recyclerview
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DetailsScreen">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="#+id/original_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="25sp"
android:layout_below="#+id/image_thumbnail"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="#+id/overview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/original_title"
android:textSize="18sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="vote average "
android:id="#+id/vote"
android:layout_below="#+id/overview"
android:textStyle="bold"
android:textSize="20sp"/>
<TextView
android:id="#+id/vote_average"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/vote"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="release date"
android:layout_below="#id/vote_average"
android:textStyle="bold"
android:textSize="20sp"
android:id="#+id/release"
/>
<TextView
android:id="#+id/release_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/release"
/>
<TextView
android:id="#+id/trailers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="25sp"
android:layout_below="#+id/release_date"
android:layout_centerHorizontal="true"
android:text="Trailers"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/trailers"
android:padding="8dp"
/>
</RelativeLayout>
</ScrollView>
it has to get in the onClick and start new activity to open youtube app or browser if any missing code you wanna review tell me
Your code is perfect.
Only one problem is here, your parent_layout2 is not getting click-event it is consumed by child(the button).As by Android definition first priority is given to child.
Just update parents width to match_parent. It will work. I have tested your code just now.
Here is your updated xml
layout_listitem2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/parent_layout2"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
/>
</RelativeLayout>
Happy Coding. Please Vote if it works.
Thanks

How show a Horizontal Recyclerview - have trouble showing horizontal items

I want to show horizontal items in recyclerview, but I've tried but no work it all, only show me horizontal items with enlarge widh.
In Android Studio show as I want
But in runtime in my movile, show me
The items is width wraped but the other items appear after scroll right separated of my previus item.
This is my code.
LIST_ITEM
<?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="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linearFondo"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#drawable/item_list_sel"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView2"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
app:srcCompat="#drawable/ruta"
tools:srcCompat="#drawable/ruta" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<TextView
android:id="#+id/tnombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:inputType="textMultiLine"
android:lines="1"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textSize="12sp"
android:textStyle="bold"></TextView>
<TextView
android:id="#+id/thorarios"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:textColor="#color/colorPrimary"
android:textSize="10sp"
android:visibility="gone"></TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
And My Xml to show the recyclerview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="sitetech.NFCcheckPoint.ui.operador.CheckFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#color/SuccessColor"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:id="#+id/tfecha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Fecha"
android:textSize="20sp" />
<Button
android:id="#+id/bpruebas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pasar Tarjeta" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Ruta para el registro" />
<com.omega_r.libs.omegarecyclerview.OmegaRecyclerView
android:id="#+id/rlista"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#888"
android:dividerHeight="0dp"
android:fadeScrollbars="true"
android:orientation="horizontal"
android:overScrollMode="never"
android:scrollbarAlwaysDrawHorizontalTrack="true"
android:scrollbarAlwaysDrawVerticalTrack="false"
android:scrollbars="horizontal"
app:itemSpace="0dp"
tools:listitem="#layout/ruta_sel_template">
</com.omega_r.libs.omegarecyclerview.OmegaRecyclerView>
</LinearLayout>
</LinearLayout>
And my Fragment code to load
dataAdapter = new rutaSelAdapter(lista, new onItemClick() {
#Override
public void onClickItemList(View v, int position) {
ToastHelper.info(lista.get(position).getNombre().toString());
}
});
rlista.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
rlista.setAdapter(dataAdapter);
And my dataAdapter class
public class rutaSelAdapter extends OmegaRecyclerView.Adapter<rutaSelAdapter.ViewHolder> {
public List<Ruta> lista;
private onItemClick onItemClick;
private Ruta selectedItem;
public rutaSelAdapter(List<Ruta> l, onItemClick onclick) {
lista = l;
this.onItemClick = onclick;
if (l.size() > 0)
setSelectedItem(l.get(0));
}
#Override
public int getItemCount() {
return lista.size();
}
#Override
public rutaSelAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new rutaSelAdapter.ViewHolder(parent);
}
#Override
public void onBindViewHolder(rutaSelAdapter.ViewHolder holder, int position) {
Ruta dato = lista.get(position);
holder.display(dato);
}
public void setSelectedItem(Ruta rx){
selectedItem = rx;
}
public Ruta getSelectedItem(){
return selectedItem;
}
public void updateData(Ruta bx) {
boolean nuevo = true;
for (Ruta rx : lista) {
if (rx.getId() == bx.getId()) {
lista.set(lista.indexOf(rx), bx);
nuevo = false;
}
ToastHelper.info("Se a modificado el horario.");
}
if (nuevo) {
lista.add(bx);
ToastHelper.exito("Horario asignado.");
}
notifyDataSetChanged();
}
public void deleteData(Ruta rx) {
//ToastHelper.normal("Se a eliminado el horario " + rx.getHora());
lista.remove(rx);
notifyDataSetChanged();
}
public class ViewHolder extends SwipeViewHolder implements View.OnClickListener {
private final TextView tnombre;
private LinearLayout linearFondo;
HorarioDao horarioManager = AppController.daoSession.getHorarioDao();
private Ruta currentItem;
public ViewHolder(ViewGroup itemView) {
super(itemView, R.layout.ruta_sel_template, SwipeViewHolder.NO_ID, SwipeViewHolder.NO_ID);
tnombre = findViewById(R.id.tnombre);
linearFondo = findViewById(R.id.linearFondo);
contentView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setSelectedItem(currentItem);
onItemClick.onClickItemList(v, getAdapterPosition());
}
});
}
private Ruta backItem;
private void setSelection(Ruta rx){
//if (backItem == null)
}
#Override
public void onClick(final View v) {
}
public void display(Ruta rx) {
currentItem = rx;
if (rx.getNombre() == null) tnombre.setText("");
else tnombre.setText(rx.getNombre().toString());
}
}
}
In Android Studio show me right as I want.
But in runtime, in my mobile the items have full width but wrap content.enter image description here
i think you just add notifyDataSetChanged in your class adapter
public rutaSelAdapter(List<Ruta> l, onItemClick onclick) {
lista = l;
this.onItemClick = onclick;
if (l.size() > 0)
setSelectedItem(l.get(0));
notifyDataSetChanged();
}
In your recyclerview item layout update width of these 2 textviews [tnombre and thorarios] to wrap_content
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<TextView
android:id="#+id/tnombre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:inputType="textMultiLine"
android:lines="1"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textSize="12sp"
android:textStyle="bold"></TextView>
<TextView
android:id="#+id/thorarios"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:textColor="#color/colorPrimary"
android:textSize="10sp"
android:visibility="gone"></TextView>
</LinearLayout>
If someone else have this issue, I fixed adding this line in the fragment class.
rlista.setHasFixedSize(true);

My recycler view doesn't show all items that it has, how can I fix it?

I have something wrong with recyclerView. It doesn't show all items. For example, recyclerView has 12 items, but it shows like 10 with a half items. I can set paddingBottom for it, and it shows all items, but I don't think it is a good way. How can I fix it? I think maybe it becouse of my buttons above it.
acctivity_search_coin.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingBottom="50dp"
app:layout_constraintTop_toBottomOf="#+id/lianerForThreeBtn" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:background="#FFFFFF"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:textSize="15sp"
android:drawablePadding="6dp"
android:gravity="center_vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/lianerForThreeBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="#+id/button2">
<Button
android:id="#+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:layout_weight="1"/>
<Button
android:id="#+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:layout_weight="1"/>
<Button
android:id="#+id/btn3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:layout_weight="1"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
activity_recycler_view_adapter_coin_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recyclerImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="#+id/denominationTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginStart="20dp"
android:layout_marginEnd="8dp"
android:layout_toEndOf="#+id/secondIV"
android:textSize="20sp" />
<TextView
android:id="#+id/yearTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/denominationTV"
android:layout_alignStart="#+id/denominationTV"
android:layout_alignParentEnd="true"
android:layout_marginStart="0dp"
android:layout_marginTop="6dp"
android:layout_marginEnd="8dp"
android:textSize="20sp" />
<ImageView
android:id="#+id/firstIV"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentStart="true"
android:layout_marginStart="0dp"/>
<ImageView
android:id="#+id/secondIV"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="4dp"
android:layout_toEndOf="#+id/firstIV"/>
</RelativeLayout>
RecyclerViewAdapterCoinList.java
public class RecyclerViewAdapterCoinList extends RecyclerView.Adapter<RecyclerViewAdapterCoinList.ViewHolder> {
private List<String> mYearList;
private List<String> mDenominationList;
private List<String> mImageList1;
private List<String> mImageList2;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
Context context;
RecyclerViewAdapterCoinList(Context context, List<String> denominationList,List<String> yearList, List<String> imageList1,List<String> imageList2) {
this.mInflater = LayoutInflater.from(context);
this.mYearList = yearList;
this.mDenominationList = denominationList;
this.mImageList1 = imageList1;
this.mImageList2 = imageList2;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.activity_recycler_view_adapter_coin_list, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
String year = mYearList.get(position);
String denomination = mDenominationList.get(position);
String imageString1 = mImageList1.get(position);
String imageString2 = mImageList2.get(position);
holder.denominationTV.setText(denomination);
holder.yearTV.setText(year);
try {
Glide.with(context).load(imageString1).into(holder.firstIV);
Glide.with(context).load(imageString2).into(holder.secondIV);
}catch (Exception e){e.printStackTrace();}
}
// total number of rows
#Override
public int getItemCount() {
return mDenominationList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView denominationTV;
TextView yearTV;
ImageView firstIV;
ImageView secondIV;
ViewHolder(View itemView) {
super(itemView);
denominationTV = itemView.findViewById(R.id.denominationTV);
yearTV = itemView.findViewById(R.id.yearTV);
firstIV = itemView.findViewById(R.id.firstIV);
secondIV = itemView.findViewById(R.id.secondIV);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
String getItem(int id) {
return mDenominationList.get(id);
}
void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}
Try adding app:layout_constraintBottom_toBottomOf="parent" in the recycler view and also make the height of the recycler view to be 0dp (match_constraint).

Items get mixed up when changing xml attributes using the adapter class in recycle view in android

I have an adapter class which loads items dynamically and shows them in a RecyclerView. My problem is that when I swipe up and down the recycler view the images get mixed and are shown in wrong positions (i.e. in other item's view).
For example, I have an item X and another item Y. Now when I swipe up and down the image which was to be shown in item Y get shown in item X and the same happens with other attributes.
Here is my adapter class:
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.NewsViewHolder> {
private DatabaseReference refDNewsTable= FirebaseDatabase.getInstance().getReference().child("news_feeds");
private StorageReference refStorage = FirebaseStorage.getInstance().getReferenceFromUrl("firebase_url_comes_here");
private Context mContext;
private List<NewsModel> newsList;
public class NewsViewHolder extends RecyclerView.ViewHolder {
public TextView nHeading, nDes,nDaTi;
public ImageView nImg;
public NewsViewHolder(View view) {
super(view);
nHeading = (TextView) view.findViewById(R.id.tv_newsHeadline);
nDes = (TextView) view.findViewById(R.id.tv_nDes);
nImg = (ImageView) view.findViewById(R.id.iv_nImg);
nDaTi=(TextView) view.findViewById(R.id.tv_nDaTi);
}
}
public NewsAdapter(Context mContext, List<NewsModel> newsList) {
this.mContext = mContext;
this.newsList = newsList;
}
#Override
public NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.news_model, parent, false);
return new NewsViewHolder(itemView);
}
#Override
public void onBindViewHolder(final NewsViewHolder holder, int position) {
NewsModel news = newsList.get(position);
holder.nHeading.setText(news.getnHeading());
if(news.hasDes()) {
holder.nDes.setText(news.getnDes());
}else{
holder.nDes.setVisibility(View.GONE);
}
if(news.hasImg()){
// Reference to an image file in Firebase Storage
StorageReference imgReference =refStorage.child(news.getnId());
// Load the image using Glide
Glide.with(mContext)
.using(new FirebaseImageLoader())
.load(imgReference)
.into(holder.nImg);
}else{
holder.nImg.setVisibility(View.GONE);
}
holder.nDaTi.setText(news.getnDaTi());
}
public void downloadFile(final NewsViewHolder holder){
try {
final File localFile = File.createTempFile("images", "jpg");
refStorage.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Bitmap bitmap = BitmapFactory.decodeFile(localFile.getAbsolutePath());
holder.nImg.setImageBitmap(bitmap);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
}
});
} catch (IOException e ) {}
}
#Override
public int getItemCount() {
return newsList.size();
}
}
And this is my news_model.xml file:
<?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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_margin="4dp"
card_view:cardPreventCornerOverlap="true"
card_view:cardElevation="16dp"
android:layout_height="wrap_content"
android:id="#+id/card_view"
android:layout_gravity="center"
card_view:cardCornerRadius="0dp">
<LinearLayout
android:orientation="vertical"
android:background="#color/cBgNews"
android:padding="0dp"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:padding="10dp"
card_view:cardElevation="6dp"
card_view:cardBackgroundColor="#color/cNewsHeadline"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="0dp"
android:id="#+id/card_des"
android:layout_gravity="center"
card_view:cardCornerRadius="0dp">
<TextView
android:text="TextView"
android:textSize="17sp"
android:textStyle="normal"
android:padding="10dp"
android:maxLines="3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/cWhite"
android:gravity="start"
android:layout_margin="0dp"
android:id="#+id/tv_newsHeadline" />
</android.support.v7.widget.CardView>
<LinearLayout
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="#0000"
android:paddingEnd="6dp"
android:paddingStart="6dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_marginBottom="0dp"
android:layout_height="wrap_content"
android:maxHeight="300dp"
app:srcCompat="#mipmap/ic_launcher"
android:scaleType="fitCenter"
android:layout_gravity="center_horizontal"
android:id="#+id/iv_nImg" />
<TextView
android:text="Description..."
android:maxLines="8"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:padding="10dp"
android:textColor="#color/cBlack"
android:id="#+id/tv_nDes" />
<TextView
android:text="Date and Time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10sp"
android:textColor="#999999"
android:gravity="end"
android:id="#+id/tv_nDaTi" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
NOTE: I am using firebase to receive the content directly and haven't created any kind of database in my app yet. i.e. it loads data every time it needs it directly in adapter class.

RecycleView do not displaying the data

I have very popular question - why the data not showing in the RecycleView.
That is my Adapter:
public class CommentsAdapter extends RecyclerView.Adapter<CommentsAdapter.ViewHolder> {
private static final String TAG = "CommentsAdapter";
public ImageView mThumbView;
private List<PostComment> postCommentList;
public CommentsAdapter(List<PostComment> postCommentList) {
this.postCommentList = postCommentList;
}
#Override
public CommentsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_comment_item, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(CommentsAdapter.ViewHolder holder, int position) {
PostComment record = postCommentList.get(position);
holder.mAuthorTextView.setText(record.getUser().getFull_name());
holder.mDateTextView.setText(record.getCreated_at());
holder.mTextTextView.setText(record.getText());
}
#Override
public int getItemCount() {
Log.i(TAG + " comments size", Integer.toString(postCommentList.size()));
return postCommentList.size();
}
public void addItems(List<PostComment> postCommentList) {
this.postCommentList.addAll(postCommentList);
}
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mAuthorTextView;
public TextView mDateTextView;
public TextView mTextTextView;
public ViewHolder(View v) {
super(v);
mAuthorTextView = (TextView) itemView.findViewById(R.id.comment_author);
mDateTextView = (TextView) itemView.findViewById(R.id.comment_date);
mTextTextView = (TextView) itemView.findViewById(R.id.comment_text);
}
}
}
That is my view:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
....>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp">
<ImageView
android:id="#+id/comment_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="centerInside"
android:src="#drawable/cat"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/middleLayout"
android:layout_toRightOf="#+id/comment_image"
android:layout_marginTop="8dp">
<TextView
android:id="#+id/comment_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:singleLine="true"
android:text="Dark Plastic"
android:textColor="?attr/colorPrimary"
android:textSize="14dp" />
<TextView
android:id="#+id/comment_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#+id/comment_author"
android:text="21 minutes"
android:textSize="14dp" />
</RelativeLayout>
<TextView
android:id="#+id/comment_text"
android:layout_width="wrap_content"
android:layout_toRightOf="#+id/comment_image"
android:layout_height="wrap_content"
android:maxLines="3"
android:layout_marginTop="8dp"
android:layout_below="#id/middleLayout"
android:text="sd"
android:textColor="#android:color/black"
android:textSize="16dp" />
</RelativeLayout>
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_gravity="bottom"
android:paddingLeft="16dp"
android:paddingRight="16dp"></View>
That is how I set up the adapter:
mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
post_details_comments_list.setLayoutManager(mLayoutManager);
mCommentListAdapter = new CommentsAdapter(new ArrayList<PostComment>());
post_details_comments_list.setAdapter(mCommentListAdapter);
After I set up the adapter, I the getItemCount returns the quantity of the items - so that is not null.
RecycleView layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.gms.maps.MapView
android:id="#+id/post_details_mapview"
android:layout_width="wrap_content"
android:layout_height="200dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/post_details_comments_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/TextLabelStyle"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/post_details_comments_list"
android:layout_width="match_parent"
android:scrollbars="vertical"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/edit_comment"
android:hint="#string/hint_type_comment"/>
</LinearLayout>
I make the request to the server and here is the callback, where I get the results and set up the adapter:
#Override
public void onAllPostCommentsCallback(AllComments allComments) {
Log.i(TAG + " I've the comments", allComments.getPostCommentList().toString());
mCommentListAdapter.addItems(allComments.getPostCommentList());
post_details_comments_list.setAdapter(mCommentListAdapter);
mCommentListAdapter.notifyDataSetChanged();
}
But the list is not displaying.
What can be the reason?
put null inplace of parent in View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_comment_item, parent, false);
I've already solved the issue. The problem was that I had RecycleView inside the NestedScrollView
Answer in this post helped me a lot:
How to use RecyclerView inside NestedScrollView?

Categories