pulltorefresh工程研究(二)

本文详细介绍了PullToRefresh控件在ExpandableListView、ViewPager及ListView中的应用,并提供了具体的实现代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

探究ExpandableListView 可展开的列表控件

/*******************************************************************************
 * Copyright 2011, 2012 Chris Banes.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.handmark.pulltorefresh.samples;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ExpandableListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshExpandableListView;

public final class PullToRefreshExpandableListActivity extends ExpandableListActivity {
    private static final String KEY = "key";

    private List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
    private List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();

    private PullToRefreshExpandableListView mPullRefreshListView;
    private SimpleExpandableListAdapter mAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ptr_expandable_list);
        //R.layout.activity_ptr_expandable_list在下面有写
        mPullRefreshListView = (PullToRefreshExpandableListView) findViewById(R.id.pull_refresh_expandable_list);

        // Set a listener to be invoked when the list should be refreshed.
        mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ExpandableListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ExpandableListView> refreshView) {
                // Do work to refresh the list here.
                new GetDataTask().execute();
            }
        });

        for (String group : mGroupStrings) {
            Map<String, String> groupMap = new HashMap<String, String>();
            groupData.add(groupMap);
            groupMap.put(KEY, group);
            //每次都插入相同的childList
            List<Map<String, String>> childList = new ArrayList<Map<String, String>>();
            for (String string : mChildStrings) {
                Map<String, String> childMap = new HashMap<String, String>();
                childList.add(childMap);
                childMap.put(KEY, string);
            }
            childData.add(childList);
        }
        //设置适配器...
        mAdapter = new SimpleExpandableListAdapter(this, groupData, android.R.layout.simple_expandable_list_item_1,
                new String[] { KEY }, new int[] { android.R.id.text1 }, childData,
                android.R.layout.simple_expandable_list_item_2, new String[] { KEY }, new int[] { android.R.id.text1 });
        setListAdapter(mAdapter);
    }

    private class GetDataTask extends AsyncTask<Void, Void, String[]> {

        @Override
        protected String[] doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            return mChildStrings;
        }

        @Override
        protected void onPostExecute(String[] result) {
            Map<String, String> newMap = new HashMap<String, String>();
            newMap.put(KEY, "Added after refresh...");
            groupData.add(newMap);

            List<Map<String, String>> childList = new ArrayList<Map<String, String>>();
            for (String string : mChildStrings) {
                Map<String, String> childMap = new HashMap<String, String>();
                childMap.put(KEY, string);
                childList.add(childMap);
            }
            childData.add(childList);

            mAdapter.notifyDataSetChanged();

            // Call onRefreshComplete when the list has been refreshed.
            mPullRefreshListView.onRefreshComplete();

            super.onPostExecute(result);
        }
    }

    private String[] mChildStrings = { "Child One", "Child Two", "Child Three", "Child Four", "Child Five", "Child Six" };

    private String[] mGroupStrings = { "Group One", "Group Two", "Group Three" };
}


activity_prt_expandable_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" >

    <!-- The PullToRefreshExpandableListView replaces a standard ExpandableListView widget. -->

    <com.handmark.pulltorefresh.library.PullToRefreshExpandableListView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/pull_refresh_expandable_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        ptr:ptrHeaderBackground="@android:color/darker_gray"
        ptr:ptrHeaderTextColor="@android:color/white"
        ptr:ptrMode="pullUpFromBottom" />

</LinearLayout>

探究ViewPager

PullToRefreshViewPagerActivity.java
/*******************************************************************************
 * Copyright 2011, 2012 Chris Banes.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.handmark.pulltorefresh.samples;

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.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

import com.handmark.pulltorefresh.extras.viewpager.PullToRefreshViewPager;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;

public class PullToRefreshViewPagerActivity extends Activity implements OnRefreshListener<ViewPager> {

    private PullToRefreshViewPager mPullToRefreshViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ptr_viewpager);

        mPullToRefreshViewPager = (PullToRefreshViewPager) findViewById(R.id.pull_refresh_viewpager);
        mPullToRefreshViewPager.setOnRefreshListener(this);

        ViewPager vp = mPullToRefreshViewPager.getRefreshableView();
        vp.setAdapter(new SamplePagerAdapter());
    }

    @Override
    public void onRefresh(PullToRefreshBase<ViewPager> refreshView) {
        new GetDataTask().execute();
    }

    static class SamplePagerAdapter extends PagerAdapter {
//必须重载下面几个方法...
        private static int[] sDrawables = { R.drawable.wallpaper, R.drawable.wallpaper, R.drawable.wallpaper,
                R.drawable.wallpaper, R.drawable.wallpaper, R.drawable.wallpaper };

        @Override
        public int getCount() {
            return sDrawables.length;
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(container.getContext());
            imageView.setImageResource(sDrawables[position]);

            // Now just add ImageView to ViewPager and return it
            container.addView(imageView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

            return imageView;
        }

        @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) {
            // Simulates a background job.
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
            }
            return null;
        }

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

}


activity_prt_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" >

    <!-- The PullToRefreshScrollView replaces a standard PullToRefreshScrollView widget. -->

    <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>

探究ListInViewPager(高级)

搞清楚每一个xml页面布局的含义…比如activity_ptr_list_in_vp.xml中,有一个viewpager, 然后这个viewpager在setAdapter的时候,创建 一个实例,这个实例在填充数据的时候,需要选择layout_listview_in_viewpager.xml,这样就能创建了…在使用viewpager的时候,一定要注意标签,是android.support.v4.view.ViewPager,而不是ViewPager

PullToRefreshListInViewPagerActivity.java
package com.handmark.pulltorefresh.samples;

import java.util.Arrays;

import android.app.Activity;
import android.content.Context;
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.view.ViewGroup.LayoutParams;
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 PullToRefreshListInViewPagerActivity extends Activity implements OnRefreshListener<ListView> {

    private static final String[] STRINGS = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance",
            "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
            "Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
            "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
            "Allgauer Emmentaler" };

    private ViewPager mViewPager;

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

        mViewPager = (ViewPager) findViewById(R.id.vp_list);
        mViewPager.setAdapter(new ListViewPagerAdapter());
    }

    private class ListViewPagerAdapter extends PagerAdapter {

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            Context context = container.getContext();

            PullToRefreshListView plv = (PullToRefreshListView) LayoutInflater.from(context).inflate(
                    R.layout.layout_listview_in_viewpager, container, false);

            ListAdapter adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1,
                    Arrays.asList(STRINGS));
            plv.setAdapter(adapter);

            plv.setOnRefreshListener(PullToRefreshListInViewPagerActivity.this);

            // Now just add ListView to ViewPager and return it
            container.addView(plv, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

            return plv;
        }

        @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 3;//这里就写了,返回3个页面listview组成一个pagerView的数据填充...
        }

    }

    @Override
    public void onRefresh(PullToRefreshBase<ListView> refreshView) {
        new GetDataTask(refreshView).execute();
    }

    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(4000);
            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            mRefreshedView.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" >

    <!-- 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>

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" />

以后有机会再更,之前的被保存了两遍草稿,所以上午写好了的都没有了,包括一些动画!!!以后会找回来的!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值