recently, when I was watching a mobile phone app tutorial with a high imitation of Lashou, there was a problem when I saw the commodity information in the main interface. It used PullToFresh to achieve the functions of pull-down refresh and pull-up load, that is, to send a http request to my tomcat server to request data and then return it through json, but now I don"t seem to be able to monitor this pull-down action. I hope God can take a look at it for me.
public class GoodsAdapter extends BaseAdapter {
private List<Goods> mList;
public GoodsAdapter(List<Goods> list){
mList=list;
}
@Override
public int getCount() {
return (mList != null) ? mList.size() : 0;
}
@Override
public Object getItem(int position) {
return (mList != null && mList.size() > position) ? mList.get(position-1): null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
if(convertView==null){
convertView= LayoutInflater.from(parent.getContext()).inflate(R.layout.goods_list_row,null);
holder=new ViewHolder();
ViewUtils.inject(holder,convertView);
convertView.setTag(holder);
}else{
holder=(ViewHolder)convertView.getTag();
}
Goods goods=mList.get(position);
Picasso.with(parent.getContext()).load(goods.getImgUrl())
.placeholder(R.drawable.default_pic).into(holder.photo);
holder.title.setText(goods.getSortTile());
holder.tv_content.setText(goods.getTitle());
holder.price.setText(String.valueOf("" + goods.getPrice()));
holder.value.setText(String.valueOf("" + goods.getValue()));
if (goods.isOp()) {
holder.appointment_img.setVisibility(View.VISIBLE);
} else {
holder.appointment_img.setVisibility(View.GONE);
}
return convertView;
}
class ViewHolder{
@ViewInject(R.id.photo)
private ImageView photo;
@ViewInject(R.id.appoitment_img)
private ImageView appointment_img;
@ViewInject(R.id.title)
private TextView title;
@ViewInject(R.id.tv_content)
private TextView tv_content;
@ViewInject(R.id.price)
private TextView price;
@ViewInject(R.id.value)
private TextView value;
@ViewInject(R.id.distance)
private TextView distance;
}
}
public class FragmentIndex extends android.support.v4.app.Fragment {
@ViewInject(R.id.home_city)
private TextView home_city;
@ViewInject(R.id.goods_list_view)
private PullToRefreshListView goods_list_view;
private List<Goods> mList;
private int page=0,size=20,count=0;
private GoodsAdapter mGoodsAdapter;
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle bundle) {
View view=LayoutInflater.from(getActivity()).inflate(R.layout.frag_index ,null);
ViewUtils.inject(this,view);
goods_list_view.setMode(PullToRefreshBase.Mode.BOTH);//
goods_list_view.setScrollingWhileRefreshingEnabled(true);
//
/* goods_list_view.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
//
loadDatas(refreshView.getScrollY()<0);
}
});*/
//
goods_list_view.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
//....,,
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
loadDatas(true);
Log.v("","");
}
@Override
//..pagePP,
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
loadDatas(false);
}
});
//
new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
goods_list_view.setRefreshing();
return true;
}
}).sendEmptyMessageDelayed(0,200);
return view;
}
//truefalse
private void loadDatas(final boolean direction){
RequestParams params=new RequestParams();
if(direction){//
page=1;
}else{
pagePP;
}
params.addQueryStringParameter("page",String .valueOf(page));
params.addQueryStringParameter("size",String.valueOf(size));
new HttpUtils().send(HttpRequest.HttpMethod.GET, CONST.GOODS_LIST, params, new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
goods_list_view.onRefreshComplete();
Gson gson=new GsonBuilder().create();
ResponseObject<List<Goods>> object=gson.fromJson(responseInfo.result,new TypeToken <ResponseObject<List<Goods>>>(){}.getType());
page = object.getPage();
size = object.getSize();
count = object.getCount();
if (direction) {//
mList=(List<Goods>)object.getDatas();
mGoodsAdapter = new GoodsAdapter(mList);
goods_list_view.setAdapter(mGoodsAdapter);
} else {//
mList.addAll((List<Goods>)object.getDatas());
mGoodsAdapter.notifyAll();
}
if (count == page) {//
goods_list_view.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
}
}
@Override
public void onFailure(HttpException e, String s) {
goods_list_view.onRefreshComplete();
Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
}
});
}
@OnClick({R.id.home_city,R.id.home_map,R.id.home_search,})
public void onClick(View v){
switch (v.getId()){
case R.id.home_city://
{
startActivity(new Intent(getActivity(), CityActivity.class));
break;
}
case R.id.home_map:
break;
case R.id.home_search:
break;
}
}
@Override
public void setMenuVisibility(boolean menuVisibility){
super.setMenuVisibility(menuVisibility);
if(this.getView()!=null)
this.getView().setVisibility(menuVisibility?View.VISIBLE:View.GONE);
}
}