我正在尝试在我的应用程序中实现ActionBar-PullToRefresh.活动中有一个片段,片段中有一个列表视图. listview的实现是使用自定义适配器.
我尝试使用github上的QuickStart-ABS指南实现它,但拉不起作用.我有一种感觉,我没有正确初始化PullToRefresh.请看下面我的代码……
fragment_news_list.xml
<?xml version="1.0" encoding="utf-8"?>
<uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ptr_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listview_news_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout>
NewsListFragment.java
import java.util.ArrayList;
import java.util.List;
import uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout;
import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh;
import uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragment;
import com.android.volley.RequestQueue;
import com.android.volley.toolBox.ImageLoader;
import com.android.volley.toolBox.JsonArrayRequest;
import com.android.volley.toolBox.NetworkImageView;
import com.android.volley.toolBox.Volley;
public class NewsListFragment extends SherlockFragment implements
OnRefreshListener {
ProgressDialog pd;
ImageLoader imageLoader;
JsonArrayRequest jsArrayRequest;
Database db;
ListView listview;
List<NewsItem> newsItems = new ArrayList<NewsItem>();
NewsAdapter adapter;
NewsDbAdapter mNewsDbAdapter;
private PullToRefreshLayout mpullToRefreshLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mNewsDbAdapter = DatabaseHelper.get(
getActivity().getApplicationContext()).getNewsDbAdapter();
// unrelated code removed
View view = inflater.inflate(R.layout.fragment_news_list, container,
false);
pd = new ProgressDialog(getActivity());
pd.setMessage("Loading...");
pd.show();
pd.setCancelable(true);
newsItems = mNewsDbAdapter.getNewsTitles();
adapter = new NewsAdapter(getActivity(), newsItems);
listview = (ListView) view.findViewById(R.id.listview_news_list);
listview.setAdapter(adapter);
pd.dismiss();
return view;
}
public class NewsAdapter extends ArrayAdapter<NewsItem> {
// Adapter code goes in here... removed as not necessary
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ViewGroup viewGroup = (ViewGroup) view;
// As we're using a ListFragment we create a PullToRefreshLayout manually
mpullToRefreshLayout = new PullToRefreshLayout(viewGroup.getContext());
// We can Now setup the PullToRefreshLayout
ActionBarPullToRefresh.from(getActivity())
// We need to insert the PullToRefreshLayout into the Fragment's ViewGroup
.insertLayoutInto(viewGroup)
// Here we mark just the ListView and it's Empty View as pullable
.theseChildrenArePullable(android.R.id.list, android.R.id.empty)
.listener(this)
.setup(mpullToRefreshLayout);
}
@Override
public void onRefreshStarted(View view) {
// Hide the list
// setListShown(false);
listview.setVisibility(View.INVISIBLE);
/**
* Simulate Refresh with 4 seconds sleep
*/
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printstacktrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Notify PullToRefreshLayout that the refresh has finished
mpullToRefreshLayout.setRefreshComplete();
if (getView() != null) {
// Show the list again
//setListShown(true);
listview.setVisibility(View.VISIBLE);
}
}
}.execute();
}
}
我哪里错了?如果某个人有一个带有listview的片段的例子,请在这里分享链接..
谢谢!!!
[编辑]
改变了这个
theseChildrenArePullable(android.R.id.list, android.R.id.empty)
至
theseChildrenArePullable(R.id.listview_news_list, android.R.id.empty)
它起作用了……
解决方法:
我相信问题是你没有在Fragment布局中使用ListView,而是从框架中使用默认的ListView,试试这个:
// setup the PullToRefreshLayout
ActionBarPullToRefresh.from(getActivity())
// We need to insert the PullToRefreshLayout into the Fragment's ViewGroup
.insertLayoutInto(viewGroup)
// Here we mark just the ListView and it's Empty View as pullable
.theseChildrenArePullable(R.id. listview_news_list, android.R.id.empty)
.listener(this)
.setup(mpullToRefreshLayout);
如果它不起作用,您还可以尝试在onCreateView方法中执行以下操作:
mpullToRefreshLayout = ((PullToRefreshLayout) view.findViewById(R.id.ptr_layout));
然后在onActivityCreated或onViewCreated中像这样设置它:
ActionBarPullToRefresh
.from(getActivity())
.allChildrenArePullable()
.listener(this)
.setup(this.mpullToRefreshLayout);
为了记录,我在我的应用程序中的片段中使用PullToRefreshLayout并且它正常工作.如果有效,请告诉我.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。