1

I am using SwipeRefreshLayout for my recyclerview.I am showing data in recyclerview by making an api call and i am calling that api on swipe refresh.The issue I am having is that if list size is zero than swipe refresh is not working.I want to make api call on swipe refresh even when list size is zero so that list gets updated if any new data is there.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swiperefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/datalist"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/homebackground"
            android:clipToPadding="false"

            android:paddingBottom="8dp"

            />



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

    <TextView
        android:id="@+id/tvNoData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="No orders yet"
        android:textSize="20sp"
        android:visibility="gone" />
</RelativeLayout>

Code

   swiperefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                callLoginApi();
            }
        });
 private void callLoginApi() {

    APICALL(in, new Callback<Orssaf>() {
            @Override
            public void success(Result<Orssaf> result) {
                swiperefresh.setRefreshing(false);

                }
            }
            @Override
            public void failure(TwitterException e) {

                swiperefresh.setRefreshing(false);
            }
        });
    }

Adapter

public class OrderHistoryAdapter extends RecyclerView.Adapter<OrderHistoryAdapter.MyViewHolder> {
    private Context mContext;
    private ArrayList<OrderDetailsData> orders;
    public OrderHistoryAdapter(Context context, ArrayList<OrderDetailsData> order) {
        mContext = context;
        orders = order;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_foodvitecurrentorder, parent, false);
        return new MyViewHolder(itemView);
    }
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        OrderDetailsData order = orders.get(position);
        Picasso.with(mContext).load(order.getSenderImage()).placeholder(R.drawable.profile_img).transform(new CircleTransform()).into(holder.profileimage);
        holder.itemscount.setText("dfg"));
        holder.pricecount.setText("$fsdf"));
        holder.ordertime.setText("sdgsdg");
        holder.message.setText("greg");

        }
    }
    @Override
    public int getItemCount() {
        return orders.size();
    }
    public class MyViewHolder extends RecyclerView.ViewHolder {
        private CircularImageView profileimage;
        private TextView status, message, itemscount, pricecount, ordertime
        public MyViewHolder(View itemView) {
            super(itemView);
            profileimage = (CircularImageView) itemView.findViewById(R.id.profileimage);
            status = (TextView) itemView.findViewById(R.id.status);
            message = (TextView) itemView.findViewById(R.id.message);
            itemscount = (TextView) itemView.findViewById(R.id.itemscount);
            pricecount = (TextView) itemView.findViewById(R.id.pricecount);
            ordertime = (TextView) itemView.findViewById(R.id.ordertime);

        }
    }
}

UPDATED according to answer..

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swiperefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.foodviteseller.co.foodviteseller.views.RecyclerViewEmptySupport
            android:id="@+id/datalist"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/homebackground"
            android:clipToPadding="false"

            android:paddingBottom="8dp"

            />
        <TextView
            android:id="@+id/emptyView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:text="EmptyList"
            android:textSize="20sp"
            android:visibility="gone" />

        <!--<include layout="@layout/no_data_layout" />-->

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

    <TextView
        android:id="@+id/tvNoData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="No orders yet"
        android:textSize="20sp"
        android:visibility="gone" />
</RelativeLayout>

Code

  datalist = (RecyclerViewEmptySupport) v.findViewById(R.id.datalist);

 private void callLoginApi() {

    APICALL(in, new Callback<Orssaf>() {
            @Override
            public void success(Result<Orssaf> result) {
                swiperefresh.setRefreshing(false);
              if (orders.size() > 0) {
    orderAdapter = new OrderHistoryAdapter(mContext, orders);
    datalist.setAdapter(orderAdapter);
} else {
    emptyView.setVisibility(View.VISIBLE);
   datalist.setEmptyView(emptyView);
    tvNoData.setVisibility(View.VISIBLE);
    tvNoData.setText("No orders yet");
}

                }
            }
            @Override
            public void failure(TwitterException e) {

                swiperefresh.setRefreshing(false);
            }
        });
    }
Android Developer
  • 9,157
  • 18
  • 82
  • 139

2 Answers2

4

It's, like you said, you list is empty, so no VIew to swipe, you need to add an emptyView like with listView.setEmptyView(). But with the RecyclerView no setEmptyView() exists so just follow this post :

https://stackoverflow.com/a/30415582/4854450

That's work for me.

So you XML gona be like :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swiperefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/datalist"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/homebackground"
            android:clipToPadding="false"
            android:paddingBottom="8dp"/>

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

    <TextView
        android:id="@+id/tvNoData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="No orders yet"
        android:textSize="20sp"
        android:visibility="gone" />
</RelativeLayout>

EDIT: when list is empty

    EmptyAdapter adapter = new EmptyAdapter();
    datalist.setAdapter(adapter);


public class EmptyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return 0;
    }
}
Community
  • 1
  • 1
Raphael Teyssandier
  • 1,722
  • 2
  • 13
  • 25
0

Change the order of your RelativeLayout subviews. Your top view in -case of no list items- is the TextView, so it's blocking the SwipeRefreshLayout. Changing the order should work because of the Z-ordering of the views is defined by the order the views are added. check this answer

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/tvNoData"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="No orders yet"
    android:textSize="20sp"
    android:visibility="gone" />

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swiperefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/datalist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/homebackground"
        android:clipToPadding="false"

        android:paddingBottom="8dp"

        />



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


</RelativeLayout>
Community
  • 1
  • 1
Abdallah Alaraby
  • 2,222
  • 2
  • 18
  • 30