onLoadFinished后不显示列表项

时间:2013-03-10 13:15:24

标签: android android-arrayadapter android-loader

我坚持以下情况。

我和活动实现了2个选项卡和一个pageradapter,上面有2个片段,我正在使用ActionBarSherlock来实现兼容性。

有问题的片段从SherlockListFragment扩展并实现 LoaderManager.LoaderCallbacks
我创建了一个自定义ArrayListAdapter 和一个扩展AsyncLoader的自定义Loader。

问题是,当加载程序完成加载任务时,列表视图不会刷新,只有显示空白页

我遵循了这个solution,但它并没有解决我的问题。

这是我的片段布局xml文件。

 <LinearLayout android:id="@+id/progressContainer"
        android:orientation="vertical"
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        android:visibility="gone"
        android:gravity="center">

    <ProgressBar style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:paddingTop="4dip"
            android:singleLine="true" />

</LinearLayout>


<FrameLayout android:id="@+id/listContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >



<ListView
    android:id="@id/android:list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="@color/abs__background_holo_light"
    android:divider="@android:color/transparent"
    android:dividerHeight="@dimen/activity_horizontal_margin"
    android:drawSelectorOnTop="false"
    android:headerDividersEnabled="true"
    android:smoothScrollbar="true"
     />

    <TextView
            android:id="@android:id/empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FFFFFF"

            android:gravity="center"
            android:text="no hay rumores"
            />

这是自定义加载程序代码

    package *.android.core;

import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;

import *.Rumor;
import *.DownloadHelper;

import java.util.List;


public class RumorListLoader extends AsyncTaskLoader<List<Rumor>> {

    List<Rumor> mRumors;
    Context mContext;

    /**
     * Constructor.
     * 
     * @param context
     */
    public RumorListLoader(final Context context) {
        super(context);
        this.mContext = context;

    }

    /*
     * (non-Javadoc)
     * @see android.support.v4.content.Loader#deliverResult(java.lang.Object)
     */
    @Override
    public void deliverResult(final List<Rumor> data) {
        if (isReset()) {
            if (data != null) {
                onReleaseResources(data);
            }
        }

        final List<Rumor> oldList = data;
        this.mRumors = data;

        if (isStarted()) {
            super.deliverResult(data);
        }

        if (oldList != null) {
            onReleaseResources(oldList);
        }
    }

    /*
     * (non-Javadoc)
     * @see android.support.v4.content.Loader#isReset()
     */
    @Override
    public boolean isReset() {
        // TODO Auto-generated method stub
        return super.isReset();
    }

    @Override
    public List<Rumor> loadInBackground() {
        final List<Rumor> rumores = DownloadHelper.downloadPreviousRumors(this.mContext);

        return rumores;
    }

    /*
     * (non-Javadoc)
     * @see
     * android.support.v4.content.AsyncTaskLoader#onCanceled(java.lang.Object)
     */
    @Override
    public void onCanceled(final List<Rumor> data) {
        // TODO Auto-generated method stub
        super.onCanceled(data);

        onReleaseResources(data);
    }

    /**
     * Helper function that takes care of releasing resources.
     * 
     * @param apps
     */
    protected void onReleaseResources(final List<Rumor> apps) {
    }

    /*
     * (non-Javadoc)
     * @see android.support.v4.content.Loader#onReset()
     */
    @Override
    protected void onReset() {
        // TODO Auto-generated method stub
        super.onReset();

        onStopLoading();

        if (this.mRumors != null) {
            onReleaseResources(this.mRumors);
            this.mRumors = null;
        }
    }

    /*
     * (non-Javadoc)
     * @see android.support.v4.content.Loader#onStartLoading()
     */
    @Override
    protected void onStartLoading() {
        if (this.mRumors != null) {
            deliverResult(this.mRumors);
        }
        if (takeContentChanged() || this.mRumors == null) {
            forceLoad();
        }
    }

    /*
     * (non-Javadoc)
     * @see android.support.v4.content.Loader#onStopLoading()
     */
    @Override
    protected void onStopLoading() {
        // TODO Auto-generated method stub
        super.onStopLoading();
        cancelLoad();
    }

}

最后这是片段类

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockListFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;


import java.util.ArrayList;
import java.util.List;


public class RumorsFragment extends SherlockListFragment implements
        LoaderManager.LoaderCallbacks<List<Rumor>> {

    protected Context mContext;

    public static final String ARG_SECTION_NUMBER = "section_number";

    protected final static int RUMOR_LOADER_CODE = 100;
    private final static String TAG = "ru_rufragment";

    protected RumorAdapter mAdapter;

    protected View mProgressContainer;
    protected View mListContainer;
    boolean mListShown;
    public ListView mList;
    View mEmptyView;
    TextView mStandardEmptyView;
    CharSequence mEmptyText;

    private LoaderManager.LoaderCallbacks<List<Rumor>> mCallbacks;

    final private Handler mHandler = new Handler();

    final private Runnable mRequestFocus = new Runnable() {
        public void run() {
            mList.focusableViewAvailable(mList);
        }
    };

    /**
     * Constructor.
     */
    public RumorsFragment() {

    }

    /**
     * Setup the list data.
     */
    protected void init() {

    }

    @Override
    public void onActivityCreated(final Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        // Initially there is no data

        this.mContext = getSherlockActivity();

        // Create an empty adapter to display the data.
        this.mAdapter = new RumorAdapter(this.mContext, R.layout.rumor_item);
        setListAdapter(this.mAdapter);
        setEmptyText("Sin datos");
        setListShown(false);
        mCallbacks = this;

        getLoaderManager().initLoader(RUMOR_LOADER_CODE, null, mCallbacks);
    }

    @Override
    public void onAttach(final Activity activity) {
        super.onAttach(activity);
        this.mContext = getSherlockActivity();
    }

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // This fragment adds elements to the action bar
        setHasOptionsMenu(true);
    }

    @Override
    public Loader<List<Rumor>> onCreateLoader(final int arg0, final Bundle data) {

        return new RumorListLoader(this.mContext);
    }

    @Override
    public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
            final Bundle savedInstanceState) {

        final View fragmentView = inflater.inflate(R.layout.fragment_rumors, container, false);
        setRetainInstance(true);
        return fragmentView;
    }

    @Override
    public void onDetach() {
        // TODO Auto-generated method stub
        super.onDetach();
    }

    @Override
    public void onLoaderReset(final Loader<List<Rumor>> arg0) {
        this.mAdapter.clear();

    }

    @Override
    public void onLoadFinished(final Loader<List<Rumor>> loader, final List<Rumor> data) {

        Log.i(TAG, "Gets to onLoadFinished");
        switch (loader.getId()) {
        case RUMOR_LOADER_CODE:
            mAdapter.setData(data);
             // The list should now be shown;
            if (isResumed()) {
                setListShown(true);
            } else {
                setListShownNoAnimation(true);
            }
        }




    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        // TODO Auto-generated method stub
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onPrepareOptionsMenu(final Menu menu) {
        // TODO Auto-generated method stub
        super.onPrepareOptionsMenu(menu);
    }

    public void setListShown(Boolean shown, boolean animate) {
        ensureList();
        if (mListShown == shown) {
            return;
        }
        mListShown = shown;
        if (shown) {
            if (animate) {
                mProgressContainer.startAnimation(AnimationUtils.loadAnimation(getSherlockActivity(), android.R.anim.fade_out));
                mListContainer.startAnimation(AnimationUtils.loadAnimation(getSherlockActivity(), android.R.anim.fade_in));
            } else {
                mProgressContainer.clearAnimation();
                mListContainer.clearAnimation();
            }
            mProgressContainer.setVisibility(View.GONE);
            mListContainer.setVisibility(View.VISIBLE);
        } else {
            if (animate) {
                mProgressContainer.startAnimation(AnimationUtils.loadAnimation(getSherlockActivity(), android.R.anim.fade_in));
                mListContainer.startAnimation(AnimationUtils.loadAnimation(getSherlockActivity(), android.R.anim.fade_out));
            } else {
                mProgressContainer.clearAnimation();
                mListContainer.clearAnimation();
            }
            mProgressContainer.setVisibility(View.VISIBLE);
            mListContainer.setVisibility(View.GONE);
        }
    }

    public void setListShown(boolean shown) {
        setListShown(shown, true);
    }

    public void setListShownNoAnimation(boolean shown) {
        setListShown(shown, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {

        super.onViewCreated(view, savedInstanceState);
        ensureList();
    }

    @Override
    public void onDestroyView() {
        mHandler.removeCallbacks(mRequestFocus);
        mList = null;
        mListShown = false;
        mEmptyView = mProgressContainer = mListContainer = null;
        mStandardEmptyView = null;
        super.onDestroyView();
    }

    public ListView getListView() {
        ensureList();
        return mList;
    }

    public void setEmptyText(CharSequence text) {
        ensureList();
        if(mStandardEmptyView == null) {
            throw new IllegalStateException("Cant be used");
        }
        mStandardEmptyView.setText(text);
        if(mEmptyText == null) {
            mList.setEmptyView(mStandardEmptyView);
        }
        mEmptyText = text;
    }

    private void ensureList() {
        if (mList != null) {
            return;
        }

        View root = getView();
        if (root == null) {
            throw new IllegalStateException("View not created");
        }
        if(root instanceof ListView) {
            mList = (ListView) root;
        } else {
            mStandardEmptyView = (TextView) root.findViewById(android.R.id.empty);
            if (mStandardEmptyView == null) {
                mEmptyView = root.findViewById(android.R.id.empty);
            } else {
                mStandardEmptyView.setVisibility(View.GONE);
            }
            mProgressContainer = root.findViewById(R.id.progressContainer);
            mListContainer = root.findViewById(R.id.listContainer);
            View rawListView = root.findViewById(android.R.id.list);
            if(!(rawListView instanceof ListView)) {
                if (rawListView == null) {
                    throw new RuntimeException("Listview argument android.R.id.list");
                }
                throw new RuntimeException( "Content has view with id attribute 'android.R.id.list' "
                        + "that is not a ListView class");
            }
            mList = (ListView) rawListView;
            if (mEmptyView != null) {
                mList.setEmptyView(mEmptyView);
            } else if (mEmptyText != null) {
                mStandardEmptyView.setText(mEmptyText);
                mList.setEmptyView(mStandardEmptyView);
            }
        }
        mListShown = true;
        //mList.setOnClickListener(mOn)
        if (mAdapter != null) {
            ListAdapter adapter = mAdapter;
            mAdapter = null;
            setListAdapter(adapter);
        } else {
            if (mProgressContainer != null) {
                setListShown(false, false);
            }
        }
        mHandler.post(mRequestFocus);
    }



}

1 个答案:

答案 0 :(得分:0)

最后,我通过关注链接示例并删除代码中的“ensurelist”部分来解决问题。