Android 下拉刷新



//新建项目引用

 


 

1.PullToRefreshListFragment------>添加------library

(对着项目右键----->Properties----->android)

 

 

 

 

 

2.PullToRefreshViewPager------也添加------library



 

 

3.LauncherActivity---------------添加3个库

 

 

//新建项目-----UsingListView------也添加3个库

 

 

复制libs包写的android-support-v4.jar包分别拷入 其他的4个库包中(覆盖到原来的)

 




 

//activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

 <!-- library库下的PullToRefreshListView类的全称 --> 

<com.handmark.pulltorefresh.library.PullToRefreshListView
    android:id="@+id/mylv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    
      
    
</com.handmark.pulltorefresh.library.PullToRefreshListView>
    
    
    




</LinearLayout>

 


//MainActivity---主程序

package com.jikexueyuan.usinglistview;

import java.util.ArrayList;
import java.util.List;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;


import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;


public class MainActivity extends Activity {
private PullToRefreshListView lv;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

lv=(PullToRefreshListView) findViewById(R.id.mylv);

List<String> arr=new ArrayList<String>();
arr.add("jikexueyuan");
arr.add("eoe");

adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arr);
lv.setAdapter(adapter);

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


@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {


//new AsyncTask<Params参数, Progress进度, Result结果>()-->Void(V要大写)
  new AsyncTask<Void, Void, Void>() {


protected Void doInBackground(Void... params) {

try {

Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
      //在这里执行主线程的操作
 
@SuppressLint("NewApi")
protected void onPostExecute(Void result) {
  //添加数据
  adapter.addAll("Hello","大家好");
  //通知listview,下载数据
  lv.onRefreshComplete();
  
  };
  }.execute();//执行

}


});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}





 


//新建项目引用

 


 

1.PullToRefreshListFragment------>添加------library

(对着项目右键----->Properties----->android)

 

 

 

 

 

2.PullToRefreshViewPager------也添加------library



 

 

3.LauncherActivity---------------添加3个库

 

 

//新建项目-----UsingListView------也添加3个库

 

 

复制libs包写的android-support-v4.jar包分别拷入 其他的4个库包中(覆盖到原来的)

 




 

//activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

 <!-- library库下的PullToRefreshListView类的全称 --> 

<com.handmark.pulltorefresh.library.PullToRefreshListView
    android:id="@+id/mylv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    
      
    
</com.handmark.pulltorefresh.library.PullToRefreshListView>
    
    
    




</LinearLayout>

 


//MainActivity---主程序

package com.jikexueyuan.usinglistview;

import java.util.ArrayList;
import java.util.List;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;


import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;


public class MainActivity extends Activity {
private PullToRefreshListView lv;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

lv=(PullToRefreshListView) findViewById(R.id.mylv);

List<String> arr=new ArrayList<String>();
arr.add("jikexueyuan");
arr.add("eoe");

adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arr);
lv.setAdapter(adapter);

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


@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {


//new AsyncTask<Params参数, Progress进度, Result结果>()-->Void(V要大写)
  new AsyncTask<Void, Void, Void>() {


protected Void doInBackground(Void... params) {

try {

Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
      //在这里执行主线程的操作
 
@SuppressLint("NewApi")
protected void onPostExecute(Void result) {
  //添加数据
  adapter.addAll("Hello","大家好");
  //通知listview,下载数据
  lv.onRefreshComplete();
  
  };
  }.execute();//执行

}


});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}





 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值