【问题标题】:Fragments in Android version<3.0Android版本<3.0中的片段
【发布时间】:2013-05-28 05:35:58
【问题描述】:

我想在已经扩展 ListActivity 的活动中创建片段。 这个片段活动甚至应该适用于 android 版本

从下面的链接中,我得到了在较低的 android 版本中实现片段的解决方案。他们告诉将主要活动扩展到 FragmentActivity。 Fragments in Android 2.2.1, 2.3, 2.0. Is this possible?

现在我的问题是如何将我的主 Activity 扩展到 FragmentActivity,因为它已经在扩展 ListActivity。

请帮帮我。谢谢。

【问题讨论】:

    标签: android android-layout android-listview android-fragments android-fragmentactivity


    【解决方案1】:

    不幸的是,android 支持库不包含 FragmentListActivity。您必须通过让自定义 Listactivity 扩展 FragmentActivity 来创建自己的。

    我在 GitHub 上找到了这个课程。也许你试一试:)

    *
     * Copyright (C) 2006 The Android Open Source Project
     * 
     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
     * the License. You may obtain a copy of the License at
     * 
     * http://www.apache.org/licenses/LICENSE-2.0
     * 
     * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
     * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
     * specific language governing permissions and limitations under the License.
     */
    
    import android.app.Activity;
    import android.app.ListActivity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    
    /**
     * <em>Copy from Android source to enable {@link Fragment} support.</em>
     * 
     * @see ListActivity
     */
    public abstract class FragmentListActivity extends FragmentActivity {
    
        // changed to private as original suggested
        private ListAdapter mAdapter;
        // changed to private as original suggested
        private ListView mList;
    
        private Handler mHandler = new Handler();
        private boolean mFinishedStart = false;
    
        private Runnable mRequestFocus = new Runnable() {
        public void run() {
            mList.focusableViewAvailable(mList);
        }
        };
    
        /**
         * This method will be called when an item in the list is selected. Subclasses should override. Subclasses can call
         * getListView().getItemAtPosition(position) if they need to access the data associated with the selected item.
         * 
         * @param l
         *            The ListView where the click happened
         * @param v
         *            The view that was clicked within the ListView
         * @param position
         *            The position of the view in the list
         * @param id
         *            The row id of the item that was clicked
         */
        protected void onListItemClick(ListView l, View v, int position, long id) {
        }
    
        /**
         * Ensures the list view has been created before Activity restores all of the view states.
         * 
         * @see Activity#onRestoreInstanceState(Bundle)
         */
        @Override
        protected void onRestoreInstanceState(Bundle state) {
        ensureList();
        super.onRestoreInstanceState(state);
        }
    
        /**
         * Updates the screen state (current list and other views) when the content changes.
         * 
         * @see Activity#onContentChanged()
         */
        @Override
        public void onContentChanged() {
        super.onContentChanged();
    
        // changed references from com.android.internal.R to android.R.* 
        View emptyView = findViewById(android.R.id.empty);
        mList = (ListView) findViewById(android.R.id.list);
    
        if (mList == null) {
            throw new RuntimeException(
                "Your content must have a ListView whose id attribute is " +
                    "'android.R.id.list'");
        }
        if (emptyView != null) {
            mList.setEmptyView(emptyView);
        }
        mList.setOnItemClickListener(mOnClickListener);
        if (mFinishedStart) {
            setListAdapter(mAdapter);
        }
        mHandler.post(mRequestFocus);
        mFinishedStart = true;
        }
    
        /**
         * Provide the cursor for the list view.
         */
        public void setListAdapter(ListAdapter adapter) {
        synchronized (this) {
            ensureList();
            mAdapter = adapter;
            mList.setAdapter(adapter);
        }
        }
    
        /**
         * Set the currently selected list item to the specified position with the adapter's data
         * 
         * @param position
         */
        public void setSelection(int position) {
        mList.setSelection(position);
        }
    
        /**
         * Get the position of the currently selected list item.
         */
        public int getSelectedItemPosition() {
        return mList.getSelectedItemPosition();
        }
    
        /**
         * Get the cursor row ID of the currently selected list item.
         */
        public long getSelectedItemId() {
        return mList.getSelectedItemId();
        }
    
        /**
         * Get the activity's list view widget.
         */
        public ListView getListView() {
        ensureList();
        return mList;
        }
    
        /**
         * Get the ListAdapter associated with this activity's ListView.
         */
        public ListAdapter getListAdapter() {
        return mAdapter;
        }
    
        private void ensureList() {
        if (mList != null) {
            return;
        }
        // use legacy hack to get layout ID instead of com.android.internal.R.layout.list_*
        setContentView(getSupportLayoutResourceId());
        }
    
        /**
         * Support-hack to make legacy methods work.
         * 
         * @return the layout ID used for this {@link Activity}. This must be the same you are using with
         *         {@link #setContentView(int)}.
         */
        abstract protected int getSupportLayoutResourceId();
    
        private AdapterView.OnItemClickListener mOnClickListener = new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id)
        {
            onListItemClick((ListView) parent, v, position, id);
        }
        };
    }
    

    来源:https://gist.github.com/panzerfahrer/4201753

    【讨论】:

      【解决方案2】:

      使用 android 支持库,允许您支持 Fragment 一直到 2.x 版本的 android。

      1. Android Support Library on developer.android.com
      2. Setting Up and Using the Support Library
      3. Creating a Fragment

      【讨论】:

        【解决方案3】:

        这是转换它的逻辑。

        1. 按照链接Fragments in Android 2.2.1, 2.3, 2.0. Is this possible? 中的说明导入库
        2. 将您的 List Activity 分为 FragmentActivity 和 ListFragment
        3. 将 ListFragment 加载到 FragmentActivity

        这并不容易,因为您可能需要更改整个代码,但您可以随时通过 Google 搜索。

        祝你好运^^

        【讨论】:

          【解决方案4】:

          我如何将我的主 Activity 扩展到 FragmentActivity,因为它已经在扩展 ListActivity。

          • 创建一个扩展 FragmentActivity 本身的 Activity,然后在该 Activity 中扩展 ListFragment 的 Fragment 并使用 ListActivity 中存在的相同代码来实现目标。

          【讨论】:

            【解决方案5】:
            猜你喜欢
            • 2011-11-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-03-24
            相关资源
            最近更新 更多