ListView实现下拉刷新

            效果图:

ListView实现下拉刷新ListView实现下拉刷新

 

ListView实现下拉刷新

布局文件:

 <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"
    tools:context=".MainActivity" >
    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/refreshListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>

代码:

package com.example.pulltorefreshdemo;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
public class MainActivity extends Activity {
 PullToRefreshListView mPullToRefreshListView;
 ListView mlListView;
 List<String> titleList=new ArrayList<String>();
 ArrayAdapter<String> adapter;
 Handler handler=new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPullToRefreshListView=(PullToRefreshListView)findViewById(R.id.refreshListView);
        mlListView=mPullToRefreshListView.getRefreshableView();
      //PULL_FROM_START:下拉模式.PULL_FROM_END:上拉模式.BOTH:两者均可
        mPullToRefreshListView.setMode(Mode.PULL_FROM_START);
        
        titleList.add("刷新测试");
        adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, titleList);
        
        mlListView.setAdapter(adapter);
        mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
   @Override
   public void onRefresh(PullToRefreshBase<ListView> refreshView) {
          new Thread(){
           public void run() {
           
            titleList.add(0,""+new Date());
            
            handler.post(new Runnable() {
       
       @Override
       public void run() {
         adapter.notifyDataSetChanged();
         mPullToRefreshListView.onRefreshComplete();
       }
      });
            
           }
          }.start();
   }
  });
       
        
       
        
        
    }

    
    
}

你可能感兴趣的:(android,ListView下拉刷新)