views:

25

answers:

1

I have a ViewFlipper that contains Layouts.

Is there a way I can hook up a Class to manage each layout seperately?

Kind of like setContentView(R.layout.main); but I'm not sure how to reference the 3 different layouts in code.

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" >
          <ListView android:layout_width="wrap_content" android:id="@+id/ListView" android:layout_height="220dp"></ListView>
          <ViewFlipper android:id="@+id/ViewFlipper" android:layout_width="fill_parent" android:layout_height="fill_parent">
                <LinearLayout android:id="@+id/LinearLayoutST" android:layout_width="fill_parent" android:layout_height="fill_parent">
                      <ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/icon"></ImageView>
                </LinearLayout>
                <LinearLayout android:id="@+id/LinearLayoutChart" android:layout_height="fill_parent" android:layout_width="fill_parent">
                      <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="View 2"></TextView>
                </LinearLayout>
                <LinearLayout android:id="@+id/LinearLayoutDetails" android:layout_height="fill_parent" android:layout_width="fill_parent">
                      <TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="View 3"></TextView>
                </LinearLayout>
          </ViewFlipper>
    </LinearLayout>
A: 

You can do this in two ways:

  1. Attach a corresponding class instance to the view or layout you want to manipulate. This is sufficient if you just want to use regular calls, set OnClickListeners etc:

    LinearLayout layoutChart = (LinearLayout)findViewById(R.id.LinearLayoutChart);

  2. If you need to override default class behaviour (i.e. subclass), create an extended class:

    public class LinearLayoutChart extends LinearLayout { ...}

Make sure to implement all parent constructors (even if just passing on to calling super...), especially the ones that take the attributes.

Implement your customized code in the extension class.

Then, edit your layout xml and replace the LinearLayout tag and end tag with the fully qualified class name of your special class including its package name

<com.mycompany.mypackage.LinearLayoutChart>
<!-- layout definition as before -->
</com.mycompany.mypackage.LinearLayoutChart>

This will make the layout inflater instantiate your class instead of the stock one, so you can override protected methods etc.

Thorstenvv