I have multiple views in a horizontalscrollview, in an activity (view1, view2, view3 to viewN) for N-views. When i come to this activity, view1 is the the first visible item in the horizontalscrollview. But I want to start with view5 or view6 to be the first visible item in my horizontalscrollview, so when i swipe left to right, it shows up as all views view1, view2 upto view4 before that view5.
horizontalScrollView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
horizontalScrollView.scrollTo(view5.getLeft());
}
});
This waits until the views have been measured, then scrolls the scrollview to the coordinates of view5.
Haven't tested this, but it should work.
Related
I have a LinearLayout and I'm inflating a CardView in it like this:
final LinearLayout itineraryDetailLL = (LinearLayout) findViewById(R.id.itineraryDetailLinearlayout);
final View childView = getLayoutInflater().inflate(R.layout.cardview, null);
itineraryDetailLL.addView(childView);
The inflation of the child view is done on a onclick of a button. I want to scroll to the bottom of the screen whenever a new cardview is inflated. I'm doing it like this:
ScrollView scrollview = ((ScrollView) findViewById(R.id.masterScrollView));
scrollview.fullScroll(View.FOCUS_DOWN);
but this scrolls to somewhere middle of the screen and not to bottom. What am I doing wrong?
You have to post an event to happen on the next frame, when this ScrollView would be laid out:
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});
I have a fragment at the bottom of my Activity which can be filled with words(views). This is a image of the fragment:
Between the blue buttons you can inflate views by clicking on a item in an GridView that is above this fragment. Everytime I click an item a view with text: "Test" is being added. It starts left with inflating views untill it doesn't fit in the LinearLayout anymore.
When the LinearLayout is full, it looks like this:
When the layout looks full, you still can inflate views but you don't see them being added anymore.
The most right purple view you can see on the image above has a bigger height then the rest because it doesn't fit anymore.
I want to check if there is enough space in the LinearLayout to inflate a new view.
This is the code that inflates the views in the LinearLayout:
public void inflateAllViews(){
LayoutInflater inflaterr = (LayoutInflater) MainApplication.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
linearLayout.removeAllViews();
ArrayList<WordTile> wordsToInflate = WordManager.getWordsToInflate();
for(WordTile WordTile : wordsToInflate){
View word = inflaterr.inflate(R.layout.word, null);
final TextView textView = (TextView)word.findViewById(R.id.text_word);
textView.setText(WordTile.getTitle());
linearLayout.addView(word);
}
}
But I need to check whether the view can be added or not because there is no space left. Because in the background you can add unlimited views to the LinearLayout.
I tried to find a way to calculate the widths of the inflated views. The only thing I found was this:
ViewTreeObserver vto = word.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Log.d("TEST", "Height = " + word.getHeight() + " Width = " + word.getWidth());
widthOfChildren += word.getWidth();
System.out.println("widthofchildren:" + widthOfChildren);
ViewTreeObserver obs = word.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});
The code works and prints the width I need for every view. But I can't use the width outside of the OnGlobalLayoutListener....
Does anyone have a solution to my problem?
EDIT:
The problem is not the width of the LinearLayout because I know that width, it is the widths of the views that are in the LinearLayout. The LinearLayout has a standard width which doesn't change.
In scrollview, if I add any view in middle, normally all the views below the added view scrolls downside. But I want to scroll the top views of added view to upside without disturbing the bottom views. Is it possible in scrollview , please help me ?
In the figure , If view 4 was added , then view 1 has to be scrolled upwards , without changing the positions of view 2 and view 3.
You can probably get the height of the view you are adding with and then scroll the scrollview manually that many pixels
scrollView.scrollBy(0, viewAdded.getHeight())
I've been wanting to try this question for quite some time, I finally got the chance today. The method is pretty simple (in fact, #dweebo already mentioned it earlier) - we move the ScrollView up as we add the view. For getting precise (and valid) dimensions when adding, we use a ViewTreeObserver. Here's the code you can get hints from:
// Getting reference to ScrollView
final ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);
// Assuming a LinearLayout container within ScrollView
final LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
// The child we are adding
final View view = new View(ScaleActivity.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100);
view.setLayoutParams(params);
// Finally, adding the child
parent.addView(view, 2); // at index 2
// This is what we need for the dimensions when adding
ViewTreeObserver viewTreeObserver = parent.getViewTreeObserver();
viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
parent.getViewTreeObserver().removeOnPreDrawListener(this);
scrollView.scrollBy(0, view.getHeight());
// For smooth scrolling, run below line instead
// scrollView.smoothScrollBy(0, view.getHeight())
return false;
}
});
Is there any idea or how can i achieve this Animation like following picture? because i tried to give a animation on Linearlayout B when i pressed a cancel button, the linearlayout B will slide out from left to right but once LinearLayout B fully Gone, the LinearLayout C directly step to below of LinearLayout A rather than slide slowly to below of LinearLayout A. please help...
Set android:animateLayoutChanges="true" to the linear layout and you will achieve this automatically.
First add a ViewTreeObserver.OnPreDraw listener to the view C:
int bHeight = viewB.getHeight();
viewC.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
viewC.startAnimation(new TranslateAnimation(0, 0, bHeight, 0));
viewC.getViewTreeObserver().removeOnPreDrawListener(this);
return false;
}
});
Then set the visibility of B to gone.
viewB.setVisibility(View.GONE);
What this does is hides the view B, causing the view C to move up. However before C is drawn in it's new position we animate it from where it used to be to where it is now.
in my app Scrollview contains some views,i want to drag down the view,and at the same time the Scrollview will move down,but now in order to implements view drad down, i have to overrided Scrollview's onInterceptTouchEvent method to return false, thus when the view drag down the Scrollview is not move down.give me some helps,please.
the problem had solved.
if(v.getParent().equals(mLayout)){
if(event.getRawY()>mDisplayMetrics.heightPixels*2/3+100){
nonLockingScrollView.scrollBy(0, 5);
}else if(event.getRawY()<50){
nonLockingScrollView.scrollBy(0, -5);
}
}