Recyclerview 的列表项目对齐问题

List Item aligninment issue with Recycler View

我使用了我的一个项目 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 并成功地向用户展示了项目。

好吧,问题是每当我使用自定义的 Listview 项目时,它都与 Listview 一起工作。但是当我尝试使用 RecyclerView 加载相同内容时,它没有对齐/适合屏幕。

这是我的预期结果。我是通过使用 ListView 实现的。这是我预期的结果

但是当我使用 RecyclerView 时它显示如下。

但我使用相同的 listview_item xml 来加载两个视图..

这是我的 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>

适配器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();
    }
}

哪里出了问题。使用 RecyclerView 有任何类型的限制吗?很想知道这件事。谢谢

而不是像这样设置视图:

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

执行以下操作:

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