当前位置:   article > 正文

上拉加载下拉刷新

上拉加载下拉刷新

需要关联第三方包XListView

导入三个包

**

package com.example.xlistviewtest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.com.cctest.view.XListView;
import org.com.cctest.view.XListView.IXListViewListener;
import android.app.Activity;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.ArrayAdapter;
public class MainActivity extends Activity {
private List<String> list;
int j;
private ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setData();
final XListView xListView = (XListView) findViewById(R.id.xListView);
属性:
// 激活加载更多
xListView.setPullLoadEnable(true);
// 激活下拉刷新
xListView.setPullRefreshEnable(true);
// 设置xlistView上拉加载下拉刷新的监听
xListView.setXListViewListener(new IXListViewListener() {
@Override
// 下拉刷新的回调方法
public void onRefresh() {
// 下拉刷新,应该展示最新数据,所以清空集合
list.clear();
arrayAdapter.notifyDataSetChanged();
//设置刷新时间
xListView.setRefreshTime(dateFormat());
//停止加载更多
xListView.stopLoadMore();
//停止刷新
xListView.stopRefresh();
}
@Override
// //上拉加载的回调方法
public void onLoadMore() {
arrayAdapter.notifyDataSetChanged();
//停止加载更多
xListView.stopLoadMore();
//停止刷新
xListView.stopRefresh();
}
});
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
xListView.setAdapter(arrayAdapter);
}



/**
* 格式化毫秒值
*
* @return
*/
private String dateFormat() {
long currentTimeMillis = System.currentTimeMillis();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format = dateFormat.format(currentTimeMillis);
return format;
}
  • 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
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

**

**

在strings.xml文件中
<string name="xlistview_header_hint_normal">下拉刷新</string>
    <string name="xlistview_header_hint_ready">松开刷新数据</string>
    <string name="xlistview_header_hint_loading">正在加载...</string>
    <string name="xlistview_header_last_time">上次更新时间:</string>
    <string name="xlistview_footer_hint_normal">查看更多</string>
    <string name="xlistview_footer_hint_ready">松开载入更多</string>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

**

在activit_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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.xlistview.MainActivity" >
    <com.example.xlistview.XListView
        android:id="@+id/xListView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </com.example.xlistview.XListView>
</RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

**

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/1009030
推荐阅读
相关标签
  

闽ICP备14008679号