|
| 1 | +package com.github.kingaza.aspree; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.ArrayList; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.support.annotation.Nullable; |
| 7 | +import android.support.v4.app.ListFragment; |
| 8 | +import android.util.Log; |
| 9 | +import android.view.LayoutInflater; |
| 10 | +import android.view.View; |
| 11 | +import android.view.ViewGroup; |
| 12 | +import android.widget.ArrayAdapter; |
| 13 | + |
| 14 | +import com.github.kingaza.aspree.model.CountryModel; |
| 15 | +import com.github.kingaza.aspree.protocol.Country; |
| 16 | +import com.github.kingaza.aspree.protocol.Pagination; |
| 17 | +import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout; |
| 18 | +import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection; |
| 19 | + |
| 20 | +import retrofit.Callback; |
| 21 | +import retrofit.RetrofitError; |
| 22 | +import retrofit.client.Response; |
| 23 | + |
| 24 | +/** |
| 25 | + * Created by abu on 2015/5/1. |
| 26 | + */ |
| 27 | +public class CountryFragment extends ListFragment { |
| 28 | + |
| 29 | + private static final String TAG = "CountryFragment"; |
| 30 | + |
| 31 | + SwipyRefreshLayout mSwipyRefreshLayout; |
| 32 | + private List<String> mCountryNameList = new ArrayList<String>(); |
| 33 | + private ArrayAdapter<String> mAdapter; |
| 34 | + |
| 35 | + private Pagination mPagination = new Pagination(); |
| 36 | + |
| 37 | + Callback<CountryModel.Countries> mCallback = new Callback<CountryModel.Countries>() { |
| 38 | + @Override |
| 39 | + public void success(CountryModel.Countries countries, Response response) { |
| 40 | + mPagination = (Pagination) countries; |
| 41 | + Log.i(TAG, "Got page " + mPagination.current_page + "/" + mPagination.pages); |
| 42 | + for (Country country : countries.countries) { |
| 43 | + mCountryNameList.add(country.name); |
| 44 | + } |
| 45 | + mAdapter.notifyDataSetChanged(); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void failure(RetrofitError error) { |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + @Override |
| 54 | + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 55 | + View view = inflater.inflate(R.layout.fragment_country, container, false); |
| 56 | + mSwipyRefreshLayout = (SwipyRefreshLayout) view.findViewById(R.id.swipe_layout); |
| 57 | + mSwipyRefreshLayout.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() { |
| 58 | + |
| 59 | + @Override |
| 60 | + public void onRefresh(SwipyRefreshLayoutDirection direction) { |
| 61 | + |
| 62 | + if (direction == SwipyRefreshLayoutDirection.BOTTOM && |
| 63 | + mPagination.current_page < mPagination.pages) { |
| 64 | + CountryModel.getCountriesInPage(mPagination.current_page+1, mCallback); |
| 65 | + } |
| 66 | + |
| 67 | + mSwipyRefreshLayout.setRefreshing(false); |
| 68 | + } |
| 69 | + }); |
| 70 | + return view; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void onActivityCreated(@Nullable Bundle savedInstanceState) { |
| 75 | + super.onActivityCreated(savedInstanceState); |
| 76 | + CountryModel.getCountries(mCallback); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void onCreate(Bundle savedInstanceState) { |
| 81 | + super.onCreate(savedInstanceState); |
| 82 | + setHasOptionsMenu(true); |
| 83 | + |
| 84 | + mAdapter = new ArrayAdapter<String>(getActivity(), |
| 85 | + android.R.layout.simple_list_item_1, mCountryNameList); |
| 86 | + setListAdapter(mAdapter); |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | +} |
0 commit comments