Android PullToRefreshListView和ViewPager的结合使用
Posted 星辰
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android PullToRefreshListView和ViewPager的结合使用相关的知识,希望对你有一定的参考价值。
其实这个不是什么新东西了,在介绍(一)中我们就知道了PullToRefreshListView的用法,这里只要将其放入到ViewPager中就行啦。ViewPager还是和以往一样的定义和使用,在适配器中存视图的时候放入PullToRefreshListView就行。
1.ViewPager的布局文件
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#000000"> <!-- The PullToRefreshListView replaces a standard ListView widget. --> <android.support.v4.view.ViewPager android:id="@+id/vp_list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
2.找到ViewPager并设置适配器
private void initViewPager() { // TODO 自动生成的方法存根 mViewPager = (ViewPager) findViewById(R.id.vp_list); //得到设置好的view列表 ArrayList<View> viewList = setListViewInVp(); mViewPager.setAdapter(new ListViewPagerAdapter(viewList)); }
适配器:
/** * @author:Jack Tony * @tips :viewPager的适配器 * @date :2014-10-14 */ public class ListViewPagerAdapter extends PagerAdapter { //设置list数组,传入view private ArrayList<View> mViewList; private int pagerNum = 0; public ListViewPagerAdapter(ArrayList<View> viewList) { mViewList = viewList; } public int getPagerNum() { return pagerNum; } @Override public int getCount() { return mViewList.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public void destroyItem(View arg0, int position, Object object) { if (mViewList.get(position) != null) { ((ViewPager) arg0).removeView(mViewList.get(position)); } } @Override public Object instantiateItem(View container, int position) { try { if (mViewList.get(position).getParent() == null) { ((ViewPager) container).addView(mViewList.get(position), 0); } else { /* * 很难理解新添加进来的view会自动绑定一个父类,由于一个儿子view不能与两个父类相关, * 所以得解绑不这样做否则会产生 viewpager java.lang.IllegalStateException: * The specified child already has a parent. You must call * removeView() on the child‘s parent first. */ ((ViewGroup) mViewList.get(position).getParent()) .removeView(mViewList.get(position)); ((ViewPager) container).addView(mViewList.get(position), 0); } } catch (Exception e) { e.printStackTrace(); } finally { pagerNum = position; } return mViewList.get(position); } }
3.开始设置要放入viewPager中的listView
布局文件:
layout_listview_in_viewpager.xml
<?xml version="1.0" encoding="utf-8"?> <com.handmark.pulltorefresh.library.PullToRefreshListView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ptr="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent" android:layout_height="fill_parent" ptr:ptrHeaderBackground="@android:color/darker_gray" />
java代码:
private ArrayList<View> setListViewInVp() { //设置给viewpager中填充的view列表 ArrayList<View> viewList = new ArrayList<View>(); //将string数组放入适配器中 final String[] data = { "1", "2", "3","4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17","18", "19", "20", }; ListAdapter adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, data); //找到listView的布局,生成可以下拉刷新的listView,添加到视图列表中 LayoutInflater inflater = LayoutInflater.from(getApplication()); for (int i = 0; i < 4; i++) { PullToRefreshListView plv = (PullToRefreshListView) inflater.inflate( R.layout.layout_listview_in_viewpager, null); plv.setAdapter(adapter); plv.setOnRefreshListener(new OnRefreshListener<ListView>() { @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) { // TODO 自动生成的方法存根 new GetDataTask(refreshView).execute(); } }); viewList.add(plv); } return viewList; }
4.定义异步任务
private static class GetDataTask extends AsyncTask<Void, Void, Void> { PullToRefreshBase<?> mRefreshedView; public GetDataTask(PullToRefreshBase<?> refreshedView) { mRefreshedView = refreshedView; } @Override protected Void doInBackground(Void... params) { // Simulates a background job. try { Thread.sleep(2000); } catch (InterruptedException e) { } return null; } @Override protected void onPostExecute(Void result) { mRefreshedView.onRefreshComplete(); super.onPostExecute(result); } }
MainActivity.java的全部代码
package com.example.ptrlistviewinviewpagertest; import java.util.ArrayList; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListAdapter; 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 ViewPager mViewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { // TODO 自动生成的方法存根 initViewPager(); } private void initViewPager() { // TODO 自动生成的方法存根 mViewPager = (ViewPager) findViewById(R.id.vp_list); //得到设置好的view列表 ArrayList<View> viewList = setListViewInVp(); mViewPager.setAdapter(new ListViewPagerAdapter(viewList)); } private ArrayList<View> setListViewInVp() { //设置给viewpager中填充的view列表 ArrayList<View> viewList = new ArrayList<View>(); //将string数组放入适配器中 final String[] data = { "1", "2", "3","4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17","18", "19", "20", }; ListAdapter adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, data); //找到listView的布局,生成可以下拉刷新的listView,添加到视图列表中 LayoutInflater inflater = LayoutInflater.from(getApplication()); for (int i = 0; i < 4; i++) { PullToRefreshListView plv = (PullToRefreshListView) inflater.inflate( R.layout.layout_listview_in_viewpager, null); plv.setAdapter(adapter); plv.setOnRefreshListener(new OnRefreshListener<ListView>() { @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) { // TODO 自动生成的方法存根 new GetDataTask(refreshView).execute(); } }); viewList.add(plv); } return viewList; } /** * @author:Jack Tony * @tips :viewPager的适配器 * @date :2014-10-14 */ public class ListViewPagerAdapter extends PagerAdapter { //设置list数组,传入view private ArrayList<View> mViewList; private int pagerNum = 0; public ListViewPagerAdapter(ArrayList<View> viewList) { mViewList = viewList; } public int getPagerNum() { return pagerNum; } @Override public int getCount() { return mViewList.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public void destroyItem(View arg0, int position, Object object) { if (mViewList.get(position) != null) { ((ViewPager) arg0).removeView(mViewList.get(position)); } } @Override public Object instantiateItem(View container, int position) { try { if (mViewList.get(position).getParent() == null) { ((ViewPager) container).addView(mViewList.get(position), 0); } else { /* * 很难理解新添加进来的view会自动绑定一个父类,由于一个儿子view不能与两个父类相关, * 所以得解绑不这样做否则会产生 viewpager java.lang.IllegalStateException: * The specified child already has a parent. You must call * removeView() on the child‘s parent first. */ ((ViewGroup) mViewList.get(position).getParent()) .removeView(mViewList.get(position)); ((ViewPager) container).addView(mViewList.get(position), 0); } } catch (Exception e) { e.printStackTrace(); } finally { pagerNum = position; } return mViewList.get(position); } } private static class GetDataTask extends AsyncTask<Void, Void, Void> { PullToRefreshBase<?> mRefreshedView; public GetDataTask(PullToRefreshBase<?> refreshedView) { mRefreshedView = refreshedView; } @Override protected Void doInBackground(Void... params) { // Simulates a background job. try { Thread.sleep(2000); } catch (InterruptedException e) { } return null; } @Override protected void onPostExecute(Void result) { mRefreshedView.onRefreshComplete(); super.onPostExecute(result); } } }
源码下载:http://download.csdn.net/detail/shark0017/8036901
以上是关于Android PullToRefreshListView和ViewPager的结合使用的主要内容,如果未能解决你的问题,请参考以下文章
Android 逆向Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )
Android 逆向Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )