简体   繁体   中英

Swipe/Pull to Refresh for Android RecyclerView

I have created an app to get RSS Feed from XML file and show it in a recyclerview within card views. I want to implement the Pull to refresh to load the data. where exactly the method supposed to be ? my main activity :

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

}

and this is ReadRss class :

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

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

where exactly the method supposed to be ?

First set pullToRefresh.setRefreshing(true); if its not working than try below code

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

Here is what you can do

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

        }
    });

and then in 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);

}

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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