1

我在我的應用程序中使用了Recycler視圖和卡片視圖。如何阻止RecyclerView顯示主行下方的空行?

當我設置一定高度的ImageView的它顯示了以下回收站視圖空行。

如何刪除空行?

我強調附着在下面的截圖的問題。

請找到下面的代碼。

Item_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="5dp" 
    card_view:cardCornerRadius="5dp"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <ImageView 
      android:id="@+id/img_row_women" 
      android:layout_width="190dp" 
      android:layout_height="150dp" 
      android:layout_margin="7dp" 
      android:scaleType="fitXY" /> 


     <TextView 
      android:id="@+id/txv_women_name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginLeft="10dp" 
      android:text="" /> 

     <TextView 
      android:id="@+id/txv_women_description" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginLeft="10dp" 
      android:text="" /> 

    </LinearLayout> 


</android.support.v7.widget.CardView> 

<android.support.v7.widget.CardView 

    android:layout_width="match_parent" 
    android:layout_height="275dp" 
    android:layout_margin="5dp" 
    card_view:cardCornerRadius="5dp"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <ImageView 
      android:id="@+id/img_row_men" 
      android:layout_width="190dp" 
      android:layout_height="150dp" 
      android:layout_margin="7dp" 
      android:scaleType="fitXY" /> 


     <TextView 
      android:id="@+id/txv_men_name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginLeft="10dp" 
      android:text="" /> 

     <TextView 
      android:id="@+id/txv_men_description" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginLeft="10dp" 
      android:text="" /> 


    </LinearLayout> 


</android.support.v7.widget.CardView> 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="match_parent" 
android:fillViewport="true" 
android:scrollbars="vertical"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#fff" 

    android:orientation="vertical" 
    android:weightSum="2"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="15dp" 
     android:layout_marginRight="15dp" 
     android:layout_marginTop="15dp" 
     android:layout_weight="1" 
     android:orientation="horizontal"> 


     <TextView 

      android:id="@+id/tab1_women" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Womens" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="#000" 
      android:textSize="20dp" 

      /> 

     <TextView 
      android:id="@+id/tab1_women_viewall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:text="View all" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="#000" 
      android:textSize="20dp" /> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recycleView_women" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginTop="30dp" 
      android:clipToPadding="false" 
      android:minHeight="250dp"> 

     </android.support.v7.widget.RecyclerView> 

    </RelativeLayout> 

    <RelativeLayout 

     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="15dp" 
     android:layout_marginRight="15dp" 
     android:layout_marginTop="15dp" 
     android:orientation="horizontal"> 


     <TextView 

      android:id="@+id/tab1_men" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Mens" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="#000" 
      android:textSize="20dp" 

      /> 

     <TextView 
      android:id="@+id/tab1_men_viewall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:text="View all" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="#000" 
      android:textSize="20dp" /> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recycleView_men" 

      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginTop="30dp" 
      android:minHeight="250dp"> 

     </android.support.v7.widget.RecyclerView> 

    </RelativeLayout> 


</LinearLayout> 

MyAdapter.java

public class MyCustomAdapter_Cardview extends RecyclerView.Adapter<MyCustomAdapter_Cardview.MyViewHolder> { 


Context context; 

ArrayList<RowItem> val; 
RowItem ri = new RowItem(); 

LayoutInflater inflater; 


public MyCustomAdapter_Cardview(Context context, ArrayList<RowItem> val) { 

    this.context = context; 
    this.val = val; 
    inflater = LayoutInflater.from(context); 

} 

@Override 
public MyViewHolder onCreateViewHolder(ViewGroup parent, int position) { 

    View view = inflater.inflate(R.layout.list_item_row, parent, false); 

    MyViewHolder holder = new MyViewHolder(view); 

    return holder; 
} 

@Override 
public void onBindViewHolder(MyViewHolder myViewHolder, final int position) { 

    ri = val.get(position); 
    myViewHolder.textview_women_name.setText(ri.getTitle()); 
    Picasso.with(context).load("" + ri.getImage()).into(myViewHolder.imageView_women); 



} 


@Override 
public int getItemCount() { 
    return val.size(); 
} 


class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 

    TextView textview_women_name, textView_men, textView_des_women, textView_des_men; 
    ImageView imageView_women, imageView_men; 


    public MyViewHolder(View itemView) { 
     super(itemView); 

     textview_women_name = (TextView) itemView.findViewById(R.id.txv_women_name); 
     imageView_women = (ImageView) itemView.findViewById(R.id.img_row_women); 
     textView_des_women = (TextView) itemView.findViewById(R.id.txv_women_description); 

     textView_men = (TextView) itemView.findViewById(R.id.txv_men_name); 
     imageView_men = (ImageView) itemView.findViewById(R.id.img_row_men); 
     textView_des_men = (TextView) itemView.findViewById(R.id.txv_men_description); 


    } 

    @Override 
    public void onClick(View view) { 
     /* Intent intent = new Intent(view.getContext(), Tab1.class); 
     SharedPreferences sharedPreferences = context.getSharedPreferences("Fashion", Context.MODE_PRIVATE); 

     SharedPreferences.Editor sedt = sharedPreferences.edit(); 
     sedt.putString("textvalue", textView_des_women.getText().toString()); 
     sedt.putString("txtopertaive", textview_women_name.getText().toString()); 
     sedt.commit(); 
     view.getContext().startActivity(intent);*/ 

    } 

} 


} 

enter image description here

+0

你爲什麼要使用兩個cardviews在XML中? –

+0

我必須從服務器獲取兩組不同的行。 – Ramz

+0

你想一次顯示一個或兩個? –

回答

1

確定....適配器把這個代碼....

@Override 
public void onBindViewHolder(MyViewHolder myViewHolder, final int position) { 

    myViewHolder.raw2.setVisibility(View.Gone); 
    ri = val.get(position); 
    myViewHolder.textview_women_name.setText(ri.getTitle()); 
    Picasso.with(context).load("" + ri.getImage()).into(myViewHolder.imageView_women); 
} 

class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 

    CardView raw1,raw2; 
    TextView textview_women_name, textView_men, textView_des_women, textView_des_men; 
    ImageView imageView_women, imageView_men; 


    public MyViewHolder(View itemView) { 
     super(itemView); 

     textview_women_name = (TextView) itemView.findViewById(R.id.txv_women_name); 
     imageView_women = (ImageView) itemView.findViewById(R.id.img_row_women); 
     textView_des_women = (TextView) itemView.findViewById(R.id.txv_women_description); 

     textView_men = (TextView) itemView.findViewById(R.id.txv_men_name); 
     imageView_men = (ImageView) itemView.findViewById(R.id.img_row_men); 
     textView_des_men = (TextView) itemView.findViewById(R.id.txv_men_description); 

    raw1 = (CardView) itemview.findViewById(R.id.raw1); 
    raw2 = (CardView) itemview.findViewById(R.id.raw2); 

    } 
+0

或...完全刪除第二行,而不是充氣,然後隱藏它? –