Android 下拉重新整理上拉載入效果功能

來源:互聯網
上載者:User

標籤:listview   下拉重新整理   上拉載入   android   linkedlist   

應用情境:

在App開發中,對於資訊的擷取與示範,不可能全部將其擷取與示範,為了在使用者使用中,給予使用者以友好、方便的使用者體驗,以滑動、下拉的效果動態載入資料的要求就會出現。為此,該效果功能就需要應用到所需要的展示頁面中。

知識點介紹:本文主要根據開源項目android-pulltorefresh展開介紹。
android-pulltorefresh 
【一個強大的拉動重新整理開源項目,支援各種控制項下拉重新整理 ListView、ViewPager、WevView、ExpandableListView、GridView、(Horizontal )ScrollView、Fragment上下左右拉動重新整理,比下面johannilsson那個只支援ListView的強大的多。並且他實現的下拉重新整理ListView在item不足一屏情況下也不會顯示重新整理提示,體驗更好。】 
項目地址:
https://github.com/chrisbanes/Android-PullToRefresh 
Demo地址:
https://github.com/Trinea/TrineaDownload/blob/master/pull-to-refreshview-demo.apk?raw=true 
使用方式:第一步:建立Android工程SampleDemo
第二步:在res/values下建立attrs.xml
<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="PullToRefresh">        <attr name="mode" format="reference" >            <flag name="pullDownFromTop" value="0x1" />            <flag name="pullUpFromBottom" value="0x2" />            <flag name="both" value="0x3" />        </attr>    </declare-styleable></resources>srings.xml<?xml version="1.0" encoding="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>
第三步:將所需要的圖片檔案放入相應的檔案夾下面,所用的圖片檔案有:
第四步:
1、匯入或將開源項目android-pulltorefresh中需要的類檔案(.java),加入到自己的項目中的指定包內。
該示範用例涉及的類檔案為:
【library\src\com\handmark\pulltorefresh\library】
PullToRefreshAdapterViewBase.java
PullToRefreshBase.java
PullToRefreshListView.java
【library\src\com\handmark\pulltorefresh\library\internal】
EmptyViewMethodAccessor.java
LoadingLayout.java
2、構建自己所需要的類檔案(.java)。
【PullTask.java】
import java.util.LinkedList;import com.example.sampledemo.view.PullToRefreshListView;import android.os.AsyncTask;import android.widget.BaseAdapter;public class PullTask extends AsyncTask<Void, Void, String>{private PullToRefreshListView pullToRefreshListView;  //實現下拉重新整理與上拉載入的ListViewprivate int pullState;               //記錄判斷,上拉與下拉動作private BaseAdapter baseAdapter;     //ListView適配器,用於提醒ListView資料已經更新private LinkedList<String> linkedList;public PullTask(PullToRefreshListView pullToRefreshListView, int pullState,BaseAdapter baseAdapter, LinkedList<String> linkedList) {this.pullToRefreshListView = pullToRefreshListView;this.pullState = pullState;this.baseAdapter = baseAdapter;this.linkedList = linkedList;}@Overrideprotected String doInBackground(Void... params) {try {Thread.sleep(1000);} catch (InterruptedException e) {}return "StringTest";}@Overrideprotected void onPostExecute(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);}}

【PullAdapter.java】
import java.util.LinkedList;import com.example.sampledemo.R;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;public class PullAdapter extends BaseAdapter {private LinkedList<String> linkedList;private LayoutInflater mInflater;public PullAdapter(LinkedList<String> linkedList, Context context) {mInflater = LayoutInflater.from(context);this.linkedList = linkedList;}@Overridepublic int getCount() {return linkedList.size();}@Overridepublic Object getItem(int position) {return linkedList.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder=null;if (convertView == null) {holder = new ViewHolder();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){final String dataStr = linkedList.get(position);holder.textView.setText(dataStr);}return convertView;}private static class ViewHolder {TextView textView;        //資料顯示地區}}

3、為PullAdapter.java 設計布局檔案layout_main_listitem.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#FFFFFF"    android:orientation="vertical" >    <TextView        android:id="@+id/textView"        android:textColor="#99CC66"        android:textSize="18dp"        android:layout_marginTop="4dp"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="left" /></LinearLayout>

滑動時出現提醒布局檔案pull_to_refresh_header.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:paddingTop="10dp"    android:paddingBottom="10dip">     <TextView        android:id="@+id/pull_to_refresh_text"        android:text="@string/pull_to_refresh_pull_down_label"        android:textAppearance="?android:attr/textAppearanceMedium"        android:textStyle="bold"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true" />    <ProgressBar        android:id="@+id/pull_to_refresh_progress"        android:indeterminate="true"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="30dip"        android:layout_marginRight="20dip"        android:visibility="gone"        android:layout_centerVertical="true"        style="?android:attr/progressBarStyleSmall" />     <ImageView        android:id="@+id/pull_to_refresh_image"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="30dip"        android:layout_marginRight="20dip"        android:layout_centerVertical="true" /></RelativeLayout>

MainActivity.java 主布局檔案activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools" xmlns:cp="http://schemas.android.com/apk/res/com.example.sampledemo"    android:layout_width="match_parent"    android:background="#FFFFFF"    android:layout_height="match_parent"><com.example.sampledemo.view.PullToRefreshListView    android:id="@+id/pullrefresh"    android:background="#FFFFFF"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:divider="@android:color/black"    android:dividerHeight="0.1dip"    android:cacheColorHint="#00000000" cp:mode="both"></com.example.sampledemo.view.PullToRefreshListView></RelativeLayout>

4、編寫MainActivity.java 
import java.util.Arrays;import java.util.LinkedList;import com.example.sampledemo.view.PullToRefreshBase.OnRefreshListener;import com.example.sampledemo.view.PullToRefreshListView;import com.example.sampledemo.view.adapter.PullAdapter;import com.example.sampledemo.view.task.PullTask;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.ListView;import android.app.Activity;/** * @ClassName MainActivity.java * @Author MaHaochen * @Date 2014-4-30 15:56:47 */public class MainActivity extends Activity {private LinkedList<String> mListItems;private PullToRefreshListView mPullRefreshListView;private ArrayAdapter<String> mAdapter;private ListView mListView;private PullAdapter pullAdapter;private String[] mStrings = { "初始資料01","初始資料02","初始資料03","初始資料04","初始資料05" };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initViews();}private void initViews() {mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pullrefresh);mPullRefreshListView.setOnRefreshListener(mOnrefreshListener);mListView = mPullRefreshListView.getRefreshableView();mListItems = new LinkedList<String>();mListItems.addAll(Arrays.asList(mStrings));pullAdapter = new PullAdapter(mListItems, MainActivity.this);mListView.setAdapter(pullAdapter);}OnRefreshListener mOnrefreshListener = new OnRefreshListener() {public void onRefresh() {PullTask pullTask =new PullTask(mPullRefreshListView, mPullRefreshListView.getRefreshType(), pullAdapter, mListItems);pullTask.execute();}};}

:http://download.csdn.net/detail/ma_hoking/7276365

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.