码迷,mamicode.com
首页 > 移动开发 > 详细

Android 下拉刷新上拉加载效果功能,使用开源项目android-pulltorefresh实现

时间:2014-07-10 22:18:46      阅读:353      评论:0      收藏:0      [点我收藏+]

标签:下拉刷新   上拉加载   listview   android-pulltorefres   开源项目   

应用场景:

在App开发中,对于信息的获取与演示,不可能全部将其获取与演示,为了在用户使用中,给予用户以友好、方便的用户体验,以滑动、下拉的效果动态加载数据的要求就会出现。为此,该效果功能就需要应用到所需要的展示页面中。

知识点介绍:

本文主要根据开源项目android-pulltorefresh展开介绍。
android-pulltorefresh
【一个强大的拉动刷新开源项目,支持各种控件下拉刷新 ListView、ViewPager、WevView、ExpandableListView、GridView、(Horizontal )ScrollView、Fragment上下左右拉动刷新,比下面johannilsson那个只支持ListView的强大的多。并且他实现的下拉刷新ListView在item不足一屏情况下也不会显示刷新提示,体验更好。】
 
第一步:新建Android工程SampleDemo
第二步:在res/values下新建attrs.xml
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!--?xml version=1.0encoding=utf-8?-->
<resources>
    <declare-styleable name="PullToRefresh">
         
            <flag name="pullDownFromTop"value="0x1">
            <flag name="pullUpFromBottom"value="0x2">
            <flag name="both"value="0x3">
        </flag></flag></flag></attr>
    </declare-styleable>
</resources>
srings.xml
<!--?xml version=1.0encoding=utf-8?-->
<resources>
    <string name="app_name">SampleDemo</string>
    <string name="action_settings">Settings</string>
    <string name="pull_to_refresh_pull_down_label">滑动刷新</string>
    <string name="pull_to_refresh_release_label">释放刷新</string>
    <string name="pull_to_refresh_refreshing_label">加载中</string>
    <string name="pull_to_refresh_tap_label">点击刷新</string>
</resources>
第三步:将所需要的图片文件放入相应的文件夹下面,所用的图片文件有: bubuko.com,布布扣
第四步:
1、导入或将开源项目android-pulltorefresh中需要的类文件(.java),加入到自己的项目中的指定包内。
该演示用例涉及的类文件为:
【library src com.handmark.pulltorefresh.library】
PullToRefreshAdapterViewBase.java
PullToRefreshBase.java
PullToRefreshListView.java
【library src com.handmark.pull.torefresh.library.internal】
EmptyViewMethodAccessor.java
LoadingLayout.java
2、构建自己所需要的类文件(.java)。
【PullTask.java】
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
importjava.util.LinkedList;
 
importcom.example.sampledemo.view.PullToRefreshListView;
 
importandroid.os.AsyncTask;
importandroid.widget.BaseAdapter;
 
publicclassPullTask extendsAsyncTask<void, string="">{
 
    privatePullToRefreshListView pullToRefreshListView;  //实现下拉刷新与上拉加载的ListView
    privateintpullState;               //记录判断,上拉与下拉动作
    privateBaseAdapter baseAdapter;     //ListView适配器,用于提醒ListView数据已经更新
    privateLinkedList<string> linkedList;
     
    publicPullTask(PullToRefreshListView pullToRefreshListView, intpullState,
            BaseAdapter baseAdapter, LinkedList<string> linkedList) {
        this.pullToRefreshListView = pullToRefreshListView;
        this.pullState = pullState;
        this.baseAdapter = baseAdapter;
        this.linkedList = linkedList;
    }
     
    @Override
    protectedString doInBackground(Void... params) {
        try{
            Thread.sleep(1000);
        }catch(InterruptedException e) {
        }
        returnStringTest;
    }
 
    @Override
    protectedvoidonPostExecute(String result) {
        if(pullState == 1) {//name=pullDownFromTop value=0x1 下拉
            linkedList.addFirst(顶部数据);
        }
        if(pullState == 2) {//name=pullUpFromBottom value=0x2 上拉
            linkedList.addLast(底部数据);
        }
        baseAdapter.notifyDataSetChanged();
        pullToRefreshListView.onRefreshComplete();
        super.onPostExecute(result);
    }
}</string></string></void,>

【PullAdapter.java】
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
importjava.util.LinkedList;
 
importcom.example.sampledemo.R;
 
importandroid.content.Context;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.BaseAdapter;
importandroid.widget.TextView;
 
publicclassPullAdapter extendsBaseAdapter {
 
    privateLinkedList<string> linkedList;
    privateLayoutInflater mInflater;
     
    publicPullAdapter(LinkedList<string> linkedList, Context context) {
        mInflater = LayoutInflater.from(context);
        this.linkedList = linkedList;
    }
     
    @Override
    publicintgetCount() {
        returnlinkedList.size();
    }
 
    @Override
    publicObject getItem(intposition) {
        returnlinkedList.get(position);
    }
 
    @Override
    publiclonggetItemId(intposition) {
        returnposition;
    }
 
    @Override
    publicView getView(intposition, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
 
        if(convertView == null) {
            holder = newViewHolder();
            convertView = mInflater.inflate(R.layout.layout_main_listitem, null);
            holder.textView = (TextView) convertView.findViewById(R.id.textView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        if(linkedList.size()>0){
            finalString dataStr = linkedList.get(position);
            holder.textView.setText(dataStr);
        }
        returnconvertView;
    }
 
    privatestaticclass ViewHolder {
        TextView textView;        //数据显示区域
    }
}</string></string>

第四步:为PullAdapter.java 设计布局文件layout_main_listitem.xml
?
1
2
3
4
<!--?xml version=1.0encoding=utf-8?-->
<linearlayout android:background="#FFFFFF"android:layout_height="match_parent"android:layout_width="match_parent"android:orientation="vertical"xmlns:android="http://schemas.android.com/apk/res/android">
    <textview android:gravity="left"android:id="@+id/textView"android:layout_height="wrap_content"android:layout_margintop="4dp"android:layout_width="match_parent"android:textcolor="#99CC66"android:textsize="18dp">
</textview></linearlayout>

滑动时出现提醒布局文件pull_to_refresh_header.xml
?
1
2
3
4
5
6
<!--?xml version=1.0encoding=utf-8?-->
<relativelayout android:layout_height="fill_parent"android:layout_width="fill_parent"android:paddingbottom="10dip"android:paddingtop="10dp"xmlns:android="http://schemas.android.com/apk/res/android">
     <textview android:id="@+id/pull_to_refresh_text"android:layout_centerinparent="true"android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="@string/pull_to_refresh_pull_down_label"android:textappearance="?android:attr/textAppearanceMedium"android:textstyle="bold">
    <progressbar android:id="@+id/pull_to_refresh_progress"android:indeterminate="true"android:layout_centervertical="true"android:layout_height="wrap_content"android:layout_marginleft="30dip"android:layout_marginright="20dip"android:layout_width="wrap_content"android:visibility="gone"style="?android:attr/progressBarStyleSmall">
    <imageview android:id="@+id/pull_to_refresh_image"android:layout_centervertical="true"android:layout_height="wrap_content"android:layout_marginleft="30dip"android:layout_marginright="20dip"android:layout_width="wrap_content">
</imageview></progressbar></textview></relativelayout>

MainActivity.java 主布局文件activity_main.xml
?
1
2
3
4
<relativelayout android:background="#FFFFFF"android:layout_height="match_parent"android:layout_width="match_parent"xmlns:android="http://schemas.android.com/apk/res/android"xmlns:cp="http://schemas.android.com/apk/res/com.example.sampledemo"xmlns:tools="http://schemas.android.com/tools">
    <com.example.sampledemo.view.pulltorefreshlistview android:background="#FFFFFF"android:cachecolorhint="#00000000"android:divider="@android:color/black"android:dividerheight="0.1dip"android:id="@+id/pullrefresh"android:layout_height="fill_parent"android:layout_width="fill_parent"cp:mode="both">
    </com.example.sampledemo.view.pulltorefreshlistview>
</relativelayout>

第五步:编写MainActivity.java
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
importjava.util.Arrays;
importjava.util.LinkedList;
 
importcom.example.sampledemo.view.PullToRefreshBase.OnRefreshListener;
importcom.example.sampledemo.view.PullToRefreshListView;
importcom.example.sampledemo.view.adapter.PullAdapter;
importcom.example.sampledemo.view.task.PullTask;
 
importandroid.os.Bundle;
importandroid.widget.ArrayAdapter;
importandroid.widget.ListView;
importandroid.app.Activity;
/**
 * @ClassName MainActivity.java
 * @Author MaHaochen
 * @Date 2014-4-30 15:56:47
 */
publicclassMainActivity extendsActivity {
    privateLinkedList<string> mListItems;
    privatePullToRefreshListView mPullRefreshListView;
    privateArrayAdapter<string> mAdapter;
    privateListView mListView;
    privatePullAdapter pullAdapter;
    privateString[] mStrings = { 初始数据01,初始数据02,初始数据03,初始数据04,初始数据05
 
};
     
    @Override
    protectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }
 
    privatevoidinitViews() {
        mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pullrefresh);
        mPullRefreshListView.setOnRefreshListener(mOnrefreshListener);
        mListView = mPullRefreshListView.getRefreshableView();
        mListItems = newLinkedList<string>();
        mListItems.addAll(Arrays.asList(mStrings));
        pullAdapter = newPullAdapter(mListItems, MainActivity.this);
        mListView.setAdapter(pullAdapter);
    }
     
    OnRefreshListener mOnrefreshListener = newOnRefreshListener() {
        publicvoidonRefresh() {
        PullTask pullTask = newPullTask(mPullRefreshListView,
 
mPullRefreshListView.getRefreshType(), pullAdapter, mListItems);
        pullTask.execute();
        }
    };
}</string></string></string>

下载地址:http://download.csdn.net/detail/fngy123/7611567

Android 下拉刷新上拉加载效果功能,使用开源项目android-pulltorefresh实现,布布扣,bubuko.com

Android 下拉刷新上拉加载效果功能,使用开源项目android-pulltorefresh实现

标签:下拉刷新   上拉加载   listview   android-pulltorefres   开源项目   

原文地址:http://blog.csdn.net/fngy123/article/details/37597975

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!