6

how can I get the previous view(TextView) of the SlideButton view, and the next view of the SlideButton view.

The SlideButton view with the id "remotelight". I can get the SlideButton view with the method "findViewById". so after I get the SlideButton View, how can I get the previous view and the next view ,without the help of "findViewById" method.

thanks so much.

below is a part of layout xml:

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

    <com.ivn.SlideButton
        android:id="@+id/remotelight"
        android:layout_width="100dp"
        android:layout_height="50dp" />


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="" />

</TableRow>
Jonas
  • 121,568
  • 97
  • 310
  • 388
explorer
  • 449
  • 2
  • 10
  • 19

3 Answers3

13

You can use the ViewGroup instance that's acting as a container for your widgets to do this.

See getChildAt(int) and indexOfChild(View)

So getChildAt(indexOfChild(v)+1) will get you the next view.

So something like

 ViewGroup container = (ViewGroup) findViewbyID(R.tableRow1);
 View slideBtn = findViewbyID(R.remoteLight);
 View nextView = container.getChildAt(container.indexOfChild(slideBtn)+1);

You should add checks for overflow and that the next view is of the type that you do in fact want.

CjS
  • 2,037
  • 2
  • 21
  • 29
  • thanks so much. however, how can I get the viewGroup instance? Both of the methods are owned by ViewGroup. Thanks again! – explorer Nov 02 '12 at 12:15
  • The TableRow object is a ViewGroup. You can get hold of the TableRow with `findViewbyId(R.tableRow1)` – CjS Nov 02 '12 at 12:27
  • Hello, are you online, thanks for help me once again! I try my best to wait for your reply. – explorer Nov 02 '12 at 12:29
  • en thanks so much haha, and I use "ViewGroup vg = (ViewGroup)v.getParent();" get the ViewGroup. – explorer Nov 02 '12 at 12:36
0

I created two functions that put @CjS solution into use:

/**
 * @param view
 */
public static View prev(View view) {

    ViewGroup container = (ViewGroup) view.getParent();

    int indexOfChild = container.indexOfChild(view);

    if ((indexOfChild - 1) >= 0) {

        return container.getChildAt(indexOfChild - 1);

    }

    return null;

}

/**
 * @param view
 */
public static View next(View view) {

    ViewGroup container = (ViewGroup) view.getParent();

    int indexOfChild = container.indexOfChild(view);

    if (container.getChildCount() > (indexOfChild + 1)) {

        return container.getChildAt(indexOfChild + 1);

    }

    return null;

}
Rotem
  • 2,306
  • 3
  • 26
  • 44
0

I've created an equivalent in Kotlin Extension:

fun View.getPreviousView() : View? {
    val container : ViewGroup = this.parent as ViewGroup
    val indexOfChild = container.indexOfChild( this )
    return if (indexOfChild - 1 >= 0) {
        container.getChildAt(indexOfChild - 1)
    } else null
}

fun View.getNextView() : View?{
    val container : ViewGroup = this.parent as ViewGroup
    val indexOfChild = container.indexOfChild( this )
    return if (container.childCount > indexOfChild + 1) {
        container.getChildAt(indexOfChild + 1)
    } else null
}

Moises Portillo
  • 828
  • 8
  • 12