当列表大小为零时,SwipeRefreshLayout 不会在滑动时刷新

SwipeRefreshLayout doesn't refresh on swipe when list size is zero

我正在为我的 recyclerview.I 使用 SwipeRefreshLayout,我通过 api 调用在 recyclerview 中显示数据,我在滑动 refresh.The 问题上调用 api 我是具有的是,如果列表大小为零,则滑动刷新不是 working.I 想要 api 调用滑动刷新,即使列表大小为零,以便在有任何新数据时更新列表。

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

代码

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

适配器

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

        }
    }
}

根据答案更新..

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

代码

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

更改 RelativeLayout 子视图的顺序。在没有列表项的情况下,您的顶部视图是 TextView,因此它阻止了 SwipeRefreshLayout。更改顺序应该有效,因为视图的 Z 顺序是由添加视图的顺序定义的。检查 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>

就像你说的,你的列表是空的,所以没有 VIew 滑动,你需要像 listView.setEmptyView() 添加一个 emptyView。但是 RecyclerView 不存在 setEmptyView() 所以只要按照这个 post :

这对我有用。

所以你 XML 会像 :

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

编辑: 当列表为空时

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