Java RecyclerView中的JSON数据不';在刷卡刷新和循环之前不显示
我正在尝试使用SwipeRefreshLayout刷新片段中recyclerview上JSON的数据。我尝试过,但结果是数据在刷刷新之前不会出现,如果我刷新布局,数据将循环 这是我的密码: Fragment1.xmlJava RecyclerView中的JSON数据不';在刷卡刷新和循环之前不显示,java,android,json,android-fragments,Java,Android,Json,Android Fragments,我正在尝试使用SwipeRefreshLayout刷新片段中recyclerview上JSON的数据。我尝试过,但结果是数据在刷刷新之前不会出现,如果我刷新布局,数据将循环 这是我的密码: Fragment1.xml <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/container" xmlns:andro
<?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;
}
公共类Fragment1扩展了Fragment{
私人SwipeRefreshLayout mSwipeRefreshLayout;
私人回收站;
专用数据适配器;
私有视图myFragmentView;
私有字符串标记=Tab1.class.getSimpleName();
私人对话;
//获取联系人JSON的URL
专用静态字符串url=”http://sayangoppa.com/ibn/jadwal.php";
List listcontent=new ArrayList();
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
myFragmentView=充气机。充气(R.layout.Fragment1,容器,错误);
//Switch开始布局
mSwipeRefreshLayout=(SwipeRefreshLayout)myFragmentView.findViewById(R.id.container);
mSwipeRefreshLayout.setOnRefreshListener(新的SwipeRefreshLayout.OnRefreshListener()){
@凌驾
公共void onRefresh(){
新建GetContacts().execute();
mSwipeRefreshLayout.setRefresh(false);
}
});
返回myFragmentView;
}
结果是数据在刷卡刷新之前不会出现
您从未在刷卡之前执行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()
在您添加新数据之前您的问题的哪一部分使用了JSON数据?我看到了一个URL,但没有网络操作我正在尝试在片段中使用SwiperFreshLayout。但是当第一次打开应用程序时,数据在我滑动刷新布局之前不会出现。当数据已经出现并且我尝试再次滑动刷新时,它不会循环刷新数据。您的问题缺少一个。Asynctask代码需要是问题的一部分,我把代码放在哪里?对不起,我是android开发新手。您的代码中已经有return myFragmentView;
。在itTanks上面加一行!效果很好!