[英]SwipeRefreshLayout + NestedScrollView: Data cannot be refreshed
[英]How to use SwipeRefreshLayout and imageView into NestedScrollView in android
在我的應用程序要使用SwipeRefreshLayout
和imageView
到NestedScrollView
我寫下面的代碼,但是當運行應用程序時,滾動RecyclerView
時顯示很多錯誤,而我沒有滾動項目 。
我的xml代碼:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:orientation="vertical">
<include layout="@layout/banner_layout" />
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
我的Java代碼:
list.setLayoutManager(linearLayoutManager);
list.setHasFixedSize(true);
list.setNestedScrollingEnabled(false);
list.setAdapter(todayRecyclerAdapter);
我該如何修復並使用此視圖?
盡可能避免嵌套滾動。 您可以這樣設計布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
您可以像這樣定義適配器的數據
.............
List<Object> objects=new ArrayList<>()
object.add(new ImageData('Image url'))
object.add(new Data('title','subtitle','date'))
.............
由於RecylerView
適配器支持不同的viewType
您可以將適配器的第一個位置用作標題視圖類型,而將其用作普通視圖類型。
為此,您可以參考ans。RecyclerView是否有一個addHeaderView等效項?
對於動態標頭,您可以創建如下方法:
----
setHeaderAvailable(boolean isHeaderAvailable){
this.isHeaderAvailable=isHeaderAvailable
}
----
適配器數據將如下所示:
---
List<Object> objects=new ArrayList<>()
if(imageUrl!=null){
adapter.setHeaderAvailable(true)
object.add(new ImageData('Image url'))
}else{
adapter.setHeaderAvailable(false)
}
object.add(new Data('title','subtitle','date'))
--
並更新getItemViewType
方法,如下所示:
----
@Override
public int getItemViewType(int position) {
if (isPositionHeader(position) && isHeaderAvailable)
return TYPE_HEADER;
return TYPE_ITEM;
}
---
用這個替換您的RecyclerView。
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="200dp" />
您已經在setAdapter
之前設置了此屬性
recyclerView.setNestedScrollingEnabled(false);
<CoordinatorLayout>
<SwipeRefreshLayout>
<RecyclerView>
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<Appbarlayout>
<Toolbar>
app:layout_scrollFlags="scroll|snap"/>
<Relative Layout>
app:layout_scrollFlags="scroll|snap"/>
<ImageView>
</Relative Layout>
</Appbarlayout>
</SwipeRefreshLayout>
</CoordinatorLayout>
Please try in these way it will work.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.