PullToRefreshListView 示例代码


Listview
public final class PullToRefreshListActivity extends ListActivity {
    private LinkedList<String> mListItems;
    private PullToRefreshListView mPullRefreshListView;
    private ArrayAdapter<String> mAdapter;
    private String[] mStrings = { "1""2""3""4""5""6""7""8""9""包青天""1""2""3""4""5""6""7""8""9""包青天" };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ptr_list);
        mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
        mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                //日期格式
                String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME
                        | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
                // 更新最后刷新时间
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
                // Do work to refresh the list here.
                new GetDataTask().execute();
            }
        });
        // Add an end-of-list listener
        mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                Toast.makeText(PullToRefreshListActivity.this"滑到了最后一条", Toast.LENGTH_SHORT).show();
            }
        });
        //获取真正的ListView
        ListView actualListView = mPullRefreshListView.getRefreshableView();
        // Need to use the Actual ListView when registering for Context Menu
        registerForContextMenu(actualListView);
        mListItems = new LinkedList<String>();
        mListItems.addAll(Arrays.asList(mStrings));
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1mListItems);
        //添加声音事件
        SoundPullEventListener<ListView> soundListener = new SoundPullEventListener<ListView>(this);
        soundListener.addSoundEvent(State.PULL_TO_REFRESH, R.raw.pull_event);
        soundListener.addSoundEvent(State.RESET, R.raw.reset_sound);
        soundListener.addSoundEvent(State.REFRESHING, R.raw.refreshing_sound);
        mPullRefreshListView.setOnPullEventListener(soundListener);
        // You can also just use setListAdapter(mAdapter) or mPullRefreshListView.setAdapter(mAdapter)
        actualListView.setAdapter(mAdapter);
    }
    //**************************************************************************************************************************
    private class GetDataTask extends AsyncTask<Void, Void, String[]> {
        @Override
        protected String[] doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            return mStrings;
        }
        @Override
        protected void onPostExecute(String[] result) {
            mListItems.addFirst("Added after refresh...");
            mAdapter.notifyDataSetChanged();
            // Call onRefreshComplete when the list has been refreshed.
            mPullRefreshListView.onRefreshComplete();
            super.onPostExecute(result);
        }
    }
}

activity_ptr_list.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" >
    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/pull_refresh_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:divider="#19000000"
        android:dividerHeight="4dp"
        android:fadingEdge="none"
        android:fastScrollEnabled="false"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:smoothScrollbar="true" />
</LinearLayout>


GridView
public final class GridViewActivity extends Activity {
    private LinkedList<String> mListItems;
    private PullToRefreshGridView mPullRefreshGridView;
    private GridView mGridView;
    private ArrayAdapter<String> mAdapter;
    private boolean isLast;//true对应onPullUpToRefresh,代表拉到【最后】时【从下往上】拉
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ptr_grid);
        mPullRefreshGridView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
        mGridView = mPullRefreshGridView.getRefreshableView();
        mPullRefreshGridView.setOnRefreshListener(new OnRefreshListener2<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
                Toast.makeText(GridViewActivity.this"下拉!", Toast.LENGTH_SHORT).show();
                isLast = false;
                new GetDataTask().execute();
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                Toast.makeText(GridViewActivity.this"上拉!", Toast.LENGTH_SHORT).show();
                isLast = true;
                new GetDataTask().execute();
            }
        });
        mListItems = new LinkedList<String>();
        TextView tv = new TextView(this);
        tv.setGravity(Gravity.CENTER);
        tv.setText("下拉或上拉添加条目");
        mPullRefreshGridView.setEmptyView(tv);
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1mListItems);
        mGridView.setAdapter(mAdapter);
    }
    private class GetDataTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            if (isLast) {
                mListItems.addLast("新增条目");
            } else {
                String[] mStrings = { "1""2""3""4""5""6""7""8""9" };
                mListItems.addAll(Arrays.asList(mStrings));
            }
            mAdapter.notifyDataSetChanged();
            mPullRefreshGridView.onRefreshComplete();
            super.onPostExecute(result);
        }
    }
}

activity_ptr_grid.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" >
    <com.handmark.pulltorefresh.library.PullToRefreshGridView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/pull_refresh_grid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="100dp"
        android:gravity="fill"
        android:horizontalSpacing="1dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="1dp"
        ptr:ptrDrawable="@drawable/android"
        ptr:ptrMode="both" />
</LinearLayout>


ViewPage
public class ViewPagerActivity extends Activity implements OnRefreshListener2<ViewPager> {
    private PullToRefreshViewPager mPullToRefreshViewPager;//封装过的带刷新功能的ViewPager
    private ViewPager viewPager;//代表原始的ViewPager,某些API若PullToRefreshViewPager没有提供就找原始的ViewPager
    private MyPagerAdapter mAdapter;
    private LinkedList<Integer> mListItems;//ViewPager的Adapter中封装的数据,封装的都是图片的id
    private boolean isLast;//true对应onPullUpToRefresh,代表拉到【最后】时【从右往左】拉
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ptr_viewpager);
        mListItems = new LinkedList<Integer>();
        mListItems.add(R.drawable.wallpaper);
        mListItems.add(R.drawable.wallpaper);
        mListItems.add(R.drawable.wallpaper);
        mPullToRefreshViewPager = (PullToRefreshViewPager) findViewById(R.id.pull_refresh_viewpager);
        mPullToRefreshViewPager.setOnRefreshListener(this);
        viewPager = mPullToRefreshViewPager.getRefreshableView();
        mAdapter = new MyPagerAdapter();
        viewPager.setAdapter(mAdapter);
    }
    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ViewPager> refreshView) {
        isLast = false;
        new GetDataTask().execute();
    }
    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ViewPager> refreshView) {
        isLast = true;
        new GetDataTask().execute();
    }
    //************************************************************************************************************
    private class MyPagerAdapter extends PagerAdapter {
        @Override
        public View instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(container.getContext());
            imageView.setImageResource(mListItems.get(position));
            container.addView(imageView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            return imageView;
        }
        @Override
        public int getCount() {
            return mListItems.size();
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }
    //************************************************************************************************************
    private class GetDataTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            if (isLast) {
                mListItems.addLast(R.drawable.android);
                mAdapter.notifyDataSetChanged();
                viewPager.setCurrentItem(mListItems.size() - 1);//这里超出范围了也不报错,比如写成+10,估计是内部做了范围判断
            } else {
                mListItems.addFirst(R.drawable.icon);
                mAdapter.notifyDataSetChanged();
                viewPager.setAdapter(mAdapter);//卧槽,需要重新设置一下Adapter,以后遇到不能正常加载条目的情况都这么干,屡试不爽!
                viewPager.setCurrentItem(0);//这里超出范围同样也不报错,比如写成-10
            }
            mPullToRefreshViewPager.onRefreshComplete();
            super.onPostExecute(result);
        }
    }
}

activity_ptr_viewpager.xml

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <com.handmark.pulltorefresh.extras.viewpager.PullToRefreshViewPager
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/pull_refresh_viewpager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        ptr:ptrAnimationStyle="flip"
        ptr:ptrHeaderBackground="@android:color/darker_gray"
        ptr:ptrMode="both" />
</FrameLayout>


ListView+Fragment
public final class ListView_Fragment extends FragmentActivity implements OnRefreshListener<ListView> {
    private PullToRefreshListView mPullRefreshListView;
    private ArrayAdapter<String> mAdapter;//适配器
    private LinkedList<String> mListItems;//数据
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ptr_list_fragment);
        PullToRefreshListFragment mPullRefreshListFragment = (PullToRefreshListFragment) getSupportFragmentManager().findFragmentById(
                R.id.frag_ptr_list);
        mPullRefreshListView = mPullRefreshListFragment.getPullToRefreshListView();
        mPullRefreshListView.setMode(Mode.BOTH);//OnRefreshListener也可以实现上下刷新
        mPullRefreshListView.setOnRefreshListener(this);
        String[] mStrings = { "1""2""3""4""5""6""7""8""9" };
        mListItems = new LinkedList<String>();
        mListItems.addAll(Arrays.asList(mStrings));
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1mListItems);
        ListView actualListView = mPullRefreshListView.getRefreshableView();//或使用getListView()
        actualListView.setAdapter(mAdapter);//也可直接用mPullRefreshListView设置
        mPullRefreshListFragment.setListShown(true);//是否显示内容,必须调用!不知道具体是啥玩意
    }
    @Override
    public void onRefresh(PullToRefreshBase<ListView> refreshView) {
        new GetDataTask().execute();
    }
    private class GetDataTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            mListItems.addFirst("新增");
            mAdapter.notifyDataSetChanged();
            mPullRefreshListView.onRefreshComplete();
            super.onPostExecute(result);
        }
    }
}

activity_ptr_list_fragment.xml

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <fragment
        android:id="@+id/frag_ptr_list"
        android:name="com.handmark.pulltorefresh.extras.listfragment.PullToRefreshListFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>


ViewPage+ListView

// 下拉(从上往下拉)在最上面添加数据,上拉在最下面添加数据,[左拉向右边添加数据,右拉向左边添加数据]

public class ViewPage_ListView extends Activity implements OnRefreshListener2<ListView> {
    private Context context = ViewPage_ListView.this;
    private ViewPager mViewPager;//用的是原生的ViewPager
    private MyPagerAdapter mPagerAdapter;//ViewPager的适配器
    private LinkedList<LinkedList<String>> mPagerItems = new LinkedList<LinkedList<String>>();//ViewPager中的封装的数据
    private LinkedList<String> mListItems0 = new LinkedList<String>();//ViewPager中第0个ListView的数据
    private LinkedList<String> mListItems1 = new LinkedList<String>();//ViewPager中第1个ListView的数据
    private LinkedList<String> mListItems2 = new LinkedList<String>();//ViewPager中第2个ListView的数据
    private LinkedList<String> mListItems3 = new LinkedList<String>();//ViewPager中第3个ListView的数据
    private LinkedList<ArrayAdapter<String>> mListAdapters = new LinkedList<ArrayAdapter<String>>();//封装ListView的适配器
    private ArrayAdapter<String> mListAdapter0;//第0个ListView的适配器
    private ArrayAdapter<String> mListAdapter1;//第1个ListView的适配器
    private ArrayAdapter<String> mListAdapter2;//第2个ListView的适配器
    private ArrayAdapter<String> mListAdapter3;//第3个ListView的适配器
    private PullToRefreshBase<ListView> currentPTRefreshView;//代表当前条目的ListView
    private boolean isLast;//true对应onPullUpToRefresh,代表拉到【最后】时【从下往上】拉
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ptr_list_in_vp);
        //初始化ViewPager中的封装的数据
        mListItems0.addAll(Arrays.asList(new String[] { "0""0""0""0""0""0""0""0""0" }));
        mListItems1.addAll(Arrays.asList(new String[] { "1""1""1""1""1""1""1""1""1" }));
        mListItems2.addAll(Arrays.asList(new String[] { "2""2""2""2""2""2""2""2""2" }));
        mListItems3.addAll(Arrays.asList(new String[] { "3""3""3""3""3""3""3""3""3" }));
        mPagerItems.add(mListItems0);
        mPagerItems.add(mListItems1);
        mPagerItems.add(mListItems2);
        mPagerItems.add(mListItems3);
        //初始化ListView的适配器
        mListAdapter0 = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1mListItems0);
        mListAdapter1 = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1mListItems1);
        mListAdapter2 = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1mListItems2);
        mListAdapter3 = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1mListItems3);
        //将ListView的适配器封装到一个集合中
        mListAdapters.add(mListAdapter0);
        mListAdapters.add(mListAdapter1);
        mListAdapters.add(mListAdapter2);
        mListAdapters.add(mListAdapter3);
        mViewPager = (ViewPager) findViewById(R.id.vp_list);
        mPagerAdapter = new MyPagerAdapter();
        mViewPager.setAdapter(mPagerAdapter);
    }
    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        this.currentPTRefreshView = refreshView;//标识你刷新的是哪个ListView
        this.isLast = false;
        new GetDataTask().execute();
    }
    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        this.currentPTRefreshView = refreshView;
        this.isLast = true;
        new GetDataTask().execute();
    }
    //**************************************************************************************************************************
    private class MyPagerAdapter extends PagerAdapter {
        @Override
        public View instantiateItem(ViewGroup container, int position) {
            //初始化ViewPager中的View,此View是一个PullToRefreshListView
            PullToRefreshListView mListView = (PullToRefreshListView) LayoutInflater.from(context).inflate(R.layout.layout_listview_in_viewpager,
                    container, false);
            //ListView的适配器
            mListView.setAdapter(mListAdapters.get(position));
            mListView.setOnRefreshListener(ViewPage_ListView.this);
            //将要显示的条目加入到ViewGroup的缓存容器中,然后作为返回值返回
            container.addView(mListView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            return mListView;
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
        @Override
        public int getCount() {
            return mPagerItems.size();
        }
    }
    //**************************************************************************************************************************
    private class GetDataTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            int position = mViewPager.getCurrentItem();//ViewPager当前的条目
            if (isLast) {
                switch (position) {
                case 0:
                    for (int i = 0; i < 1; i++) {
                        mPagerItems.get(position).addLast("现在是ViewPager第0个条目");
                        mPagerItems.get(position).addLast("上拉时在最后新增一条数据");
                    }
                    break;
                case 1:
                    for (int i = 0; i < 1; i++) {
                        mPagerItems.get(position).addLast("现在是ViewPager第1个条目");
                        mPagerItems.get(position).addLast("上拉时在最后新增一条数据");
                    }
                    break;
                case 2:
                    for (int i = 0; i < 1; i++) {
                        mPagerItems.get(position).addLast("现在是ViewPager第2个条目");
                        mPagerItems.get(position).addLast("上拉时在最后新增一条数据");
                    }
                    break;
                case 3:
                    for (int i = 0; i < 1; i++) {
                        mPagerItems.get(position).addLast("现在是ViewPager第3个条目");
                        mPagerItems.get(position).addLast("上拉时在最后新增一条数据");
                    }
                    break;
                }
            } else {
                mPagerItems.get(position).addFirst("现在是ViewPager第" + position + "个条目");
                mPagerItems.get(position).addFirst("下拉时在最前新增一条数据");
            }
            mListAdapters.get(position).notifyDataSetChanged();
            mPagerAdapter.notifyDataSetChanged();
            currentPTRefreshView.onRefreshComplete();
            super.onPostExecute(result);
        }
    }
}

activity_ptr_list_in_vp.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.support.v4.view.ViewPager
        android:id="@+id/vp_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

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:ptrMode="both" />


ScrollView
public final class ScrollViewActivity extends Activity {
    PullToRefreshScrollView mPullRefreshScrollView;
    ScrollView mScrollView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ptr_scrollview);
        mPullRefreshScrollView = (PullToRefreshScrollView) findViewById(R.id.pull_refresh_scrollview);
        mPullRefreshScrollView.setOnRefreshListener(new OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                new GetDataTask().execute();
            }
        });
        mScrollView = mPullRefreshScrollView.getRefreshableView();
    }
    private class GetDataTask extends AsyncTask<Void, Void, String[]> {
        @Override
        protected String[] doInBackground(Void... params) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            return null;
        }
        @Override
        protected void onPostExecute(String[] result) {
            mPullRefreshScrollView.onRefreshComplete();
            super.onPostExecute(result);
        }
    }
}

activity_ptr_scrollview.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" >
    <com.handmark.pulltorefresh.library.PullToRefreshScrollView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/pull_refresh_scrollview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        ptr:ptrAnimationStyle="flip"
        ptr:ptrMode="both" >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="8dp"
            android:text="@string/filler_text"
            android:textSize="16sp" />
    </com.handmark.pulltorefresh.library.PullToRefreshScrollView>
</LinearLayout>


水平的ScrollView
public final class Horizontal_ScrollView extends Activity {
    PullToRefreshHorizontalScrollView mPullRefreshScrollView;
    HorizontalScrollView mScrollView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ptr_horizontalscrollview);
        mPullRefreshScrollView = (PullToRefreshHorizontalScrollView) findViewById(R.id.pull_refresh_horizontalscrollview);
        mPullRefreshScrollView.setOnRefreshListener(new OnRefreshListener<HorizontalScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<HorizontalScrollView> refreshView) {
                new GetDataTask().execute();
            }
        });
        mScrollView = mPullRefreshScrollView.getRefreshableView();
    }
    private class GetDataTask extends AsyncTask<Void, Void, String[]> {
        @Override
        protected String[] doInBackground(Void... params) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            return null;
        }
        @Override
        protected void onPostExecute(String[] result) {
            mPullRefreshScrollView.onRefreshComplete();
            super.onPostExecute(result);
        }
    }
}

activity_ptr_horizontalscrollview.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" >
    <com.handmark.pulltorefresh.library.PullToRefreshHorizontalScrollView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/pull_refresh_horizontalscrollview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        ptr:ptrAnimationStyle="flip"
        ptr:ptrMode="both" >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >
            <TextView
                style="@style/HorizScrollViewItem"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:background="#ff99cc00" />
            <TextView
                style="@style/HorizScrollViewItem"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:background="#ff33b5e5" />
            <TextView
                style="@style/HorizScrollViewItem"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:background="#ffcc0000" />
            <TextView
                style="@style/HorizScrollViewItem"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:background="#ff669900" />
        </LinearLayout>
    </com.handmark.pulltorefresh.library.PullToRefreshHorizontalScrollView>
</LinearLayout>

styles 样式

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="HorizScrollViewItem">
        <item name="android:paddingRight">50dp</item>
        <item name="android:paddingLeft">50dp</item>
        <item name="android:gravity">center</item>
        <item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
        <item name="android:text">Sample Item</item>
    </style>
</resources>


WebView
public   final   class  WebViewActivity  extends  Activity {
    PullToRefreshWebView mPullRefreshWebView;
    WebView mWebView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ptr_webview);
        mPullRefreshWebView = (PullToRefreshWebView) findViewById(R.id.pull_refresh_webview);
        mWebView = mPullRefreshWebView.getRefreshableView();
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new SampleWebViewClient());
        mWebView.loadUrl("http://news.sina.com.cn/");
    }
    private static class SampleWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}

activity_ptr_webview.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <com.handmark.pulltorefresh.library.PullToRefreshWebView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/pull_refresh_webview"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        ptr:ptrMode="both" />
</LinearLayout>




来自为知笔记(Wiz)


附件列表

     

    你可能感兴趣的:(PullToRefreshListView 示例代码)