How to get the previous or next view

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

View SlideButton with identifier "remotelight". I can get a SlideButton view using the "findViewById" method. so after I get the SlideButton view, how to get the previous view and the next view without using the findViewById method.

Thank you very much.

below is the xml layout part:

<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> 
+4
source share
2 answers

To do this, you can use an instance of ViewGroup , which acts as a container for your widgets.

See getChildAt (int) and indexOfChild (View)

So getChildAt(indexOfChild(v)+1) will give you the following view.

So something like

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

You must add checks for overflow and that the following view is of the type you really want.

+11
source

I created two functions that used the @CjS solution:

 /** * @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; } 
0
source

Source: https://habr.com/ru/post/1443645/


All Articles