关闭

PullreFreshLibrary(PullToRefreshListView)上拉刷新,下拉加载(—)

标签: 上拉刷新-下拉加载Pullrefres
479人阅读 评论(0) 收藏 举报
分类:

MainActivity.xml
第三方架包下载地址:https://github.com/chrisbanes/Android-PullToRefresh
解压后使用其中的library

<com.handmark.pulltorefresh.library.PullToRefreshListView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"  
        android:id="@+id/expand_list"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        ptr:ptrDrawable="@drawable/indicator_bg_top"   
        ptr:ptrAnimationStyle="flip"  
        ptr:ptrHeaderBackground="#383838"  
        ptr:ptrHeaderTextColor="#FFFFFF"  
        />  

MainActivity.class

import java.util.ArrayList;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity  {

    private PullToRefreshListView mExpandList;
    private ArrayList<String> list;
    private ArrayAdapter<String> adapter;
    Handler hand=new Handler(){
        public void handleMessage(android.os.Message msg) {
            if(msg.what==0){
                adapter.notifyDataSetChanged();
                mExpandList.onRefreshComplete();
            }
        };
    };
    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = new ArrayList<String>();
        getData();
        mExpandList = (PullToRefreshListView)  findViewById(R.id.expand_list); 
        mExpandList.setMode(Mode.BOTH);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, list);
        mExpandList.setAdapter(adapter);
        mExpandList.setOnRefreshListener(new OnRefreshListener2<ListView>()  {

            @Override
            public void onPullDownToRefresh(PullToRefreshBase refreshView) {
                // TODO Auto-generated method stub
                list.clear();
                getData();
                hand.sendEmptyMessageDelayed(0, 2000);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase refreshView) {
                // TODO Auto-generated method stub
                getData();
                hand.sendEmptyMessageDelayed(0, 2000);
            }
        }); 
    }

    private void getData() {
        for(int i=0;i<10;i++){
            list.add(i+"");
        }
    }

    @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;
    }    
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:22640次
    • 积分:727
    • 等级:
    • 排名:千里之外
    • 原创:50篇
    • 转载:10篇
    • 译文:0篇
    • 评论:6条