How to use the SwipeRefreshLayout?

后端 未结 5 1895
孤独总比滥情好
孤独总比滥情好 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:10

    Dunno what that ActionBarActivity class you're extending is, but I got it working just fine using a FragmentActivity

    public class ActivityMain extends FragmentActivity implements OnRefreshListener {
    
        private SwipeRefreshLayout mSwipeRefreshLayout;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            setContentView(R.layout.activity_main);
            mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.container);
            mSwipeRefreshLayout.setOnRefreshListener(this);
    
            super.onCreate(savedInstanceState);
        }
    
        @Override
        public void onRefresh() {
            Toast.makeText(this, "Refresh", Toast.LENGTH_SHORT).show();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mSwipeRefreshLayout.setRefreshing(false);
                }
            }, 2000);
        }
    }
    

    Worth Pointing out I copy pasted your xml layout exactly as it is

    In terms of customization, there's really not much you can do other than change the color of the colored bar by calling setColorScheme(int colorResId, int colorResId, int colorResId, int colorResId);

    e.g.

    
    
    
        #0099CC
        #9933CC
        #669900
        #FF8800
    
    
    

    mSwipeRefreshLayout.setColorScheme(R.color.blue, R.color.purple, R.color.green, R.color.orange);

    It's kind of a disappointing addition really. The sensitivity on the refresh is fairly high and there is no setting to change it

    Edit

    I wrote this when this class (and the ActionBarActivity class) had just been added to the sdk. As such, some things have changed from when I wrote this answer. Furthermore, the type of Activity you use should not affect this solution.

    setColorScheme is now deprecated, setColorSchemeResources(int... colorResIds) should be used instead. (you can put as many color ids in there as you like).

    setDistanceToTriggerSync(int distance) can also be used to set how far down a user needs to swipe in order to trigger a refresh.

    I recommend checking out the official documentation to see what else the class has to offer.

提交回复
热议问题