PullToRefreshListView进阶(四)----->上拉分批加载2种方式
来源:互联网 发布:山脉户外 知乎 编辑:程序博客网 时间:2024/06/14 08:41
*********************************************上拉分批加载方式1****************************************************
main.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:orientation="vertical" > <Button android:id="@+id/up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="UP" /> <Button android:id="@+id/down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="DOWN" /> <ScrollView android:id="@+id/scroll" android:layout_width="match_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/content" android:layout_width="match_parent" android:layout_height="wrap_content" /> </ScrollView></LinearLayout>
package com.imooc.android_scrollview;import android.app.Activity;import android.app.ActionBar;import android.app.Fragment;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.Menu;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnTouchListener;import android.widget.Button;import android.widget.ScrollView;import android.widget.TextView;import android.os.Build;public class MainActivity extends Activity implements OnClickListener {private TextView tv;private ScrollView scroll;private Button up_btn;private Button down_btn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);tv = (TextView) findViewById(R.id.content);tv.setText(getResources().getString(R.string.content));up_btn = (Button) findViewById(R.id.up);down_btn = (Button) findViewById(R.id.down);up_btn.setOnClickListener(this);down_btn.setOnClickListener(this);scroll = (ScrollView) findViewById(R.id.scroll);scroll.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_MOVE: {/** * (1)getScrollY()————获取滚动条滑动的距离 * (2)getMeasuredHeight()包含隐藏的textview的高度 * (3)getHeight()显示在一屏幕的高度 */// 顶部状态if (scroll.getScrollY() <= 0) {Log.i("Main", "滑动到顶部");}// 底部状态// TextView的总高度<=一屏幕的高度+滚动条的滚动距离if (scroll.getChildAt(0).getMeasuredHeight() <= scroll.getHeight() + scroll.getScrollY()) {tv.append(getResources().getString(R.string.content));}break;}}return false;}});}@Overridepublic void onClick(View v) {switch (v.getId()) {// scrollTo:以滚动视图起始位置开始计算的(参考点游标)// scrollBy:相对前一次的位置,去滚动对应的距离(参考点游标)case R.id.up: {//定位到textview开始处scroll.scrollTo(0, 0);break;}case R.id.down: {scroll.scrollBy(0, 100);break;}}}}
*********************************************上拉分批加载方式2****************************************************
主页面布局
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.demo.MainActivity" > <ListView android:id="@+id/listview" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>listview的item布局
<?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:orientation="vertical" > <TextView android:gravity="center" android:id="@+id/tv_info" android:layout_width="match_parent" android:layout_height="30dp" android:text="TextView" /></LinearLayout>
底部加载提示图标
footer.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:orientation="vertical" > <ProgressBar android:id="@+id/progressBar1" android:layout_width="fill_parent" android:layout_height="30dp" /></LinearLayout>
DataService
package com.example.demo;import java.util.ArrayList;import java.util.List;public class DataService {//加载数据public List<String> getData(){List<String> data = new ArrayList<String>();for(int i = 0;i< 20;i++){data.add("分页加载的数据 " + i);}return data;}}
MainActivity
package com.example.demo;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.os.SystemClock;import android.util.Log;import android.view.View;import android.widget.AbsListView;import android.widget.AbsListView.OnScrollListener;import android.widget.ArrayAdapter;import android.widget.ListView;public class MainActivity extends Activity {protected static final int SUCCESS_GET_DATA = 0;private ListView listview;private DataService service;private List<String> data;// 加载的总数据private ArrayAdapter<String> adapter;private boolean finish = true;// 是否加载完成private View footer;private Handler mHandler = new Handler() {public void handleMessage(android.os.Message msg) {switch (msg.what) {case SUCCESS_GET_DATA:@SuppressWarnings("unchecked")ArrayList<String> result = ((ArrayList<String>) msg.obj);data.addAll(result);// 让listview自动刷新adapter.notifyDataSetChanged();finish = true;// 将页脚删除掉if (listview.getFooterViewsCount() > 0) {listview.removeFooterView(footer);}break;default:break;}};};@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//ListView控件listview = (ListView) findViewById(R.id.listview);service = new DataService();data = new ArrayList<String>();List<String> result = service.getData();//获取数据data.addAll(result);//适配器adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.tv_info,data);footer = View.inflate(this, R.layout.footer, null);// 在增加listview的页脚之前,需要提前设置一次listview.addFooterView(footer);//设置适配器listview.setAdapter(adapter);// 然后再次将页脚删除掉(可以解决第一次上拉的急闪情况)listview.removeFooterView(footer);// 滚动监听事件listview.setOnScrollListener(new MyOnScrollListener());}private final class MyOnScrollListener implements OnScrollListener {// 滚动状态发生改变时public void onScrollStateChanged(AbsListView view, int scrollState) {Log.i("i", " scrollState " + scrollState);}/** * 界面展现 */public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {// 最下面的条目数final int totalCount = firstVisibleItem + visibleItemCount;// 当翻到最后一条数据时if (totalCount == totalItemCount && finish) {// 已经移动到了listview的最后finish = false;// 添加页脚listview.addFooterView(footer);new Thread() {public void run() {SystemClock.sleep(3000);List<String> result = service.getData();Message msg = new Message();msg.what = SUCCESS_GET_DATA;msg.obj = result;mHandler.sendMessage(msg);};}.start();}}}}
0 0
- PullToRefreshListView进阶(四)----->上拉分批加载2种方式
- PullToRefreshListView进阶(二)----->上拉加载
- PullToRefreshListView进阶(三)----->上拉加载、下拉刷新!
- PullToRefreshListView进阶(五)----->上下刷新、上拉加载
- PullreFreshLibrary(PullToRefreshListView)上拉刷新,下拉加载(—)
- 下拉刷新和上拉加载(pulltorefreshlistview)
- PullToRefreshListView下拉刷新与上拉加载
- PullToRefreshListView 下拉刷新上拉加载
- PullToRefreshListView上拉加载更多,下拉刷新
- PullToRefreshListView上拉加载下拉刷新
- PullToRefreshListView上拉刷新,下拉加载
- PullToRefreshListView多条目加载上拉刷新下拉加载
- PullToRefreshListView上下拉加载
- ListView上拉刷新,数据的分批加载小demo
- RecycleView上拉加载更多、实现分批分页功能
- RecyclerView实现分批分页、上拉加载更多功能
- PullToRefreshListView上拉下拉
- PullToRefreshListView下拉刷新和上拉加载更多使用方法
- 互斥的实例和使用场景
- oracle对象
- python pickle函数应用
- 深入探讨 Java 类加载器
- 树状数组模拟3个元素的排序 Codeforces 12D Ball
- PullToRefreshListView进阶(四)----->上拉分批加载2种方式
- javascript 解析JSONArray和JSONObject
- JavaScript 学习笔记 (一)
- 堆排序的完整过程
- Android应用开发之所有动画使用详解
- TTS零基础入门之拒绝界面卡顿--加入线程
- 云 虚主机探索之经验总结
- Java对象、Json、Xml转换工具Jackson使用
- 1.大数据基础(上)_2.大数据的商业应用