java - 如何在android中使用SwipeRefreshLayout和imageView进入NestedScrollView

标签 java android android-recyclerview imageview android-nestedscrollview

在我的应用程序中,我想在 NestedScrollView 中使用 SwipeRefreshLayoutimageView

我写了下面的代码,但是当运行应用程序时,滚动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/

相关文章:

java - 如何让AjaxEditableLabel显示TextField?

java - 为什么 LongStream reduce 和 sum 性能存在差异?

java - 简单 : convertAndSendToUser Where do I get a username?

安卓 C2DM : Duplicate message to the same device and App

android - 单击按钮时向 android 添加动态复选框

android - 为什么 overScrollBy() 和 onOverScrolled() 不能与 RecyclerView 一起使用

java - 使用 Serializable 将数据从 RecyclerView 传递到 RecyclerViewMore - AndroidX

java - 如果没有 String 对象,String 的 indexOf(String obj) 方法如何工作?

java - 以编程方式在 RecyclerView 项目(项目中的某个元素)上设置背景颜色

android - Cordova +安卓 : How to stop shrinking of screen if virtual keyboard is displayed?