トラッキング コード

3/30/2014

How to use SwipeRefreshLayout, like a twitter, facebook



Create the Layout

You need to use the android.support.v4.widget.SwipeRefreshLayout.
Content is included SwipeRefreshLayout for clild layout( or view).

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_refresh_widget"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <!-- some full screen pullable view that will be the offsetable content -->

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/content"/>
</android.support.v4.widget.SwipeRefreshLayout>


Update for Swipe

You need the following step, due to updating for Swipe.
1. call setOnRefreshListener methiod due to setting Listener which handles the update starting.
2. onRefresh() is handled the update starting.
3. Then your update process finish、call setRefreshing(false).
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_swipe_refresh_widget);
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_widget);
        mSwipeRefreshLayout.setOnRefreshListener(this);
    }


    @Override
    public void onRefresh() {
        refresh();
    }

    private void refresh() {
        Message msg = mHander.obtainMessage(0, this);
        mHander.sendMessageDelayed(msg, 1000);
    }

    private static Handler mHander = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            SwipeRefreshLayoutActivity actibity = (SwipeRefreshLayoutActivity) msg.obj;
            actibity.mSwipeRefreshLayout.setRefreshing(false);
        }
    };

No comments:

Post a Comment