work_weipa_下拉刷新+上拉加载

问题:如何实现listview和gridview的下拉刷新与上拉加载?

回答:使用XListView或者PullToFreshView中的类,再进行调用即可

例子:


①listview----XListView

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>

xlistview_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="bottom" >

    <RelativeLayout
        android:id="@+id/xlistview_header_content"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:background="#ffffff" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:orientation="vertical" android:id="@+id/xlistview_header_text">

            <TextView
                android:id="@+id/xlistview_header_hint_textview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/xlistview_header_hint_normal"
                android:textColor="#000000" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="3dp" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/xlistview_header_last_time"
                    android:textSize="12sp"
                    android:textColor="#000000" />

                <TextView
                    android:id="@+id/xlistview_header_time"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="12sp"
                    android:textColor="#000000" />
            </LinearLayout>
        </LinearLayout>

        <ImageView
            android:id="@+id/xlistview_header_arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/xlistview_header_text"
            android:layout_centerVertical="true"
            android:layout_marginLeft="-35dp"
            android:src="@drawable/xlistview_arrow" />

        <ProgressBar
            android:id="@+id/xlistview_header_progressbar"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignLeft="@id/xlistview_header_text"
            android:layout_centerVertical="true"
            android:layout_marginLeft="-40dp"
            android:visibility="invisible" />
    </RelativeLayout>

</LinearLayout>

xlistview_footer.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:id="@+id/xlistview_footer_content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:background="#ffffff" >

        <ProgressBar
            android:id="@+id/xlistview_footer_progressbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:visibility="invisible" />

        <TextView
            android:id="@+id/xlistview_footer_hint_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textColor="#000000"
            android:text="@string/xlistview_footer_hint_normal" />
    </RelativeLayout>

</LinearLayout>


fragment_home.xml

<com.example.aaa.galview.XListView
            android:id="@+id/xListView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:cacheColorHint="#00000000" >
</com.example.weipa.galview.XListView>

HomeFragment.java

public class HomeFragment extends Fragment {

	// 声明管理对象
	public static AllThemesInfoManager allThemesInfoManager;
	// 创建出分页对象
	public static PageInfo pageInfo = new PageInfo();
	// 声明listView控件对象
	private XListView mListView;
	// 声明适配器对象
	private AllThemesAdapter mAdapter;
	private Handler mHandler;
	private String date;

	private int showItem = 0;

	private List<Themes> entities = new ArrayList<Themes>();

	public List<Themes> getEntities() {
		return entities;
	}

	private void onLoad() {
		mListView.stopRefresh();
		mListView.stopLoadMore();
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日  HH:mm");
		date = format.format(new Date());
		mListView.setRefreshTime(date);
	}

	Handler handler = new Handler() {

		public void handleMessage(Message msg) {
			switch (msg.what) {
			case WhatUtil.QUERYCATEGORIES:
				pageInfo = allThemesInfoManager.getPageInfo();

				entities = allThemesInfoManager.getEntities();

				mListView.setPullLoadEnable(true);
				mAdapter = new AllThemesAdapter(getActivity(), entities, getActivity());
				mListView.setAdapter(mAdapter);
				mListView.setSelection(showItem);

				// 设置item点击事件
				mListView.setOnItemClickListener(new myOnItemClickListener());

				mListView.setXListViewListener(new MyIXListViewListener());
				mHandler = new Handler();

				break;
			}
		}
	};

	class MyIXListViewListener implements IXListViewListener {

		@Override
		public void onRefresh() {
			// TODO Auto-generated method stub
			mHandler.postDelayed(new Runnable() {
				@Override
				public void run() {
					entities.clear();
					allThemesInfoManager.queryObjects(1);
					showItem = 0;
					mAdapter.notifyDataSetChanged();
					onLoad();
				}
			}, 1000);
		}

		@Override
		public void onLoadMore() {
			// TODO Auto-generated method stub
			mHandler.postDelayed(new Runnable() {
				@Override
				public void run() {
					if (pageInfo.getNowPage() < pageInfo.getCountPage()) {
						allThemesInfoManager.queryObjects(pageInfo.getNowPage() + 1);
						showItem = entities.size();
					} else if (pageInfo.getNowPage() == pageInfo.getCountPage()) {
						Toast.makeText(getActivity(), "已经是最后一页了*-*", 1).show();
					}
					mAdapter.notifyDataSetChanged();
					onLoad();
				}
			}, 1000);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = inflater.inflate(R.layout.fragment_home, container, false);

		mListView = (XListView) v.findViewById(R.id.xListView);
		// 创建管理对象
		allThemesInfoManager = new AllThemesInfoManager(getActivity(),handler);

		allThemesInfoManager.queryObjects(1);

		return v;
	}
}

②gridview-----PullToFreshView

strings.xml

<string name="pull_to_refresh_pull_label">下拉刷新</string>
<string name="pull_to_refresh_release_label">松开后刷新</string>
<string name="pull_to_refresh_refreshing_label">正在刷新\u2026</string>
    
<string name="pull_to_refresh_footer_release_label">松开后加载</string>
<string name="pull_to_refresh_footer_pull_label">上拉加载更多</string>
<string name="pull_to_refresh_footer_refreshing_label">正在加载中\u2026</string>
<string name="text">http://miloisbadboy.com/</string>
refresh_header.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pull_to_refresh_header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:paddingBottom="15dp"
    android:paddingTop="10dp" >

    <ProgressBar
        android:id="@+id/pull_to_refresh_progress"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:indeterminate="true"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/pull_to_refresh_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="20dp"
        android:gravity="center"
        android:src="@drawable/ic_pulltorefresh_arrow"
        android:visibility="visible" />

    <TextView
        android:id="@+id/pull_to_refresh_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:textColor="#000000"
        android:text="@string/pull_to_refresh_pull_label"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/pull_to_refresh_updated_at"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:layout_below="@+id/pull_to_refresh_text"
        android:layout_gravity="center"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:visibility="gone" />

</RelativeLayout>

refresh_footer.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pull_to_refresh_header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:paddingBottom="10dp"
    android:paddingTop="10dp" >

    <ProgressBar
        android:id="@+id/pull_to_load_progress"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:indeterminate="true"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/pull_to_load_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="20dp"
        android:gravity="center"
        android:src="@drawable/ic_pulltorefresh_arrow_up"
        android:visibility="visible" />

    <TextView
        android:id="@+id/pull_to_load_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:textColor="#000000"
        android:text="@string/pull_to_refresh_footer_pull_label"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold" />

</RelativeLayout>

fragment_viewer.xml

<com.example.aaa.galview.PullToRefreshView
            android:id="@+id/pull_refresh_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <!-- 这里放置listview,gridview或者scrollview的布局 ,PullToRefreshView
				要设置android:orientation="vertical"属性     否则,显示不正确         -->

            <GridView
                android:id="@+id/gridview"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:numColumns="3"
                android:verticalSpacing="10dp" />
        </com.example.aaa.galview.PullToRefreshView>

ViewerFragment.java

public class ViewerFragment extends Fragment {
	
	private PullToRefreshView mPullToRefreshView;
	private GridView mGridView;
	private List<Themes> entities;

	// 声明适配器对象
	private TestAdapter adapter;
	// 声明管理对象
	private TestInfoManager testInfoManager;
	// 创建出分页对象
	private PageInfo pageInfo = new PageInfo();

	private int showItem = 0;

	private View view;
	
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case WhatUtil.QUERYCATEGORIES:
				// 创建适配器对象
				pageInfo = testInfoManager.getPageInfo();
				entities = testInfoManager.getEntities();
				adapter = new TestAdapter(getActivity(),
						entities, getActivity());
				// 设置GridView控件的适配器
				mGridView.setAdapter(adapter);
				mGridView.setSelection(showItem);
				mPullToRefreshView.setOnHeaderRefreshListener(new OnHeaderRefreshListener() {
					
					@Override
					public void onHeaderRefresh(PullToRefreshView view) {
						// TODO Auto-generated method stub
						mPullToRefreshView.postDelayed(new Runnable() {

							@Override
							public void run() {
								entities.clear();
								testInfoManager.queryObjects(1);
								showItem = 0;
								adapter.notifyDataSetChanged();
								// 设置更新时间
								mPullToRefreshView.onHeaderRefreshComplete();
							}
						}, 1000);
					}
				});
				mPullToRefreshView.setOnFooterRefreshListener(new OnFooterRefreshListener() {
					
					@Override
					public void onFooterRefresh(PullToRefreshView view) {
						// TODO Auto-generated method stub
						mPullToRefreshView.postDelayed(new Runnable() {

							@Override
							public void run() {
								System.out.println("上拉加载");
								if (pageInfo.getNowPage() != pageInfo.getCountPage()) {
									testInfoManager.queryObjects(pageInfo.getNowPage() + 1);
									showItem = entities.size()-1;
								}else if(pageInfo.getNowPage() == pageInfo.getCountPage()){
									Toast.makeText(getActivity(), "已经是最后一页了*-*", 1).show();
								}
								adapter.notifyDataSetChanged();
								mPullToRefreshView.onFooterRefreshComplete();
							}
						}, 1000);
					}
				});

				break;
			}
		}
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_viewer, container, false);

		mPullToRefreshView = (PullToRefreshView) view.findViewById(R.id.pull_refresh_view);
		mGridView = (GridView) view.findViewById(R.id.gridview);
		
		// 创建管理对象
		testInfoManager = new TestInfoManager(getActivity(), handler);
		// 获取当前页的数据
		testInfoManager.queryObjects(1);
		
		return view;
	}

}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
public final void M() { List split$default; com.niming.weipa.c.a.a(this.activity, getUserInfo2().getAvatar(), (ImageView) _$_findCachedViewById(R.id.iv_user_avatar)); TextView tv_user_name = (TextView) _$_findCachedViewById(R.id.tv_user_name); Intrinsics.checkExpressionValueIsNotNull(tv_user_name, "tv_user_name"); tv_user_name.setText(getUserInfo2().getNick()); if (getUserInfo2().getRank_type() == 0) { ImageView ivVipLevel = (ImageView) _$_findCachedViewById(R.id.ivVipLevel); Intrinsics.checkExpressionValueIsNotNull(ivVipLevel, "ivVipLevel"); ivVipLevel.setVisibility(8); } else { ((ImageView) _$_findCachedViewById(R.id.ivVipLevel)).setImageResource(com.niming.weipa.utils.j.a(getUserInfo2().getRank_type())); } if (Intrinsics.areEqual(getUserInfo2().getIs_vip(), "y")) { String vip_expired = getUserInfo2().getVip_expired(); Intrinsics.checkExpressionValueIsNotNull(vip_expired, "userInfo2.vip_expired"); split$default = StringsKt__StringsKt.split$default((CharSequence) vip_expired, new String[]{ConstantUtils.PLACEHOLDER_STR_ONE}, false, 0, 6, (Object) null); TextView tv_user_sub_title = (TextView) _$_findCachedViewById(R.id.tv_user_sub_title); Intrinsics.checkExpressionValueIsNotNull(tv_user_sub_title, "tv_user_sub_title"); tv_user_sub_title.setText("已开通" + getUserInfo2().getRank_type_str() + ' ' + ((String) split$default.get(0)) + "到期"); return; } TextView tv_user_sub_title2 = (TextView) _$_findCachedViewById(R.id.tv_user_sub_title); Intrinsics.checkExpressionValueIsNotNull(tv_user_sub_title2, "tv_user_sub_title"); tv_user_sub_title2.setText("您当前未开通抖阴VIP"); 上面这段代码干了什么,哪里是关键判断点。
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值