PullToRefreshListView

PullToRefreshListView

实现上拉加载下拉刷新,首先在项目工程中导入library
在布局文件中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/pull_refresh_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:divider="#19000000"
        android:dividerHeight="4dp"
        android:fadingEdge="none"
        android:fastScrollEnabled="false"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:smoothScrollbar="true"
        ptr:ptrMode="both" >
    </com.handmark.pulltorefresh.library.PullToRefreshListView>

</RelativeLayout>

item布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/item_img"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:background="@drawable/ic_launcher" />

    <LinearLayout
        android:id="@+id/item_line"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="sdfs" />

        <TextView
            android:id="@+id/region_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="sdfs" />

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/new_price_value"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="sdfs" />

            <TextView
                android:id="@+id/new_price_back"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/new_price_value"
                android:text="sdfs" />
        </RelativeLayout>

        <TextView
            android:id="@+id/tags"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="sdfs" />

        <TextView
            android:id="@+id/sub_region_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="sdfs" />
    </LinearLayout>

</LinearLayout>

在Activity代码中

package com.example.myandroid_m2;

import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ListView;

import com.bwie.vo.Myrows;
import com.bwie.vo.SuperClass;
import com.example.adapter.MyBaseAdapater;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

public class InfoActivity extends Activity {

    private PullToRefreshListView lv;

    private String URL = "http://api.fang.anjuke.com/m/android/1.3/shouye/recInfosV3/?city_id=14&lat=40.04652&lng=116.306033&api_key=androidkey&sig=9317e9634b5fbc16078ab07abb6661c5&macid=45cd2478331b184ff0e15f29aaa89e3e&app=a-ajk&_pid=11738&o=PE-TL10-user+4.4.2+HuaweiPE-TL10+CHNC00B260+ota-rel-keys%2Crelease-keys&from=mobile&m=Android-PE-TL10&cv=9.5.1&cid=14&i=864601026706713&v=4.4.2&qtime=20160411091603&pm=b61&uuid=1848c59c-185d-48d9-b0e9-782016041109&_chat_id=0";

    ArrayList<Myrows> lists = new ArrayList<Myrows>();
    Handler hd = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == 1) {
                ArrayList<Myrows> list = (ArrayList<Myrows>) msg.obj;

                lv.setAdapter(new MyBaseAdapater(InfoActivity.this, list));
                // 停止刷新
                lv.onRefreshComplete();
            }

        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info);

        // 找控件
        lv = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);

        // 请求网络
        getNetInfo(0);

        lv.setOnRefreshListener(new OnRefreshListener2<ListView>() {
            /**
             * 下拉刷新
             * 
             * @param refreshView
             */
            @Override
            public void onPullDownToRefresh(
                    PullToRefreshBase<ListView> refreshView) {
                // 重新请求网络
                getNetInfo(0);
            }

            /**
             * 上来加载
             * 
             * @param refreshView
             */
            @Override
            public void onPullUpToRefresh(
                    PullToRefreshBase<ListView> refreshView) {
                // 请求网络
                getNetInfo(1);
            }
        });

    }

    // 请求数据的方法

    public void getNetInfo(final int type) {
        new Thread() {

            public void run() {

                HttpClient hc = new DefaultHttpClient();

                HttpGet hg = new HttpGet(URL);
                HttpResponse hr;
                try {
                    hr = hc.execute(hg);

                    int statusCode = hr.getStatusLine().getStatusCode();
                    if (statusCode == 200) {
                        HttpEntity entity = hr.getEntity();
                        String string = EntityUtils.toString(entity, "utf-8");
                        Gson g = new Gson();
                        SuperClass superClass = g.fromJson(string,
                                SuperClass.class);
                        ArrayList<Myrows> list = superClass.getResult()
                                .getRows();
                        if (type == 0) {
                            lists.addAll(0, list);
                        } else {
                            lists.addAll(list);
                        }
                        hd.obtainMessage(1, lists).sendToTarget();

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }.start();
    }

}

在adapter中

package com.example.adapter;

import java.util.ArrayList;

import com.bwie.vo.Myinfo;
import com.bwie.vo.Myrows;
import com.example.myandroid_m2.R;
import com.lidroid.xutils.BitmapUtils;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyBaseAdapater extends BaseAdapter {



    ArrayList<Myrows> list;
    Context context;
    public MyBaseAdapater(Context context,ArrayList<Myrows> list) {

        this.context=context;
        this.list=list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        // 判空
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.info_item, null);

            //找控件
            holder=new ViewHolder();
            holder.item_img=(ImageView) convertView.findViewById(R.id.item_img);
            holder.address=(TextView) convertView.findViewById(R.id.address);
            holder.region_title=(TextView) convertView.findViewById(R.id.region_title);
            holder.new_price_value=(TextView) convertView.findViewById(R.id.new_price_value);
            holder.new_price_back=(TextView) convertView.findViewById(R.id.new_price_back);
            holder.tags=(TextView) convertView.findViewById(R.id.tags);
            holder.sub_region_title=(TextView) convertView.findViewById(R.id.sub_region_title);

            //进行绑定
            convertView.setTag(holder);
        }else{
            holder=(ViewHolder) convertView.getTag();
        }

        Myinfo info = list.get(position).getInfo();
        holder.address.setText(info.getAddress());
        holder.region_title.setText(info.getRegion_title());
        holder.new_price_value.setText(info.getNew_price_value());
        holder.new_price_back.setText(info.getNew_price_back());
        holder.tags.setText(info.getTags());
        holder.sub_region_title.setText(info.getSub_region_title());

        BitmapUtils bu = new BitmapUtils(context);
        bu.display(holder.item_img, info.getDefault_image());
        return convertView;
    }
    class ViewHolder {

        ImageView item_img;
        TextView address;
        TextView region_title;
        TextView new_price_value;
        TextView new_price_back;
        TextView tags;
        TextView sub_region_title;

    }
}

json数据的bean类

package com.bwie.vo;

public class SuperClass {

    private Myresult result;

    public Myresult getResult() {
        return result;
    }

    public void setResult(Myresult result) {
        this.result = result;
    }

    @Override
    public String toString() {
        return "SuperClass [result=" + result + "]";
    }

    public SuperClass(Myresult result) {
        super();
        this.result = result;
    }

    public SuperClass() {
        super();
    }



}


package com.bwie.vo;

import java.util.ArrayList;


public class Myresult {

    private ArrayList<Myrows> rows;

    public ArrayList<Myrows> getRows() {
        return rows;
    }

    public void setRows(ArrayList<Myrows> rows) {
        this.rows = rows;
    }

    @Override
    public String toString() {
        return "Myresult [rows=" + rows + "]";
    }

    public Myresult(ArrayList<Myrows> rows) {
        super();
        this.rows = rows;
    }

    public Myresult() {
        super();
    }


}


package com.bwie.vo;

public class Myrows {

    private Myinfo info;

    public Myinfo getInfo() {
        return info;
    }

    public void setInfo(Myinfo info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Myrows [info=" + info + "]";
    }

    public Myrows(Myinfo info) {
        super();
        this.info = info;
    }

    public Myrows() {
        super();

    }

}


package com.bwie.vo;

import java.util.List;

public class Myinfo {

    private List<String> activity_tags_icon;
    private String address;
    private String baidu_lat;
    private String baidu_lng;
    private String city_id;
    private String default_image;
    private String kaipan_new_date;
    private String loupan_name;
    private String loupan_property_type;
    private String new_price_back;
    private String new_price_value;
    private String region_title;
    private String sub_region_title;
    private String tags;
    public List<String> getActivity_tags_icon() {
        return activity_tags_icon;
    }
    public void setActivity_tags_icon(List<String> activity_tags_icon) {
        this.activity_tags_icon = activity_tags_icon;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getBaidu_lat() {
        return baidu_lat;
    }
    public void setBaidu_lat(String baidu_lat) {
        this.baidu_lat = baidu_lat;
    }
    public String getBaidu_lng() {
        return baidu_lng;
    }
    public void setBaidu_lng(String baidu_lng) {
        this.baidu_lng = baidu_lng;
    }
    public String getCity_id() {
        return city_id;
    }
    public void setCity_id(String city_id) {
        this.city_id = city_id;
    }
    public String getDefault_image() {
        return default_image;
    }
    public void setDefault_image(String default_image) {
        this.default_image = default_image;
    }
    public String getKaipan_new_date() {
        return kaipan_new_date;
    }
    public void setKaipan_new_date(String kaipan_new_date) {
        this.kaipan_new_date = kaipan_new_date;
    }
    public String getLoupan_name() {
        return loupan_name;
    }
    public void setLoupan_name(String loupan_name) {
        this.loupan_name = loupan_name;
    }
    public String getLoupan_property_type() {
        return loupan_property_type;
    }
    public void setLoupan_property_type(String loupan_property_type) {
        this.loupan_property_type = loupan_property_type;
    }
    public String getNew_price_back() {
        return new_price_back;
    }
    public void setNew_price_back(String new_price_back) {
        this.new_price_back = new_price_back;
    }
    public String getNew_price_value() {
        return new_price_value;
    }
    public void setNew_price_value(String new_price_value) {
        this.new_price_value = new_price_value;
    }
    public String getRegion_title() {
        return region_title;
    }
    public void setRegion_title(String region_title) {
        this.region_title = region_title;
    }
    public String getSub_region_title() {
        return sub_region_title;
    }
    public void setSub_region_title(String sub_region_title) {
        this.sub_region_title = sub_region_title;
    }
    public String getTags() {
        return tags;
    }
    public void setTags(String tags) {
        this.tags = tags;
    }
    @Override
    public String toString() {
        return "Myinfo [activity_tags_icon=" + activity_tags_icon
                + ", address=" + address + ", baidu_lat=" + baidu_lat
                + ", baidu_lng=" + baidu_lng + ", city_id=" + city_id
                + ", default_image=" + default_image + ", kaipan_new_date="
                + kaipan_new_date + ", loupan_name=" + loupan_name
                + ", loupan_property_type=" + loupan_property_type
                + ", new_price_back=" + new_price_back + ", new_price_value="
                + new_price_value + ", region_title=" + region_title
                + ", sub_region_title=" + sub_region_title + ", tags=" + tags
                + "]";
    }
    public Myinfo(List<String> activity_tags_icon, String address,
            String baidu_lat, String baidu_lng, String city_id,
            String default_image, String kaipan_new_date, String loupan_name,
            String loupan_property_type, String new_price_back,
            String new_price_value, String region_title,
            String sub_region_title, String tags) {
        super();
        this.activity_tags_icon = activity_tags_icon;
        this.address = address;
        this.baidu_lat = baidu_lat;
        this.baidu_lng = baidu_lng;
        this.city_id = city_id;
        this.default_image = default_image;
        this.kaipan_new_date = kaipan_new_date;
        this.loupan_name = loupan_name;
        this.loupan_property_type = loupan_property_type;
        this.new_price_back = new_price_back;
        this.new_price_value = new_price_value;
        this.region_title = region_title;
        this.sub_region_title = sub_region_title;
        this.tags = tags;
    }
    public Myinfo() {
        super();
    }




}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值