如何使用PulltoRefush实现下拉刷新,上拉加载

package com.example.test2;


import java.util.ArrayList;
import java.util.List;
import com.example.vo.DataObject;
import com.example.vo.Data_image;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;


import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;


/**
 * 
 * 实现lestview,上拉下载
 * @author 浅议爱
 *
 */
public class MainActivity extends ListActivity {

static final int MENU_MANUAL_REFRESH = 0;
static final int MENU_DISABLE_SCROLL = 1;
static final int MENU_SET_MODE = 2;
static final int MENU_DEMO = 3;
int index=1;
//定义集合,保存解析的数据
private List<Data_image> list_info=new ArrayList<Data_image>();
//定义适配器
private MyListAdapter adapter;
    //定义控件
private PullToRefreshListView mPullRefreshListView;
    
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


                

//获取控件id
mPullRefreshListView=(PullToRefreshListView) findViewById(R.id.pull_refresh_list);




                getinfo();



mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {


@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
// TODO Auto-generated method stub
String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);//3.刷新界面处理代理,显示新的时间
                //调用联网方法
getinfo();

}
});

//5.设置上拉加载处理
     mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
    
@Override
public void onLastItemVisible() {
getinfo();
Toast.makeText(MainActivity.this, "正在加载", 1).show();
}
});


}



private void getinfo() {
// TODO Auto-generated method stub
HttpUtils httpUtils=new HttpUtils();
String url_path="http://api.expoon.com/AppNews/getNewsList/type/1/p/"+index;
httpUtils.send(HttpMethod.POST, url_path, new RequestCallBack<String>() {


@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub

}

@Override
public void onSuccess(ResponseInfo<String> arg0) {
// TODO Auto-generated method stub
String json = arg0.result;
Gson gson=new Gson();
DataObject dataObject=gson.fromJson(json, DataObject.class);
list_info=dataObject.getData();
adapter=new MyListAdapter(MainActivity.this,list_info);
mPullRefreshListView.setAdapter(adapter);
//点击跳转
mPullRefreshListView.setOnItemClickListener(new OnItemClickListener() {


@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Intent it=new Intent(MainActivity.this,InfoActivity.class);
it.putExtra("pic", list_info.get(position).getPic_url());
it.putExtra("cotent", list_info.get(position).getNews_summary());
startActivity(it);
}
});
adapter.notifyDataSetChanged();
           index= index+1;
// Call onRefreshComplete when the list has been refreshed.
mPullRefreshListView.onRefreshComplete();


}
});


}


}



//布局

<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
        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" />


  
</RelativeLayout>



//配置文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test2"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="14" />
    <uses-permission android:name="android.permission.INTERNET"/>


    <application
        android:hardwareAccelerated="true"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="PtR ListView" >                                                                                                                 注:红色代表要修改的文件,,,在配置文件中修改
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
          <activity
            android:name=".InfoActivity"
            android:label="PtR ListView" >
        </activity>
    </application>


</manifest>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值