Search code examples
androidandroid-recyclerviewandroid-tablayoutdrawerlayoutswiperefreshlayout

SwipeRefresh not displaying in a fragment with RecyclerView


I'm trying to display a SwipeRefreshLayout in my Fragment. I've built a TabLayout with 3 Tabs(fragment). In the first one there is a RecyclerView. I've tried to implement the SwipeRefreshLayout in CoreActivity and it works. Any suggestions? Is it possible TabLayour overrides the SwipeRefreshLayout?

Here the code:

CoreActivity.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.latlo.firestore.CoreActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.AppCompat.NoActionBar">


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />



    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton5"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="top|left"
        android:layout_margin="16dp"
        android:background="@color/white"
        android:backgroundTint="@color/white"
        android:clickable="true"
        android:src="@drawable/ic_add_black_24dp"
        app:layout_anchor="@+id/viewpager"
        app:layout_anchorGravity="bottom|right" />


     </android.support.design.widget.CoordinatorLayout>


<android.support.design.widget.NavigationView
    android:id="@+id/menulaterale"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/drawer_top"
    app:menu="@menu/drawer_menu"/>


 </android.support.v4.widget.DrawerLayout>

Announces_content_fragment.xml:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:background="?android:attr/selectableItemBackground"
tools:context="chilor.lolmatch.AnnouncesContentFragment">

<ImageView
    android:id="@+id/img"
    android:layout_width="70dp"
    android:layout_height="70dp"
    tools:src="@drawable/diamond_v"
    android:layout_below="@+id/queue_type"
    android:layout_alignParentStart="true" />
<TextView
    android:id="@+id/queue_type"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    tools:text="ciao"
    android:textAppearance="?attr/textAppearanceListItem"
    android:textSize="20dp"
    android:layout_marginStart="36dp"
    android:layout_alignParentTop="true"
    android:layout_toEndOf="@+id/img" />

<Button
    android:id="@+id/button_reply"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Reply"
    android:layout_alignBottom="@+id/img"
    android:layout_alignParentEnd="true"
    style="@style/Widget.AppCompat.Button.Borderless.Colored"
    android:theme="@style/AppTheme.Button"/>

<TextView
    android:id="@+id/time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/queue_type"
    android:layout_centerVertical="true"
    android:text="24/06/17 23:00" />

<TextView
    android:id="@+id/sender"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ADC"
    android:textSize="18sp"
    android:textStyle="bold"
    android:layout_alignBaseline="@+id/button_reply"
    android:layout_alignBottom="@+id/button_reply"
    android:layout_alignStart="@+id/time" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Looking For"
    android:layout_alignBottom="@+id/queue_type"
    android:layout_alignStart="@+id/button_reply" />

<TextView
    android:id="@+id/receiver"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/textView3"
    android:layout_below="@+id/textView3"
    android:text="Support" />

<TextView
    android:id="@+id/summ"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:textStyle="bold" />




</RelativeLayout>

Recycler_view.xml:

<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swiperefresh2"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
            <android.support.v7.widget.RecyclerView 
            xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/my_recycler_view"
                android:clipToPadding="false"
                android:paddingBottom="16dp"
                android:paddingTop="16dp"
                android:scrollbars="vertical"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">
            </android.support.v7.widget.RecyclerView>

    </ScrollView>



</android.support.v4.widget.SwipeRefreshLayout>

Announces_content_fragment.activity:

public class AnnouncesContentFragment extends Fragment implements 
SwipeRefreshLayout.OnRefreshListener {

    private RecyclerView.Adapter adapter;
    private RecyclerView recyclerView;
    private List<Messages> currentItems = new ArrayList<>();
    private String summoner;
    private String rank;
    private FirebaseAuth au = FirebaseAuth.getInstance();
    private FirebaseFirestore d;
    private SwipeRefreshLayout swipeRefreshLayout;
    //public static CurrentProfile current;






    @SuppressLint("ResourceAsColor")
    @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup 
container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(
            R.layout.recycler_view, container, false);
    recyclerView = (RecyclerView)  
    v.findViewById(R.id.my_recycler_view);
    FirebaseFirestore d = FirebaseFirestore.getInstance();


    swipeRefreshLayout = (SwipeRefreshLayout) 
    v.findViewById(R.id.swiperefresh2);
    swipeRefreshLayout.setProgressViewOffset(true, 1500, 1800);



    swipeRefreshLayout.setColorSchemeColors
    (android.R.color.holo_blue_light,
            android.R.color.holo_green_light,
            android.R.color.holo_red_light,
            android.R.color.holo_orange_light);
    swipeRefreshLayout.setOnRefreshListener(this);
    swipeRefreshLayout.post(new Runnable() {
        @Override
        public void run() {
            swipeRefreshLayout.setRefreshing(true);
        }
    });
    try {
        setFirebaseValueListener();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    recyclerView.setAdapter(adapter);
    recyclerView.setHasFixedSize(true);
    recyclerView.setNestedScrollingEnabled(true);
    LinearLayoutManager mm = new LinearLayoutManager(getContext(), 
    LinearLayoutManager.VERTICAL,false);
    recyclerView.setLayoutManager(mm);
    return recyclerView;
}


public void setFirebaseValueListener() throws ExecutionException, 
InterruptedException, JSONException {
    final FirebaseFirestore d = FirebaseFirestore.getInstance();
    NetworkController nc = new NetworkController();
    nc.getMessages(getContext(), new ServerCallback() {
        @Override
        public void onSucces(JSONObject result) {
            try {
                JSONArray array = result.getJSONArray("data");
                for(int i =0;i<array.length();i++){
                    JSONObject obj = array.getJSONObject(i);
                    Messages mes = new 
                    Gson().fromJson(obj.toString(),Messages.class);
                    currentItems.add(mes);
                }
                summoner = "AFKMan";
                adapter = new 
                RecyclerViewAdapter(currentItems,summoner);
                recyclerView.setAdapter(adapter);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });
}

@Override
public void onRefresh() {
    try {
        setFirebaseValueListener();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

Solution

  • With RecyclerView, you don't need to worry about scrollview. Recyclerview automatically puts Scrollview if your list exceeds the limit of Layout.

    Try removing Scrollview this might help.

    And also it looks like you haven't included any layout parent to SwipeRefreshLayout. include LinearLayout or any other layout parent to SwipeRefreshLayout.

    Edit(Solves your problem)

    replace

    return recyclerView;
    

    by

    return v;