Android:中间片段不会显示

时间:2014-07-12 03:14:11

标签: android android-fragments android-listview android-listfragment

我有3个视图设置为在另一个下方显示。但问题是这个视图(中间的)在屏幕上根本没有显示出来。 我不确定如何将此列表发送到父视图。

这是我使用的教程:http://tausiq.wordpress.com/2012/12/12/android-custom-adapter-listview-with-listfragment-and-loadermanager-inside-fragmentactivity/

以下是代码:

public class MyActivity extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.my_activity_layout, container, false);
        DataListFragment list = new DataListFragment();
        return view;
    }
    public static class DataListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<List<Model>> {
        CustomListViewAdapter mAdapter;
        @Override 
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            setEmptyText("No Data Here");
            mAdapter = new CustomListViewAdapter(getActivity());
            setListAdapter(mAdapter);
            setListShown(false);
            // Prepare the loader.
            getLoaderManager().initLoader(0, null, this);
        }
        @Override
        public Loader<List<Model>> onCreateLoader(int arg0, Bundle arg1) {
            System.out.println("DataListFragment.onCreateLoader");
            return new DataListLoader(getActivity());
        }

        @Override
        public void onLoadFinished(Loader<List<Model>> arg0, List<Model> data) {
            mAdapter.setData(data);
            System.out.println("DataListFragment.onLoadFinished");
            // The list should now be shown.
            if (isResumed()) {
                setListShown(true);
            } else {
                setListShownNoAnimation(true);
            }
        }
        @Override
        public void onLoaderReset(Loader<List<Model>> arg0) {
            mAdapter.setData(null);
        }
    }

    public static class DataListLoader extends AsyncTaskLoader<List<Model>> {
        List<Model> mModels;
        public DataListLoader(Context context) {
            super(context);
        }
        @Override
        public List<Model> loadInBackground() {
            System.out.println("DataListLoader.loadInBackground");
            List<Model> entries = new ArrayList<Model>(5);
            entries.add(new Model("1", "2","3"));
            entries.add(new Model("1", "2","3"));
            entries.add(new Model("1", "2","3"));
            return entries;
        }

        /**
         * Called when there is new data to deliver to the client.
         */
        @Override public void deliverResult(List<Model> listOfData) {
            if (isReset()) {
                // An async query came in while the loader is stopped.  We
                // don't need the result.
                if (listOfData != null) {
                    onReleaseResources(listOfData);
                }
            }
            List<Model> oldApps = listOfData;
            mModels = listOfData;

            if (isStarted()) {
                // If the Loader is currently started, we can immediately
                // deliver its results.
                super.deliverResult(listOfData);
            }

            // At this point we can release the resources associated with
            // 'oldApps' if needed; now that the new result is delivered we
            // know that it is no longer in use.
            if (oldApps != null) {
                onReleaseResources(oldApps);
            }
        }

        /**
         * Handles a request to start the Loader.
         */
        @Override protected void onStartLoading() {
            if (mModels != null) {
                // If we currently have a result available, deliver it
                // immediately.
                deliverResult(mModels);
            }
            if (takeContentChanged() || mModels == null) {
                // If the data has changed since the last time it was loaded
                // or is not currently available, start a load.
                forceLoad();
            }
        }

        /**
         * Handles a request to stop the Loader.
         */
        @Override protected void onStopLoading() {
            // Attempt to cancel the current load task if possible.
            cancelLoad();
        }

        /**
         * Handles a request to cancel a load.
         */
        @Override public void onCanceled(List<Model> apps) {
            super.onCanceled(apps);

            // At this point we can release the resources associated with 'apps'
            // if needed.
            onReleaseResources(apps);
        }

        /**
         * Handles a request to completely reset the Loader.
         */
        @Override protected void onReset() {
            super.onReset();

            // Ensure the loader is stopped
            onStopLoading();

            // At this point we can release the resources associated with 'apps'
            // if needed.
            if (mModels != null) {
                onReleaseResources(mModels);
                mModels = null;
            }
        }

        /**
         * Helper function to take care of releasing resources associated
         * with an actively loaded data set.
         */
        protected void onReleaseResources(List<Model> apps) {}

    }
}

这是自定义适配器:

public class CustomListViewAdapter extends ArrayAdapter<Model>{
    private final LayoutInflater mInflater;
    public CustomListViewAdapter(Context context) {
        super(context, android.R.layout.simple_list_item_2);
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setData(List<Model> data) {
        clear();
        if (data != null) {
            for (Model appEntry : data) {
                add(appEntry);
            }
        }
    }

    /**
     * Populate new items in the list.
     */
    @Override public View getView(int position, View convertView, ViewGroup parent) {
        View view;

        if (convertView == null) {
            view = mInflater.inflate(R.layout.single_item, parent, false);
        } else {
            view = convertView;
        }

        Model item = getItem(position);
        ((TextView)view.findViewById(R.id.tV_1)).setText(item.getA());
        ((TextView)view.findViewById(R.id.tV_2)).setText(item.getB());
        ((TextView)view.findViewById(R.id.tV_3)).setText(item.getC());

        return view;
    }
}

这是我的主要活动:

public class MainActivity extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_layout);

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.song_info_fragment, new TopActivity());
        fragmentTransaction.add(R.id.playlist_activity_fragment, new MyActivity());
        fragmentTransaction.add(R.id.controls_fragment, new BottomActivity());
        fragmentTransaction.commit();

    }

}

当我运行代码时,我只看到顶部和底部的活动。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

好的,我终于能够让这个工作了。 我将DataListFragment类复制到同名的新类文件中,并删除了静态keywork。 此外,我将另一个名为dataListLoader的部分复制到另一个名为DataListLoader.java的文件中。 并将fragment.add()事务移动到mainActivity文件中。就这样做了!