2015-05-09 75 views
4

我的项目之一,我用ListView向用户显示项目列表,它像一个魅力。现在,我试着用RecyclerView替换Listview,并成功向用户显示这些项目。列表项目与Recycler视图对齐问题

那么问题是,当我使用一个自定义Listview项它与Listview一起工作。但是,当我尝试加载与RecyclerView相同,它不是对齐/适合屏幕。

这里是我的预期结果。我使用的ListView实现这个..以下是我期待

enter image description here

的结果,但是当我使用RecyclerView它下面显示。

enter image description here

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

这里是我的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有什么限制吗?很想知道这件事。由于

+0

你可以发表你的RecyclerView XML –

+0

我认为有问题与您的适配器 –

+0

我更新我的适配器班也@Nilesh –

回答

9

而不是设置您的看法是这样的:

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

它的工作:)接受答案。 –

+1

谢谢移动领导.....它也帮助我:) –