In one of my projects, I used ListView to show a list of items to the user, and it works like a charm. Now that I have tried replacing Listview with RecyclerView and succeed to show items to the user.
Well, the problem is that I have ever used a customized Listview element that works with Listview. But when I try to load the same with RecyclerView, it is not alignment / fit of the screen.
Here is my expected result. I achieved this using ListView. Here is the result that I expect

but when I used RecyclerView, it displays below.

But I use the same listview_item xml for both views to load.
Here is my listview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listrow"
android:layout_width="match_parent"
android:layout_height="55dp"
android:weightSum="9"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_weight="0.5"
android:src="@drawable/green" />
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1.5"
android:gravity="center"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="@+id/textView5"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:textColor="#000000"
android:textSize="20sp" />
<ImageView
android:id="@+id/imageView_followupone"
android:layout_width="0dp"
android:layout_height="22dp"
android:layout_gravity="center"
android:layout_weight="1"
android:src="@drawable/nored" />
<ImageView
android:id="@+id/imageView_followuptwo"
android:layout_width="0dp"
android:layout_height="22dp"
android:layout_gravity="center"
android:layout_weight="1"
android:src="@drawable/yesgreen" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="0dp"
android:layout_height="55dp"
android:layout_weight="1"
android:gravity="center"
android:src="@drawable/finalise" />
</LinearLayout>
:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private static ArrayList<SearchParms> itemData;
private static Context abc;
private int durationOfPatientRecord;
View itemLayoutView;
public MyAdapter( ArrayList<SearchParms> items, Context context) {
this.itemData = items;
this.abc=context;
}
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_row_serch, null);
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
SearchParms item = itemData.get( position );
viewHolder.textView1.setText( item.getPartcipantId());
viewHolder.textView2.setText( item.getParticipantName());
viewHolder.textView3.setText( "" + item.getAge());
viewHolder.textView4.setText( item.getCreateDate());
viewHolder.imgView1.setImageResource( R.drawable.finalise );
viewHolder.imgView2.setImageResource( R.drawable.yesgreen );
viewHolder.imgView3.setImageResource( R.drawable.yesgreen );
itemLayoutView.setBackgroundColor( Color.parseColor( "#15ffffff" ) );
}
}
public static class ViewHolder extends RecyclerView.ViewHolder implements OnClickListener {
public TextView textView1, textView2,textView3, textView4, textView5;
public ImageView imgView1, imgView2, imgView3, imgView4;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
textView1 = (TextView) itemLayoutView.findViewById(R.id.textView1);
textView2 = (TextView) itemLayoutView.findViewById(R.id.textView2);
textView3 = (TextView) itemLayoutView.findViewById(R.id.textView3);
textView4 = (TextView) itemLayoutView.findViewById(R.id.textView4);
textView5 = (TextView) itemLayoutView.findViewById(R.id.textView5);
imgView1 = (ImageView) itemLayoutView.findViewById(R.id.imageView1);
imgView2 = (ImageView) itemLayoutView.findViewById(R.id.imageView_followupone);
imgView3 = (ImageView) itemLayoutView.findViewById(R.id.imageView_followuptwo);
imgView4 = (ImageView) itemLayoutView.findViewById(R.id.imageView2);
itemLayoutView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int i=getAdapterPosition();
SearchParms item = itemData.get( i );
Toast.makeText(abc, "You Selected:: " +item.getParticipantName(), 3000).show();
}
}
@Override
public int getItemCount() {
return itemData.size();
}
}
. - RecyclerView? .