动作条,PullToRefresh与ListView和片段 [英] ActionBar-PullToRefresh with ListView and Fragment

查看:163
本文介绍了动作条,PullToRefresh与ListView和片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现的ActionBar,PullToRefresh在我的应用程序。活动中有一个片段和片段中有一个列表视图。列表视图的实现与自定义适配器。

I am trying to implement ActionBar-PullToRefresh in my application. The activity has a fragment in it and the fragment has a listview in it. The implementation of listview is with custom adapter.

我试着在GitHub上的快速入门-ABS引导来实现它,但是拉不工作。我有我没有正确初始化的PullToRefresh的感觉。请看看我的code低于...

I tried to implement it with the QuickStart-ABS guide on github, but the pull doesn't work. I have a feeling that I am not initialising the PullToRefresh correctly. Please have a look at my code below...

fragment_news_list.xml

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

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片段为例,请在这里分享链接。

Where am I going wrong? If some one has an example with fragment with a listview, please share the link here..

谢谢!

改变了这种

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

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

和它的工作...

推荐答案

我认为问题是,你不使用你的片段布局的ListView,但是默认的从框架,试试这个:

I believe the problem is that you are not using the ListView in your Fragment layout, but a default one from the framework, try this :

// 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方法如下:

If it's not working, you could also try by doing the following in your onCreateView method :

mPullToRefreshLayout = ((PullToRefreshLayout) view.findViewById(R.id.ptr_layout));

,然后设置它像这样在onActivityCreated或onViewCreated:

and then setup it like this in onActivityCreated or onViewCreated :

ActionBarPullToRefresh
    .from(getActivity())
    .allChildrenArePullable()
    .listener(this)
    .setup(this.mPullToRefreshLayout);

有关记录,我使用 PullToRefreshLayout 我的应用程序一个片段里面,它的工作就好了。让我知道,如果它的工作原理。

For the record, I am using PullToRefreshLayout inside a Fragment in my app and it's working just fine. Let me know if it works.

这篇关于动作条,PullToRefresh与ListView和片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆