2017-09-16 217 views
-4

我正在使用recyclerview顯示來自Web服務的數據。基於特定的條件,我需要刪除特定的回收商查看項目。所以我使用View.Gone。但它顯示出空虛的空間。我搜索了這個問題,並將高度設置爲wrapcontent,但它不起作用。請任何人幫助我。View.gone在Recyclerview中顯示空白空間android

我的代碼: XML:項目

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/businfo" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:cardCornerRadius="4dp" 
     app:contentPaddingBottom="10dp" 
     app:contentPaddingTop="16dp" 
     android:elevation="10dp" 
     android:layout_margin="5dp" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true"> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="2" 
     android:orientation="horizontal"> 
     <TextView 
      android:id="@+id/travel_name" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:textSize="14sp" 
      android:layout_marginLeft="5dp" 
      android:textColor="@color/holo_blue_light" 
      android:text="CGR Travels"/> 


     <TextView 
      android:id="@+id/bus_price" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="right" 
      android:textSize="14sp" 
      android:textColor="#F04C3B" 
      android:layout_marginRight="30dp" 
      android:text="Rs.349"/> 


    </LinearLayout> 
    <View 
     android:layout_width="match_parent" 
     android:layout_height="0.5dp" 
     android:layout_marginTop="5dp" 
     android:background="#b6b6b6"/> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="2" 
     android:layout_marginTop="5dp" 
     android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/arrtime_desttime" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_marginLeft="5dp" 
      android:textSize="15sp" 
      android:textColor="#1e356a" 
      android:text="9.00P.M - 7.00AM"/> 
     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="right" 
      android:textSize="12sp" 

      android:layout_marginRight="30dp" 
      android:text="38 seats" 
      android:id="@+id/seatcount" /> 




    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="2" 
     android:layout_marginTop="5dp" 
     android:orientation="horizontal"> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:textSize="12sp" 
      android:layout_marginLeft="5dp" 
      android:text="2+1 semi sleeeper" 
      android:id="@+id/bustype" /> 
     <TextView 
      android:id="@+id/cancellationpolicy" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="right" 
      android:textSize="12sp" 
      android:textColor="@color/holo_blue_light" 
      android:layout_marginRight="30dp" 
      android:text="Cancellation policy" 
      /> 


     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:visibility="gone" 
      android:id="@+id/routeid"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:visibility="gone" 
      android:id="@+id/dropdate"/> 


    </LinearLayout> 

</LinearLayout> 

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

的java:

@Override 
     public void onBindViewHolder(final BusrouteAdapter.ViewHolder viewHolder, final int position) { 
if ((bus_routes.get(position).getAvailableSeats() == 0)) { 
       viewHolder.businfo.setVisibility(View.GONE); 


      } else { 
       viewHolder.businfo.setVisibility(View.VISIBLE); 

      } 
} 
+0

發表您的觀點持有者 –

+0

請看看這個答案https://stackoverflow.com/a/40710448/3023833 –

+0

@MostafaAnter java.lang.IllegalStateException:當RecyclerView正在計算佈局或滾動 –

回答

0

您應該從bus_routes刪除該項:

bus_routes.remove(position); 

然後:

notifyItemRemoved(position); 
notifyItemRangeChanged(position, bus_routes.size()); 
0

您不能以這種方式直接隱藏視圖。您必須從ArrayList中刪除不需要的項目,或者在設置適配器之前不要添加不需要的項目。如果你的產品有更新,然後再刪除不需要的項目,並呼籲notifydataset改變

0

從ArrayList中刪除此元素,並使用notifyDataSetChanged();

例子:

holder.imageDelete.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      paths.remove(position); 
      notifyDataSetChanged(); 
     } 
    }); 
+0

不能調用此方法它顯示多少空間是一個像一個行文件還是小於那個? @kartheeki j –

+0

你的問題解決了嗎?@kartheeki j –

0

你不需要設置可見或剛剛經歷從列表中刪除項目和比更新列表。

@Override 
    public void onBindViewHolder(final BusrouteAdapter.ViewHolder viewHolder, final int position) { 
    if ((bus_routes.get(position).getAvailableSeats() == 0)) { 
      bus_routes.remove(position); 

     } 
    } 

嘗試從活動組適配器後打電話到youradapter.notifydatasetchanged()recyclerview

+0

java.lang.IllegalStateException:當RecyclerView正在計算佈局或滾動時,無法調用此方法 at –

+0

我更新了我的答案。請檢查並告訴我是否有錯誤發生, –