0

In my application i want use SwipeRefreshLayout and imageView into NestedScrollView

I write below codes, but when run application show me many lag when scroll RecyclerView and i'm not scroll items.

My xml code :

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

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

How can i fix it and use this views?

Dr ckeck
  • 23
  • 4
  • try to give linear layout some hardcoded height like 200 dp.. – Abhinay Sharma Mar 05 '18 at 06:21
  • @abhi20392, can you send to me code with my codes? – Dr ckeck Mar 05 '18 at 06:22
  • see my answer and tell me if it working for you or not – Abhinay Sharma Mar 05 '18 at 06:31
  • try with `linearLayoutManager.setAutoMeasureEnabled(true)` – Avinash Kumar Mar 05 '18 at 06:44
  • @AvinashKumar, not work me :( please help me – Dr ckeck Mar 05 '18 at 06:48
  • why are you using NestedScrollView it can be done with RecylerView only using viewType . And place RecylerView inside SwipeRefreshLayout – Rahul Mar 05 '18 at 07:19
  • your codes are fine, check if you have **recyclerview single item with big image or others problems...** – Avinash Kumar Mar 05 '18 at 07:19
  • @Rahul, I want show imageView above of recyclerView and when scroll recyclerView items, scroll this imageView. how can i it? – Dr ckeck Mar 05 '18 at 07:21
  • @AvinashKumar, I want show imageView above of recyclerView and when scroll recyclerView items, scroll this imageView. how can i it? – Dr ckeck Mar 05 '18 at 07:21
  • @Drckeck refer this ans https://stackoverflow.com/a/26573338/6382210. you need to add header to recylerView that's it – Rahul Mar 05 '18 at 07:23
  • @Rahul, thanks dear rahul. but i should fill recyclerview with one api and i should fill imageView with another api! how can i it? i can fill recyclerView header with another Api? !! – Dr ckeck Mar 05 '18 at 07:28
  • @Drckeck you can combine both api data into ArrayList the zero position of list can be header itself rest is your data – Rahul Mar 05 '18 at 07:31
  • @Rahul, can you send to me code with your answer ? thanks my friend – Dr ckeck Mar 05 '18 at 07:35
  • @Drckeck You do not have to change your code as i said it is working like charm, Have a look of [SOURCE](https://drive.google.com/file/d/1ozBoX9Yl-oDyusVfL23X0k6VtrphPABq/view?usp=sharing) and [APK](https://drive.google.com/file/d/1LATKgF5xT_JB6GDAJtVvX90z8WQuZHsb/view?usp=sharing) – Avinash Kumar Mar 05 '18 at 07:37

4 Answers4

3

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 viewTypes you can use first position of adapter as header view type and rest normal view type.
For that you can refer to this ans Is there an addHeaderView equivalent for RecyclerView?
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:

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

        return TYPE_ITEM;
 }
---
Rahul
  • 408
  • 5
  • 13
0

replace your RecyclerView with this one..

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

You have set this property before setAdapter

recyclerView.setNestedScrollingEnabled(false);
jake oliver
  • 118
  • 1
  • 6
  • Thanks , but first see my post. i say add this code in java : `list.setNestedScrollingEnabled(false);` `list.setAdapter(todayRecyclerAdapter);` and again not work me – Dr ckeck Mar 05 '18 at 06:41
  • Alright. do like this.and let me know. 1). fix the height of `recyclerview` 2). use `ScrollVIew` instead of `NestedScrollView`. – jake oliver Mar 05 '18 at 06:46
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.
tushar
  • 56
  • 3
  • 14
  • I want show imageView above of recyclerView and when scroll recyclerView items, scroll this imageView. how can i it? – Dr ckeck Mar 05 '18 at 07:18