繁体   English   中英

适用于 Android RecyclerView 的滑动/拉动刷新

[英]Swipe/Pull to Refresh for Android RecyclerView

我创建了一个应用程序来从 XML 文件获取 RSS 提要,并在卡片视图中的回收视图中显示它。 我想实现 Pull to refresh 来加载数据。 方法应该在哪里? 我的主要活动:

public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    //Call Read rss asyntask to fetch rss
    try {
        ReadRss readRss = new ReadRss(this, recyclerView);
        readRss.execute();
    }catch (Exception e) {
        Toast.makeText(getApplicationContext(), "There's a problem", Toast.LENGTH_LONG);
    }
    }

}

这是 ReadRss 类:

public class ReadRss extends AsyncTask<Void, Void, Void> {
Context context;
String address = "http://karary-001-site1.htempurl.com/XMLFile1.xml";
ProgressDialog progressDialog;
ArrayList<FeedItem> feedItems;
RecyclerView recyclerView;
URL url;

public ReadRss(Context context, RecyclerView recyclerView) {
    this.recyclerView = recyclerView;
    this.context = context;
    progressDialog = new ProgressDialog(context);
    progressDialog.setMessage("loading....");
}

//before fetching of rss statrs show progress to user
@Override
protected void onPreExecute() {
    progressDialog.show();
    super.onPreExecute();
}


//This method will execute in background so in this method download rss feeds
@Override
protected Void doInBackground(Void... params) {
    //call process xml method to process document we downloaded from getData() method
    ProcessXml(Getdata());

    return null;
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    progressDialog.dismiss();
    FeedsAdapter adapter = new FeedsAdapter(context, feedItems);
    recyclerView.setLayoutManager(new LinearLayoutManager(context));
    recyclerView.addItemDecoration(new VerticalSpace(20));
    recyclerView.setAdapter(adapter);

}

pullToRefresh 方法:

final SwipeRefreshLayout pullToRefresh = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        pullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //refresh code
                pullToRefresh.setRefreshing(false);
            }
        });

该方法应该在哪里?

首先设置 pullToRefresh.setRefresh(true); 如果它不起作用,请尝试下面的代码

pullToRefresh.post(new Runnable() {
@Override
public void run() {
    pullToRefresh.setRefreshing(true);
}
});

这是你可以做的

pullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            //refresh code
            ReadRss readRss = new ReadRss(this, recyclerView);
            readRss.execute();

        }
    });

然后在onPostExeceute()

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    progressDialog.dismiss();
    pullToRefresh.setRefreshing(false); // in case of pullToRefresh
    FeedsAdapter adapter = new FeedsAdapter(context, feedItems);
    recyclerView.setLayoutManager(new LinearLayoutManager(context));
    recyclerView.addItemDecoration(new VerticalSpace(20));
    recyclerView.setAdapter(adapter);

}

希望这可以帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM