Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List Item aligninment issue with Recycler View

One of my project i used ListView to show the list of items to the user and it is working like a charm. Now as a try i replaced the Listview with the RecyclerView and succeed to show the items to the user.

Well the problem is when ever i used a customized Listview item it is working with the Listview. But when i try to load the same with RecyclerView it is not aligning/ fitting for the screen.

Here is my expected result.. I achieved this by using ListView.. Here is the Result what i am expecting

enter image description here

but when i used RecyclerView it is showing as below.

enter image description here

But i am using 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>

Adapter Class:

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;
    }

    // Create new views (invoked by the layout manager)
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
            int viewType) {
        // create a new view
        itemLayoutView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_row_serch, null);


        // create ViewHolder
        ViewHolder viewHolder = new ViewHolder(itemLayoutView);

        return viewHolder;
    }

    // Replace the contents of a view (invoked by the layout manager)
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {

        // - get data from your itemsData at this position
        // - replace the contents of the view with that itemsData

        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" ) );


        }

    }

    // inner class to hold a reference to each item of RecyclerView 
    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();

        }
    }

    // Return the size of your itemsData (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return itemData.size();
    }
}

where it is making problem. is there any type of limitation for using RecyclerView? Curious to know about this. Thanks

like image 325
King of Masses Avatar asked Jan 09 '23 10:01

King of Masses


1 Answers

Instead of setting your view like this:

itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
        R.layout.list_row_serch, null);

Do the following:

itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
        R.layout.list_row_serch, parent, false);
like image 85
venkatesh venkey Avatar answered Jan 16 '23 22:01

venkatesh venkey