AndroidGeek
AndroidGeek

Reputation: 166

Fragments in Android version<3.0

I want to create fragments in my activity which is already extending ListActivity. This fragment activity should even be suitable for android versions < 3.0

From the below link I got the solution for implementing fragment in lower android version.They told to extend the main activity to FragmentActivity. Fragments in Android 2.2.1, 2.3, 2.0. Is this possible?

Now my problem is how can I extend my main Activity to FragmentActivity as already it is extending ListActivity.

Please help me out.Thanks.

Upvotes: 2

Views: 201

Answers (5)

Shadow
Shadow

Reputation: 6899

Use Jake Wharton's sherlock library to support for all versions.

https://github.com/JakeWharton/ActionBarSherlock

and a sample tutorial to use fragments is given.

http://wptrafficanalyzer.in/blog/adding-navigation-tabs-containing-listview-to-action-bar-in-pre-honeycomb-versions-using-sherlock-library/

Upvotes: 0

SKK
SKK

Reputation: 5271

how can I extend my main Activity to FragmentActivity as already it is extending ListActivity.

  • Create an Activity which extends FragmentActivity itself, and then extend ListFragment for the Fragment inside that activity and use the same code existing in your ListActivity to achieve the objective.

Upvotes: 0

danijoo
danijoo

Reputation: 2843

Unfortunatelly, the android support library doesnt contain a FragmentListActivity. You have to create your own one by letting a custom Listactivity extend FragmentActivity.

I found this class on GitHub. Maybe you give it a try :)

*
 * 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);
    }
    };
}

Source: https://gist.github.com/panzerfahrer/4201753

Upvotes: 1

reidzeibel
reidzeibel

Reputation: 1632

Here's the logic to convert it.

  1. Import the Library as described on your link Fragments in Android 2.2.1, 2.3, 2.0. Is this possible?
  2. Divide your List Activity into a FragmentActivity and ListFragment
  3. Load the ListFragment into FragmentActivity

This won't be an easy, as you might have to change the entire code, but you can always google about it.

Good luck ^^

Upvotes: 0

Dory Zidon
Dory Zidon

Reputation: 10719

use the android support library, allows you to support fragments all the way down to 2.x versions of android.

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

Upvotes: 0

Related Questions