我正在使用Johan Nilssons PullToRefresh library,但不会执行刷新列表的代码。
我的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Set a listener to be invoked when the list should be refreshed.
listView = new PullToRefreshListView(getActivity());
listView.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
//check if internet connection is available
ConnectivityManager conMgr = (ConnectivityManager) getActivity().
getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() == null) {
Log.d("debug","Keine Internetverbindung, kein Refresh möglich");
Toast toast = Toast.makeText(getActivity(),
"Keine Internetverbindung! Refresh nicht möglich!",
Toast.LENGTH_LONG);
toast.show();
}else{
//refresh list
Log.d("debug","Internetverbindung verfügbar, refreshe Liste");
DOMParser tmpDOMParser = new DOMParser();
feed = tmpDOMParser.parseXml("http://www.test.de/feed");
Log.d("debug", "Refresh Liste mit Feed: "+feed);
adapter.setNewFeed(feed);
WriteFeed(feed);
adapter.notifyDataSetChanged();
// Call onRefreshComplete when the list has been refreshed.
listView.onRefreshComplete();
}
}
});
//inflate the fragment with the custom detail fragment
View view = inflater.inflate(R.layout.feed_list, null);
return view;
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//new PullToRefresh ListView
listView = new PullToRefreshListView(getActivity());
listView.setVerticalFadingEdgeEnabled(true);
// Set custom list adapter to the ListView
adapter = new CustomListAdapter(getActivity(), feed);
listView.setAdapter(adapter);
}
我的feed_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" >
<mypackage.com.PullToRefreshListView
android:id="@+id/android:list"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
</LinearLayout>
在LogCat中,最后一步是: 标签:PullToRefreshListView文字:onRefresh
在我的列表视图中,显示“正在加载”文字和旋转符号。为什么onRefresh()
中的代码没有执行?
SOLUTION:
在我的ListFragment中,我将代码更改为:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//inflate the fragment with the custom detail fragment
View view = inflater.inflate(R.layout.feed_list, null);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Set a listener to be invoked when the list should be refreshed.
listView = (PullToRefreshListView) getListView();
listView.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
//do refresh stuff here
});
listView.setVerticalFadingEdgeEnabled(true);
// Set custom list adapter to the ListView
adapter = new CustomListAdapter(getActivity(), feed);
listView.setAdapter(adapter);
}
我的feed_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" >
<mypackage.com.PullToRefreshListView
android:id="@id/android:list"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</LinearLayout>
答案 0 :(得分:1)
我认为您正在创建一个新的PullToRefreshListView
,而不是在xml文件中使用PullToRefreshListView
。
试试此代码
</LinearLayout>
<?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" >
<mypackage.com.PullToRefreshListView
android:id="@+id/list"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
</LinearLayout>
和
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//inflate the fragment with the custom detail fragment
View view = inflater.inflate(R.layout.feed_list, null);
// Set a listener to be invoked when the list should be refreshed.
listView = (PullToRefreshListView) view.findViewById(R.id.list);
listView.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
//check if internet connection is available
ConnectivityManager conMgr = (ConnectivityManager) getActivity().
getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() == null) {
Log.d("debug","Keine Internetverbindung, kein Refresh möglich");
Toast toast = Toast.makeText(getActivity(),
"Keine Internetverbindung! Refresh nicht möglich!",
Toast.LENGTH_LONG);
toast.show();
}else{
//refresh list
Log.d("debug","Internetverbindung verfügbar, refreshe Liste");
DOMParser tmpDOMParser = new DOMParser();
feed = tmpDOMParser.parseXml("http://www.test.de/feed");
Log.d("debug", "Refresh Liste mit Feed: "+feed);
adapter.setNewFeed(feed);
WriteFeed(feed);
adapter.notifyDataSetChanged();
// Call onRefreshComplete when the list has been refreshed.
listView.onRefreshComplete();
}
}
});
return view;
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listView.setVerticalFadingEdgeEnabled(true);
// Set custom list adapter to the ListView
adapter = new CustomListAdapter(getActivity(), feed);
listView.setAdapter(adapter);
}