【问题标题】:Android: PullToRefresh in ListFragment does not refresh the listAndroid:ListFragment 中的 PullToRefresh 不刷新列表
【发布时间】:2014-01-10 16:14:25
【问题描述】:

我正在使用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()中的代码没有执行?

解决方案:

在我的 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>

【问题讨论】:

  • 你能把R.layout.feed_list的xml代码贴出来吗?
  • 您正在动态创建ListView,但没有将其添加到Layout 的任何位置。onCreateView 只有这么多代码?
  • 我在原帖中添加了请求信息

标签: android android-listview pull-to-refresh


【解决方案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);

    }

【讨论】:

  • 我是这样实现的,但在代码行listView = (PullToRefreshListView) getListView(); 中收到错误01-10 20:22:56.169: E/AndroidRuntime(9356): Caused by: java.lang.IllegalStateException: Content view not yet created'
  • 谢谢,使用您更新的代码,我收到此错误:01-11 06:26:43.260: E/AndroidRuntime(925): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' in the line super.onViewCreated(view, savedInstanceState);
  • 感谢您的帮助,我找到了解决方案并将其添加到我的原始帖子中!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-20
相关资源
最近更新 更多