简体   繁体   English

如何在Android中将SwipeRefreshLayout和imageView使用到NestedScrollView中

[英]How to use SwipeRefreshLayout and imageView into NestedScrollView in android

In my application i want use SwipeRefreshLayout and imageView into NestedScrollView 在我的应用程序要使用SwipeRefreshLayoutimageViewNestedScrollView

I write below codes, but when run application show me many la g when scroll RecyclerView and i'm not scroll items . 我写下面的代码,但是当运行应用程序时,滚动RecyclerView时显示很多错误,我没有滚动项​​目

My xml code : 我的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>

My java codes: 我的Java代码:

list.setLayoutManager(linearLayoutManager);
list.setHasFixedSize(true);
list.setNestedScrollingEnabled(false);
list.setAdapter(todayRecyclerAdapter);

How can i fix it and use this views? 我该如何修复并使用此视图?

Avoid nested scrolling whenever possible . 尽可能避免嵌套滚动。 You can design layout like this: 您可以这样设计布局:

<?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>

You can define data of adapter like this 您可以像这样定义适配器的数据

.............
List<Object> objects=new ArrayList<>()
object.add(new ImageData('Image url'))
object.add(new Data('title','subtitle','date'))
.............

Since RecylerView adapter supports different viewType s you can use first position of adapter as header view type and rest normal view type. 由于RecylerView适配器支持不同的viewType您可以将适配器的第一个位置用作标题视图类型,而将其用作普通视图类型。
For that you can refer to this ans Is there an addHeaderView equivalent for RecyclerView? 为此,您可以参考ans。RecyclerView是否有一个addHeaderView等效项?
For a dynamic header, you can create a method like : 对于动态标头,您可以创建如下方法:

----
setHeaderAvailable(boolean isHeaderAvailable){
  this.isHeaderAvailable=isHeaderAvailable
}
----

and adapter data will look like this: 适配器数据将如下所示:

---
  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'))
--

and update getItemViewType method like this: 并更新getItemViewType方法,如下所示:

----
@Override
public int getItemViewType(int position) {
     if (isPositionHeader(position) && isHeaderAvailable)
            return TYPE_HEADER;

        return TYPE_ITEM;
 }
---

replace your RecyclerView with this one.. 用这个替换您的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM