我正在使用SwipeRefreshLayout进行我的recyclerview。我通过进行api调用在recyclerview中显示数据,我在刷卡刷新时调用api。我遇到的问题是,如果列表大小为零,则刷卡刷新不起作用。即使列表大小为零,我想在刷卡刷新时进行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);
}
});
}
答案 0 :(得分:4)
就像你说的那样,你的列表是空的,所以没有VIew
可以滑动,你需要添加一个像listView.setEmptyView()
一样的空视图。但由于RecyclerView
没有setEmptyView()
存在,所以请按照以下文章进行操作:
https://stackoverflow.com/a/30415582/4854450
这对我有用。
所以你的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;
}
}
答案 1 :(得分:0)
更改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>