PullToRefreshでListFragment

2013-06-27T00:00:00+00:00 Android Java

※以下のAndroid-PullToRefreshは現時点でも既にdeprecatedになっており、 https://github.com/chrisbanes/ActionBar-PullToRefresh を使うべきだそうです

https://github.com/chrisbanes/Android-PullToRefresh を使うにあたってListFragmentでPullToRefreshをする場合

  • extra/PullToRefreshListFragmentのプロジェクトをインポート。使うプロジェクトでライブラリプロジェクト参照を追加
  • ListFragmentを継承しているクラスをPullToRefreshListFragmentを継承
  • getPullToRefreshListViewメソッド#setOnRefreshListenerを実装

っていう感じかと。ざっくり書くと

package kinjouj.sample.feedreader;

import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import com.handmark.pulltorefresh.extras.listfragment.PullToRefreshListFragment;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import org.horrabin.horrorss.RssItemBean;

public class FeedListFragment extends PullToRefreshListFragment
    implements OnRefreshListener<ListView> {

    private static final String TAG = FeedListFragment.class.getName();

    public static final String ARGUMENT_FEED_KEY = "feed";

    private PullToRefreshListView mListView;
    private FeedListAdapter mAdapter;
    private Handler mHandler;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.v(TAG, "onViewCreated");

        mListView = getPullToRefreshListView();
        mListView.setOnRefreshListener(this);

        mAdapter = new FeedListAdapter(getActivity());
        mHandler = new Handler();

        loadFeedItems();
    }

    @Override
    public void onRefresh(PullToRefreshBase<ListView> refreshView) {
        mAdapter.clear();
        loadFeedItems();
        mAdapter.notifyDataSetChanged();

        new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                mHandler.post(new Thread() {
                    @Override
                    public void run() {
                        mListView.onRefreshComplete();
                    }
                });
            }
        }.start();
    }

    private Feed getFeed() {
        Bundle args = getArguments();

        if (args != null) {
            Object o = args.getSerializable(ARGUMENT_FEED_KEY);

            if (o instanceof Feed) {
                return (Feed)o;
            }
        }

        return null;
    }

    private void loadFeedItems() {
        new Thread() {
            @Override
            public void run() {
                Feed feed = getFeed();

                List<RssItemBean> items = FeedUtils.parseFeedItems(feed.getFeedUrl());
                mAdapter.addAll(items);
                mHandler.post(new Thread() {
                    @Override
                    public void run() {
                        setListAdapter(mAdapter);
                    }
                });
            }
        }.start();
    }
}

Laravel使ってみた (6) - Request & Input -