I have a horizontal scroll view, I also have a relativelayout as it's child. I am adding child views of this relativelayout dynamically. I have a header text which should be update when I scroll according to respective child views. How can I do this because I am able to get the current focused item in horizontal scroll. Please give me some suggestion or examples which can be helpful for me, thanks..
2 Answers
If you are creating this childs dinamically you can set a tag to them with the content you want to show in the header TextView
.
//Creating RelativeLayout childs
TextView newChild = new TextView(this).
newChild.setTag(textToShowWhenThisItemIsFocused);
Then if you know which item is focused you just have to get the tag.
// "selected" is the focused view
header.setText((String) selected.getTag());
When to use the second code depends on your implementation. Since you didn't provide any code it's hard to know how to monitor the scroll, but i.e. you could control Touch Events
and update the header when the user is moving his finger over the screen (you should also take into account the inertia after the user stops tapping).
EDIT: How to get the focused View
First of all, I barely have experience doing things like this, so I'm not sure if this is going to work or if there are better ways to do it. I'll just tell you the way I would approach this problem.
To be able to know the focused View you need to know the coordinates where this View should be. Since it's an horizontal ScrollView we will need the X coordinate. Since we want the View in the middle of the ScrollView I would do it like this:
private int centerSV;
private ScrollView mScrollView;
...
centerSV = mScrollView.getWidth()/2;
Now we have the center of the ScrollView. Now we need to know which child is in this position:
private int getFocusedChildId(){
for(int i=0; i<mChilds.length; i++){
int childLeftCoord = mChilds[i].getLeft() - mScrollView.getScrollX();
if(childLeftCoord <= centerSV && centerSV <= childLeftCoord + mChilds[i].getWidth())
return mChilds[i].getId();
}
// No view found in the center, maybe ScrollView wasn't full. Return the first one
return mChilds[0].getId();
}
Again, I'm not sure if this is going to work, it's just an idea of how to approach your issue. Also, you should take this into account:

- 1
- 1

- 8,065
- 7
- 42
- 66
-
How can I know which item is focused please give me some suggestions on it. – user2349315 Jul 04 '13 at 07:23
-
I thought you were already doing that: "I am able to get the current focused item in horizontal scroll". I need to know a couple things about your app. By "the focused item" you mean the View that is right in the middle of the ScrollView? – PX Developer Jul 04 '13 at 07:25
-
yes exactly, I want to get the focused view id which is in the middle of the horizontal scrollview. – user2349315 Jul 04 '13 at 07:29
You should specify an OnTouchListener
for your HorizontalScrollView
and in it's onTouch()
method detect the type of MotionEvent
and change your TextView
's color to the appropriate

- 1,344
- 1
- 9
- 22