How to use the SwipeRefreshLayout?

后端 未结 5 1907
孤独总比滥情好
孤独总比滥情好 2020-12-25 11:51

Background

Google has recently published an update to its support library, which now has a new \"SwipeRefreshLayout\" view.

The view allows to wrap anothe

5条回答
  •  我在风中等你
    2020-12-25 12:23

    MainActivity.java

    public class MainActivity extends ActionBarActivity {
        TextView textView;
        private SwipeRefreshLayout mSwipeRefreshLayout;
        static int count = 0;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.scrollTextView);
    
        // /You will setup the action bar with pull to refresh layout
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.container);
    
         mSwipeRefreshLayout.setColorScheme(R.color.blue,
         R.color.green, R.color.orange, R.color.purple);
        mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.e(getClass().getSimpleName(), "refresh");
                new GetLinks().execute();
            }
        });
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    public class GetLinks extends AsyncTask {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
    
        }
    
        @Override
        protected Void doInBackground(Void... params) {
    
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
    
                e.printStackTrace();
    
            }
            return null;
        }
    
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
             //Here you can update the view
            textView.setText(textView.getText().toString()+"--New Content Added" + ++count);
    
            // Notify swipeRefreshLayout that the refresh has finished
            mSwipeRefreshLayout.setRefreshing(false);
        }
    
    }
    
    }
    

    activity_main.xml

    
    
        
                
        
    
    
    

    colors.xml

    
    
    
        #FF33B5E5
        #FFAA66CC
        #FF99CC00
        #FFFFBB33
    
    
    

提交回复
热议问题