我正在尝试使用SwipeRefreshLayout来刷新片段中的recyclerview上的JSON数据。我试过了,但结果是数据在刷卡刷新之前没有出现,如果我刷新布局,数据就会循环播放。
这是我的代码:
Fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/container"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</android.support.v4.widget.SwipeRefreshLayout>
Fragment1.java
public class Fragment1 extends Fragment{
private SwipeRefreshLayout mSwipeRefreshLayout;
private RecyclerView recyclerView;
private DataAdapter adapter;
private View myFragmentView;
private String TAG = Tab1.class.getSimpleName();
private ProgressDialog pDialog;
// URL to get contacts JSON
private static String url = "http://sayangoppa.com/ibn/jadwal.php";
List<jsonContent> listcontent=new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.Fragment1, container, false);
//SwipeRefreshLayout begin
mSwipeRefreshLayout = (SwipeRefreshLayout) myFragmentView.findViewById(R.id.container);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener(){
@Override
public void onRefresh(){
new GetContacts().execute();
mSwipeRefreshLayout.setRefreshing(false);
}
});
return myFragmentView;
}
答案 0 :(得分:0)
结果是数据在刷卡刷新
之前不会出现
在滑动之前,你永远不会执行Asynctask ...
例如
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener(){
@Override
public void onRefresh(){
listcontent.clear(); // prevent repeating data
new GetContacts().execute();
mSwipeRefreshLayout.setRefreshing(false);
}
});
new GetContacts().execute(); // load before refresh
return myFragmentView;
}
如果我刷新布局,数据将循环播放。
你的意思是重复?您需要在添加新数据之前致电listcontent.clear()