LoadMoreListView+SwipeRefreshLayout(分页下拉)基本结构

一切为了快速迭代



import java.util.ArrayList;

import org.json.JSONObject;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.android.volley.Response.Listener;
import com.widget.view.LoadMoreListView.OnLoadMoreListener;
import com.yirui.youbao.CommonAdapter;
import com.yirui.youbao.ViewHolder;
import com.yirui.youbao.ViewWrapper;
import com.yirui.youbao.app.R;
import com.yirui.youbao.model.Pagination;
import com.yirui.youbao.model.Parking;
import com.yirui.youbao.net.Commands;
import com.yirui.youbao.net.VolleyHelper;
import com.yirui.youbao.util.Json;

/**
 * 
 * 
 * @author pythoner
 * 
 */
public class ParkingPayedListFragment extends BaseFragment {

	private com.widget.view.LoadMoreListView listView;
	private CommonAdapter<Parking> adapter;
	private Pagination pagination = new Pagination();
	private ArrayList<Parking> parkings = new ArrayList<Parking>();

	private SwipeRefreshLayout swipeRefreshLayout;

	public static ParkingPayedListFragment newInstance(/*KeyValue kv*/) {
		ParkingPayedListFragment fragment = new ParkingPayedListFragment();
		Bundle bundle = new Bundle();
		// bundle.putParcelable("KeyValue", kv);
		fragment.setArguments(bundle);
		return fragment;
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle args = getArguments();
		if (args != null) {
			// curKeyValue = args.getParcelable("KeyValue");
		}
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		View view = inflater.inflate(R.layout.fragment_list_comm, container,false);
		listView = (com.widget.view.LoadMoreListView) view.findViewById(R.id.listView);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				//
			}
		});
		listView.setOnLoadMoreListener(new OnLoadMoreListener() {

			@Override
			public void onLoadMore() {
				pagination.page++;
				doDefaultParkingQuery();
			}
		});

		swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
		swipeRefreshLayout.setColorScheme(R.color.scheme_green,R.color.scheme_yellow, R.color.scheme_gray, R.color.scheme_red);
		swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

					@Override
					public void onRefresh() {
						// TODO Auto-generated method stub
						refreshListView();
						//
						doDefaultParkingQuery();
					}
				});

		doDefaultParkingQuery();

		return view;
	}

	private void doDefaultParkingQuery() {
		doCommandParkingQuery("潍坊路", pagination);
	}

	private void doCommandParkingQuery(String keyword, Pagination pagination) {
		Commands.doCommandParkingQuery(context, keyword, pagination,
				new Listener<JSONObject>() {
					@Override
					public void onResponse(JSONObject response) {
						// TODO Auto-generated method stub
						// Log.i("tag", response.toString());
						if (context.isSuccess(response)) {
							updateViews(Json.parseParkings(response));
						}
					}

				});
	}
	
	private void refreshListView() {
		listView.resetLoadMoreListener();
		pagination.page = 1;
		parkings.clear();
		// notifyDataSetChanged();
	}
	
	private void closeRefreshing() {
		if (swipeRefreshLayout != null && swipeRefreshLayout.isRefreshing()) {
			swipeRefreshLayout.setRefreshing(false);
		}
	}

	private void updateViews(Object obj) {
		closeRefreshing();
		if (obj == null) {
			return;
		}
		ArrayList<Parking> list = (ArrayList<Parking>) obj;
		// Log.i("tag", "list.size==="+list.size());
		listView.onLoadMoreComplete();
		if (list.size() == 0) {
			listView.removeLoadMoreListener();
			return;
		}
		parkings.addAll(list);
		notifyDataSetChanged();

	}

	private void notifyDataSetChanged() {
		if (adapter == null) {
			listView.setAdapter(adapter = new CommonAdapter<Parking>(context,
					parkings, R.layout.item_for_parkings) {
				@Override
				public void setValues(ViewHolder helper, Parking item,
						int position) {
					createItem(helper, item, position);
				}
			});
		} else {
			adapter.notifyDataSetChanged();
		}
	}

	//打开关闭下滑面板用,本例并不关心,可忽略
	private View lastContainer = null;
	private Parking lastItem = null;
	private void createItem(ViewHolder helper, final Parking item,
			final int position) {
		ImageView item_0 = helper.getView(R.id.item_0);
		VolleyHelper.loadImageByVolley(item.getImage(), item_0,
				R.drawable.default_face);
		helper.setText(R.id.item_1, item.getName());
		helper.setText(R.id.item_2, item.getPrice());
		// helper.setText(R.id.item_3, item.getName());
		final View container = helper.getView(R.id.container);
		final int maxHeight = 140;// 菜单的高
		if (item.isOpen()) {
			container.setLayoutParams(new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.MATCH_PARENT, maxHeight));
		} else {
			container.setLayoutParams(new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.MATCH_PARENT, 0));
		}
		helper.getView(R.id.item_4).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (container == lastContainer) {
							doSliding(container, maxHeight);
							item.setOpen(!item.isOpen());
						} else {
							doSliding(container, maxHeight);
							item.setOpen(true);
							if (lastContainer != null) {
								doSliding(lastContainer, 0);
								lastItem.setOpen(false);
							}
							lastContainer = container;
							lastItem = item;
						}

					}
				});

		helper.getView(R.id.btn_0).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Log.i("tag", "btn_0[" + position + "]");
					}
				});
		helper.getView(R.id.btn_1).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Log.i("tag", "btn_1[" + position + "]");
					}
				});
		helper.getView(R.id.btn_2).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Log.i("tag", "btn_2[" + position + "]");
					}
				});

		helper.getView(R.id.btn_3).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Log.i("tag", "btn_3[" + position + "]");
					}
				});
		helper.getView(R.id.btn_4).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Log.i("tag", "btn_4[" + position + "]");
					}
				});
	}

	private void doSliding(View view, int maxHeight) {
		ViewWrapper wrapper = new ViewWrapper(view);
		if (view.getHeight() == 0) {
			ObjectAnimator.ofInt(wrapper, "height", maxHeight).setDuration(400)
					.start();
		} else {
			ObjectAnimator.ofInt(wrapper, "height", 0).setDuration(400).start();
		}
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		super.onDestroyView();
	}

}


布局
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.widget.view.LoadMoreListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:cacheColorHint="@android:color/transparent" />

</android.support.v4.widget.SwipeRefreshLayout>


/*
 * 分页
 */
public class Pagination {

	public int page=1;
	public int size=10;
	
	public void reset(){
		page=1;
		size=10;
	}
	
	public int nextPage(){
		return page++;
	}
}

你可能感兴趣的:(android)