Android下拉刷新之二

based on PullToRefresh

this essay describe how to refresh image

define two object (PullToRefreshListView , List) in class

private PullToRefreshListView lv;
List<Map<String, Object>> listem=new ArrayList<Map<String,Object>>();

in onCreate method , get the ListView 

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

then define two array to show images and text

String[] name={"aa","bb"};
int[] imgid={R.drawable.a,R.drawable.e};

create loop and Map , put images and text in it

for(int i=0;i<name.length;i++){
        Map<String, Object> listMap=new HashMap<String, Object>();
        listMap.put("img", imgid[i]);
        listMap.put("name", name[i]);
        listem.add(listMap);
}

define SimpleAdapter object , and make it to show image and text in xml file

final SimpleAdapter adapter=new SimpleAdapter(this
        ,listem,R.layout.fragment_main
        ,new String[]{"img","name"},new int[]{R.id.head,R.id.name});

lv.setAdapter(adapter);

set ListView Listen to events and achieve those method

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


@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
// TODO Auto-generated method stub
//使用异步方式刷新
new AsyncTask<Void, Void, Void>() {

@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

protected void onPostExecute(Void result) 
{

/*int i=(int) (Math.random()*3);
String jString=Integer.toString(i);
adapter.addAll("hello",jString);*/
//adapter.addAll(R.drawable.a,"cc");
//System.out.println(listem.size());
Map<String, Object> listMap=new HashMap<String, Object>();
listMap.put("img", R.drawable.a);
listMap.put("name", "c");
listem.add(listMap);
lv.onRefreshComplete();//完成刷新动作
};

}.execute();//执行下拉刷新操作
}
});


in the end , put my complete code 

package com.example.usinglistview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

	private PullToRefreshListView lv;
	List
    
    
     
     > listem=new ArrayList
     
     
      
      
       
       >();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv=(PullToRefreshListView) findViewById(R.id.mylv);
        
        /*List
       
       
         arr=new ArrayList 
        
          (); arr.add("huaruan"); arr.add("ror");*/ String[] name={"aa","bb"}; int[] imgid={R.drawable.a,R.drawable.e}; for(int i=0;i 
         
           listMap=new HashMap 
          
            (); listMap.put("img", imgid[i]); listMap.put("name", name[i]); listem.add(listMap); } final SimpleAdapter adapter=new SimpleAdapter(this ,listem,R.layout.fragment_main ,new String[]{"img","name"},new int[]{R.id.head,R.id.name}); lv.setAdapter(adapter); lv.setOnRefreshListener(new OnRefreshListener 
           
             () { @Override public void onRefresh(PullToRefreshBase 
            
              refreshView) { // TODO Auto-generated method stub new AsyncTask 
             
               () { @Override protected Void doInBackground(Void... params) { try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } protected void onPostExecute(Void result) { /*int i=(int) (Math.random()*3); String jString=Integer.toString(i); adapter.addAll("hello",jString);*/ //adapter.addAll(R.drawable.a,"cc"); //System.out.println(listem.size()); Map 
              
                listMap=new HashMap 
               
                 (); listMap.put("img", R.drawable.a); listMap.put("name", "c"); listem.add(listMap); lv.onRefreshComplete(); }; }.execute(); } }); } } 
                
               
              
             
            
           
          
         
       
      
      
    
    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值