android pulltorefresh应用到fragment

用法基本和上一篇差不多,建议先去看一下上一篇的内容http://blog.csdn.net/jz1095567375/article/details/25300693


activity_main.xml

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/frag_ptr_list"
        android:name="com.demo.pulltorefreshinfragment.ListFragmet"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
------------------------------------------------------------------------------------------------------------
activity_fragment_list.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="fill_parent"
    android:orientation="vertical" >

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/pull_refresh_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:divider="#19000000"
        android:dividerHeight="4dp"
        android:fadingEdge="none"
        android:fastScrollEnabled="false"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:smoothScrollbar="true" />

</LinearLayout>
---------------------------------------------------------------------------------------------------------
public class ListFragmet extends Fragment {
	private PullToRefreshListView pullToRefreshListView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_fragment_list, null);
		pullToRefreshListView = (PullToRefreshListView) view
				.findViewById(R.id.pull_refresh_list);
		return view;
	}

	public PullToRefreshListView getPullToRefreshListView() {
		return pullToRefreshListView;
	}
}
----------------------------------------------------------------------------------------------------------

public class MainActivity extends FragmentActivity implements OnRefreshListener<ListView> {

	private LinkedList<String> listItems;
	private ArrayAdapter<String> arrayAdapter;
	private ListFragmet mPullRefreshListFragment;
	private PullToRefreshListView mPullRefreshListView;

	private String[] mStrings = { "Abbaye de Belloc",
			"Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
			"Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu",
			"Airag", "Airedale", "Aisy Cendre", "Allgauer Emmentaler",
			"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
			"Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis",
			"Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
			"Allgauer Emmentaler" };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mPullRefreshListFragment = (ListFragmet) getSupportFragmentManager().findFragmentById(R.id.frag_ptr_list);

		// 从fragment里得到PullToRefreshListView对象
		mPullRefreshListView = mPullRefreshListFragment.getPullToRefreshListView();

		// 设置刷新监听器
		mPullRefreshListView.setOnRefreshListener(this);

		// 也可以用mPullRefreshListFragment.getListView()得到listView
		ListView actualListView = mPullRefreshListView.getRefreshableView();

		listItems = new LinkedList<String>();
		listItems.addAll(Arrays.asList(mStrings));
		arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listItems);

		// 也可以用mPullRefreshListView.setAdapter(arrayAdapter)
		actualListView.setAdapter(arrayAdapter);
	}

	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		// 获取系统时间,xx月xx日 xx:xx
		String label = DateUtils.formatDateTime(getApplicationContext(),
				System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME
						| DateUtils.FORMAT_SHOW_DATE
						| DateUtils.FORMAT_ABBREV_ALL);

		// 显示最后更新的时间
		refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

		// // 异步更新列表
		new GetDataTask().execute();
	}

	private class GetDataTask extends AsyncTask<Void, Void, String[]> {

		@Override
		protected String[] doInBackground(Void... params) {
			try {
				Thread.sleep(4000);
			} catch (InterruptedException e) {
			}
			return mStrings;
		}

		@Override
		protected void onPostExecute(String[] result) {
			listItems.addFirst("Added after refresh...");
			arrayAdapter.notifyDataSetChanged();

			// // 当列表刷新完成后回调onRefreshComplete
			mPullRefreshListView.onRefreshComplete();

			super.onPostExecute(result);
		}
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值