2015-04-06 79 views
0

我正在使用recyclerView(TwowayView),當數據發生變化時,它的行爲很奇怪(在滾動中)。我正在使用以下代碼來設置適配器(每次數據更改時)。RecyclerView當數據集發生變化時奇怪的滾動

// sites = list , rvlinks = twowayview 

    adapter = new SitesAdapter(getActivity(), rvLinks, LinksFragment.this, sites); 
    rvLinks.setAdapter(adapter); 

爲了更好的理解,我製作了錯誤視頻。

https://youtu.be/xPvHper3gpc

如所看到的,是recyclerview具有大的頂部和底部區域(參見圖像),其具有僅列出2項的inspite。

僅使用notifyDatasetChanged()會產生相同的結果。陷入這個。

Recyclerview具有固定大小的設置。

rvLinks.setHasFixedSize(true); 

任何幫助將是偉大的。

enter image description here

這裏是我的XML佈局:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@+id/dummyview" 
    android:layout_below="@+id/sp_category" 
    android:layout_marginTop="15dp" 
    android:background="@drawable/white_round"> 

    <Widget.EmptyRecyclerView 
    android:id="@+id/rv_links" 
    android:layout_width="match_parent" 
    android:layout_height="300dp" 
    android:drawSelectorOnTop="false" 

    android:orientation="vertical" 
    app:twowayview_layoutManager="GridLayoutManager" 
    app:twowayview_numColumns="2" 

    tools:context=".MainActivity" /> 



    <TextView 
     android:id="@+id/tv_empty" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:text="No Sites Found" /> 

</RelativeLayout> 

這是我與執行emptyView的RecyclerView。

public class EmptyRecyclerView extends TwoWayView { 
    private View emptyView; 
    final private AdapterDataObserver observer = new AdapterDataObserver() { 
     @Override 
     public void onChanged() { 
      checkIfEmpty(); 
     } 

     @Override 
     public void onItemRangeInserted(int positionStart, int itemCount) { 
      checkIfEmpty(); 
     } 

     @Override 
     public void onItemRangeRemoved(int positionStart, int itemCount) { 
      checkIfEmpty(); 
     } 
    }; 

    public EmptyRecyclerView(Context context) { 
     super(context); 
    } 

    public EmptyRecyclerView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public EmptyRecyclerView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    void checkIfEmpty() { 
     if (emptyView != null && getAdapter() != null) { 
      final boolean emptyViewVisible = getAdapter().getItemCount() == 0; 
      emptyView.setVisibility(emptyViewVisible ? VISIBLE : GONE); 
      setVisibility(emptyViewVisible ? GONE : VISIBLE); 
     } 
    } 

    @Override 
    public void setAdapter(Adapter adapter) { 
     final Adapter oldAdapter = getAdapter(); 
     if (oldAdapter != null) { 
      oldAdapter.unregisterAdapterDataObserver(observer); 
     } 
     super.setAdapter(adapter); 
     if (adapter != null) { 
      adapter.registerAdapterDataObserver(observer); 
     } 

     checkIfEmpty(); 
    } 

    public void setEmptyView(View emptyView) { 
     this.emptyView = emptyView; 
     checkIfEmpty(); 
    } 
} 
+0

你有沒有爲recyclerview指定任何尺寸?有一個錯誤,您無法使用recyclerview的「wrap_content」來根據其內容https://code.google.com/p/android/issues/detail?id=74772來確定視圖的大小。所以假設你指定的高度可能是match_parent或者某個固定的dp值 – random 2015-04-06 10:49:52

+0

我已經將我的xml添加到了問題中。見編輯。 – 2015-04-06 10:54:35

+0

不要將EmptyRecyclerView高度設置爲match_parent,而應首先嚐試將dp值設置爲接近預期高度 – random 2015-04-06 10:59:46

回答

0

至於建議由@random我試圖match_parent但TwoWayView同樣的問題。所以我決定在support-v7 lib中將其更改爲RecyclerView

令人驚訝的是,它按預期工作。可能是它在自己的GridLayoutManager中做了一些奇特的工作,導致了這個問題。 recyclerview的GridlayoutManager效果很好。

感謝您的幫助。 :)

1

嘗試設置一個精確的高度爲您recyclerview,因爲它不尊重WRAP_CONTENT和設置match_parent將您的ListView下面的整個空間的高度

一旦你開始看到的結果,你可以使用自定義您的recyclerview佈局管理器,覆蓋onMeasure()。

檢查這個爲例

Nested Recycler view height doesn't wrap its content

+0

我將其更改爲recyclerview,並按預期工作。 Thnq。 :) – 2015-04-07 09:13:42

相關問題