文章标题

Android UI列表的上拉下拉刷新之Android-PullToRefresh
(1) 设置Listview
layout文件中定义PullToRefreshListView
com.handmark.pulltorefresh.library.PullToRefreshListView
android:id=”@+id/list_view”
android:layout_width=”match_parent”
android:layout_height=”match_parent” />

(2)设置Mode

Java代码 收藏代码
PullToRefreshListView mListView = (PullToRefreshListView) findViewById(R.id.list_view);
mListView.setMode(Mode.BOTH);

Mode.BOTH:同时支持上拉下拉
Mode.PULL_FROM_START:只支持下拉Pulling Down
Mode.PULL_FROM_END:只支持上拉Pulling Up
(3)实现Listener

如果Mode设置成Mode.BOTH,需要设置刷新Listener为OnRefreshListener2,并实现onPullDownToRefresh()、onPullUpToRefresh()两个方法。

如果Mode设置成Mode.PULL_FROM_START或Mode.PULL_FROM_END,需要设置刷新Listener为OnRefreshListener,同时实现onRefresh()方法。当然也可以设置为OnRefreshListener2,但是Mode.PULL_FROM_START的时候只调用onPullDownToRefresh()方法,Mode.PULL_FROM的时候只调用onPullUpToRefresh()方法.

public class MainActivity extends Activity {

private ArrayList<String> mListItems;  
private PullToRefreshListView mListView;  
private SampleListAdapter mAdapter;  

@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.sample);  

    // 设置初期数据  
    mListItems = new ArrayList<String>();  
    for (int i = 1; i <= 10; i++) {  
        mListItems.add("Item " + Integer.toString(i));  
    }  

    // 设置ListView  
    mListView = (PullToRefreshListView) findViewById(R.id.list_view);  
    mAdapter = new SampleListAdapter();  
    mListView.setAdapter(mAdapter);  

    // 设置PullToRefresh  
    mListView.setMode(Mode.BOTH);  
    mListView.setOnRefreshListener(new OnRefreshListener2<ListView>(){  

        // 下拉Pulling Down  
        @Override  
        public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {  
            // 下拉的时候数据重置  
            mListItems = new ArrayList<String>();  
            for (int i = 1; i <= 10; i++) {  
                mListItems.add("Item " + Integer.toString(i));  
            }  
            mAdapter.notifyDataSetChanged();  

            new FinishRefresh().execute();  
        }  

        // 上拉Pulling Up  
        @Override  
        public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {  
            // 上拉的时候添加选项  
            int count = mListItems.size();  
            mListItems.add("Item " + Integer.toString(++count));  
            mAdapter.notifyDataSetChanged();  

            new FinishRefresh().execute();  
        }  

    });  

}  

private class SampleListAdapter extends BaseAdapter {  

    @Override  
    public int getCount() {  
        return mListItems.size();  
    }  

    @Override  
    public Object getItem(int index) {  
        return mListItems.get(index);  
    }  

    @Override  
    public long getItemId(int index) {  
        return index;  
    }  

    @Override  
    public View getView(int index, View view, ViewGroup arg2) {  
        if(view == null){  
            LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
            view = inflater.inflate(R.layout.list_item, null);  
        }  
        TextView textView = (TextView)view.findViewById(R.id.listItemText);  
        textView.setText(mListItems.get(index));  
        return view;  
    }  
}  

private class FinishRefresh extends AsyncTask<Void, Void, Void>{  
    @Override  
    protected Void doInBackground(Void... params) {  
        return null;  
    }  

    @Override  
    protected void onPostExecute(Void result){  
        mListView.onRefreshComplete();  
    }  
}  

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值