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

问题描述 投票:0回答:4

在我的应用程序中,我想使用SwipeRefreshLayoutimageView进入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);

我该如何解决它并使用此视图?

java android android-recyclerview imageview android-nestedscrollview
4个回答
3
投票

尽可能避免嵌套滚动。您可以设计如下布局:

<?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适配器支持不同的viewTypes,因此您可以使用适配器的第一个位置作为标题视图类型并使用普通视图类型。 为此你可以参考这个ans Is there an addHeaderView equivalent for RecyclerView? 对于动态标头,您可以创建一个方法,如:

----
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;
 }
---

0
投票

用这个替换您的RecyclerView ..

<android.support.v7.widget.RecyclerView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="200dp" />

0
投票

您已在setAdapter之前设置此属性

recyclerView.setNestedScrollingEnabled(false);

0
投票
   <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.
© www.soinside.com 2019 - 2024. All rights reserved.