• Android 下拉刷新上拉载入效果功能


    应用场景:

    在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 

    使用方式:

    第一步:新建AndroidprojectSampleDemo
    第二步:在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),增加到自己的项目中的指定包内。


    该演示用例涉及的类文件为:
    【librarysrccomhandmarkpulltorefreshlibrary】
    PullToRefreshAdapterViewBase.java
    PullToRefreshBase.java
    PullToRefreshListView.java
    【librarysrccomhandmarkpulltorefreshlibraryinternal】
    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;  //实现下拉刷新与上拉载入的ListView
    	private 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;
    	}
    	
    	@Override
    	protected String doInBackground(Void... params) {
    		try {
    			Thread.sleep(1000);
    		} catch (InterruptedException e) {
    		}
    		return "StringTest";
    	}
    
    	@Override
    	protected 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;
    	}
    	
    	@Override
    	public int getCount() {
    		return linkedList.size();
    	}
    
    	@Override
    	public Object getItem(int position) {
    		return linkedList.get(position);
    	}
    
    	@Override
    	public long getItemId(int position) {
    		return position;
    	}
    
    	@Override
    	public 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" 
    
    };
    	
    	@Override
    	protected 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();
    		}
    	};
    }

    下载地址:


  • 相关阅读:
    C#对文件的操作
    Quartz使用
    北邮校园网自动登录 python
    interactive python ---- week5
    interactive python ----pong game
    自旋锁(spinlock)(转)
    interactive python(3)
    interactive python(2)
    Ubuntu下为Firefox安装Adobe Flash Player(转)
    flex中list 控件行中添加Button后的点击事件处理
  • 原文地址:https://www.cnblogs.com/cxchanpin/p/6834552.html
Copyright © 2020-2023  润新知