gpt4 book ai didi

android - ActionBar-PullToRefresh 与 ListView 和 Fragment

转载 作者:太空宇宙 更新时间:2023-11-03 10:21:11 24 4
gpt4 key购买 nike

我正在尝试在我的应用程序中实现 ActionBar-PullToRefresh。该 Activity 中有一个 fragment , fragment 中有一个 ListView 。 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>

新闻列表 fragment .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 fragment 的示例,请在此处分享链接。

谢谢!!!

[编辑]

改变了这个

theseChildrenArePullable(android.R.id.list, android.R.id.empty)

theseChildrenArePullable(R.id.listview_news_list, android.R.id.empty)

成功了...

最佳答案

我认为问题在于您没有在 Fragment 布局中使用 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);

郑重声明,我在我的应用程序的 Fragment 中使用 PullToRefreshLayout,它工作正常。让我知道它是否有效。

关于android - ActionBar-PullToRefresh 与 ListView 和 Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21281301/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com