在我的应用程序中,我想在 NestedScrollView
中使用 SwipeRefreshLayout
和 imageView
我写了下面的代码,但是当运行应用程序时,滚动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);
我该如何修复它并使用此 View ?
最佳答案
尽可能避免嵌套滚动。 您可以这样设计布局:
<?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
,因此您可以将适配器的第一个位置用作
标题 View 类型和其余普通 View 类型。
为此,您可以引用this ans
Is there an addHeaderView equivalent for RecyclerView?
对于动态 header ,您可以创建如下方法:
----
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;
}
---
关于java - 如何在android中使用SwipeRefreshLayout和imageView进入NestedScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49104538/