android - Recyclerview 的列表项目对齐问题

标签 android listview adapter android-recyclerview listitem

我使用的项目之一 ListView向用户显示项目列表,它就像一个魅力。现在作为尝试,我用 RecyclerView 替换了 Listview并成功向用户显示项目。

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

这是我的预期结果..我通过使用 ListView 实现了这一点..这是我期待的结果

enter image description here

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

enter image description here

但是我使用相同的 listview_item xml 加载两个 View ..

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

    // 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 有任何类型的限制吗?很想知道这件事。谢谢

最佳答案

而不是像这样设置你的 View :

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

关于android - Recyclerview 的列表项目对齐问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30136793/

相关文章:

java - 与 Instagram 相比,CameraPreview 显示的是放大后的图像

java - onConfigurationChanged 不适用于 API17

android - 以编程方式更改水平进度条的颜色

android - 使用 SimpleAdapter 的 ListView 中来自 URL 的图像

Android - fragment 中的 ListView

android - 我刚刚在 Eclipse 中创建了一个小的 android 项目,我应该将哪些文件添加到版本控制中?

android - 最好使用 Cursor 适配器或 Array 适配器

android - OnItemClickListener 不适用于包含按钮的 ListView 项目

android - Android 中的自定义 ListAdapter 问题?获取 ClassCastException?如何?

android - 如何动态更改适配器布局?