0

1.i只是實現了一些recyclerviews,其中一些項目有額外的空白空間。這個空間有時會在滾動後消失。我已經盡力想辦法了!我改變了高度來包裝內容,但仍存在問題!RecyclerView項目之間額外的空白空間

這裏是截圖: Recyclerview empty space

這裏項XML:

<android.support.v7.widget.CardView 
    android:id="@+id/btn" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#ffffff" 
    android:gravity="center" 
    android:orientation="horizontal" 
    android:adjustViewBounds="true" 
    android:padding="8dp" 

    android:layout_margin="6dp" 

    > 

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

     <ImageView 
      android:id="@+id/banner_image" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:adjustViewBounds="true" 
      android:src="@drawable/no_image_banner" /> 

     <TextView 
      android:id="@+id/txtHey" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="4dp" 
      android:gravity="center" 
      android:maxLength="20" 
      android:text="sadas" 
      android:textColor="#4d4a46" 
      android:textSize="11dp" 

      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/txtHi" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="2dp" 
      android:gravity="center" 
      android:text="sadas" 
      android:textColor="#999999" 
      android:textSize="9dp" /> 

     <FrameLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:gravity="center_vertical"> 

      <TextView 
       android:id="@+id/txtPrice" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center" 
       android:text="sadas" 
       android:textColor="#888888" 
       android:textSize="9dp" /> 

      <LinearLayout 
       android:id="@+id/price_cancel" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="6dp" 
       android:background="#888888" 
       android:orientation="horizontal" /> 
     </FrameLayout> 

     <TextView 
      android:id="@+id/txtFinalPrice" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="sadas" 

      android:textColor="#3dc24d" 
      android:textSize="9dp" /> 

     <TextView 
      android:id="@+id/txtHello" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" 
      android:text="hey" 
      android:textSize="9dp" 
      android:visibility="gone" /> 
    </LinearLayout> 
</android.support.v7.widget.CardView> 

主要頁面的XML:

<android.support.design.widget.AppBarLayout 
    android:id="@+id/topBar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <include 
     layout="@layout/toolbar_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</android.support.design.widget.AppBarLayout> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/product_list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="6dp" 
     > 

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

    <TextView 
     android:id="@+id/request_results" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:textSize="21dp" 
     android:textStyle="bold" /> 
</RelativeLayout> 

  1. 第二個問題是我在嵌套滾動視圖中使用了一些recyclerviews來防止滾動問題。一切都很好,但在android的低版本像kitkat我有滾動問題(導致嵌套不會工作在sdk < 21)。我可以使用正常的滾動視圖,這兩個版本都能很好地工作,但正常的滾動視圖不適用於協調器佈局,所以appbar佈局隱藏行爲不會工作!

你能幫我解決這些問題嗎?

這裏是適配器類:

private ArrayList<Struct> structs; 
OnItemListener onItemListener; 
private boolean isGrid; 
private int Tab; 

public Adapter_Recycler(ArrayList<Struct> structs, OnItemListener onItemListener, int Tab, boolean isGrid) { 
    this.structs = structs; 
    this.onItemListener = onItemListener; 
    this.Tab = Tab; 
    this.isGrid = isGrid; 
} 


@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view = null; 


    if (Tab == 2) { 
     view = inflater.inflate(R.layout.item_product_color, parent, false); 
    } 
    if (Tab == 1 && isGrid == false) { 
     view = inflater.inflate(R.layout.item_product_list, parent, false); 
    } 
    if (Tab == 1 && isGrid) { 
     view = inflater.inflate(R.layout.item_product_list, parent, false); 
    } 

    ViewHolder viewHolder = new ViewHolder(view); 
    return viewHolder; 
} 

@Override 
public void onBindViewHolder(ViewHolder holder, final int position) { 
    if (Tab == 2) { 
     holder.txtHey.setText(structs.get(position).hey); 
     holder.txtPrice.setText(structs.get(position).hi); 
     holder.txtHi.setVisibility(View.GONE); 
     holder.PriceCancel.setVisibility(View.GONE); 
     holder.txtFinalPrice.setVisibility(View.GONE); 
     holder.txtHey.setTypeface(Activity_Main.SANS); 
     holder.txtPrice.setTypeface(Activity_Main.SANS); 
     Glide 
       .with(Activity_Main.CONTEXT) 
       .load(structs.get(position).image) 
       .placeholder(R.drawable.background_card) 
       .fitCenter() 
       .into(holder.banner_image); 
    } 

    if (Tab == 1) { 
     holder.txtHey.setText(structs.get(position).hey); 
     holder.txtHi.setText(structs.get(position).hi); 
     holder.txtPrice.setText(structs.get(position).price); 
     holder.txtFinalPrice.setText(structs.get(position).final_price); 
     if (holder.txtFinalPrice.getText().toString() == "") { 
      holder.PriceCancel.setVisibility(View.GONE); 
     } else { 
      holder.PriceCancel.setVisibility(View.VISIBLE); 
     } 
     holder.txtHey.setTypeface(Activity_Main.SANS); 
     holder.txtHi.setTypeface(Activity_Main.SANS); 
     holder.txtPrice.setTypeface(Activity_Main.SANS); 
     holder.txtFinalPrice.setTypeface(Activity_Main.SANS); 
     Glide 
       .with(Activity_Main.CONTEXT) 
       .load(structs.get(position).image) 
       .placeholder(R.drawable.background_card) 
       .fitCenter() 
       .into(holder.banner_image); 
    } 
    holder.linearLayout.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (onItemListener != null) { 
       onItemListener.onItemSelect(position); 
      } 
     } 
    }); 

    holder.txtHello.setText(structs.get(position).hello); 
    holder.txtHello.setTypeface(Activity_Main.SANS); 


} 


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


public static class ViewHolder extends RecyclerView.ViewHolder { 

    CardView linearLayout; 
    ImageView banner_image; 
    TextView txtHey; 
    TextView txtHi; 
    TextView txtHello; 
    TextView txtPrice; 
    TextView txtFinalPrice; 
    LinearLayout PriceCancel; 

    public ViewHolder(View itemView) { 
     super(itemView); 
     linearLayout = (CardView) itemView.findViewById(R.id.btn); 
     banner_image = (ImageView) itemView.findViewById(R.id.banner_image); 
     txtHey = (TextView) itemView.findViewById(R.id.txtHey); 
     txtHi = (TextView) itemView.findViewById(R.id.txtHi); 
     txtHello = (TextView) itemView.findViewById(R.id.txtHello); 
     txtPrice = (TextView) itemView.findViewById(R.id.txtPrice); 
     txtFinalPrice = (TextView) itemView.findViewById(R.id.txtFinalPrice); 
     PriceCancel = (LinearLayout) itemView.findViewById(R.id.price_cancel); 
    } 
} 

}

+0

我看到你已經爲回收物品添加了xml,請添加用於添加RecyclerView的地方以及適配器。同時顯示如何初始化'RecyclerView',即設置其'LayoutManager','Adapter'等的位置。並嘗試分別陳述你面臨的問題和更好的結果。 – Abbas

+0

fyi:這些屬性不適用於cardview android:orientation =「horizo​​ntal」 android:adjustViewBounds =「true」 android:padding =「8dp」android:background =「#ffffff」 ' – k0sh

+0

ok,但那不是問題! 我只是將cardview更改爲線性佈局,反之亦然,這些額外的屬性因此而出現! @ k0sh –

回答

0

我終於明白了問題所在! 多餘空間是因爲
.placeholder(R.drawable.background_card)」!

移除佔位符後問題解決。

當然問題是與圖像!不佔位符。

0

如果可能的話,然後提供適配器類,因爲可能會出現一些問題與onCreateViewHolder與LayoutInflater。

+0

我添加了適配器類 –