简体   繁体   English

Android如何以编程方式将ScrollView子级移到顶部

[英]Android how to move scrollview child to top programatically

I have a ScrollView which contains numerous views on LinearLayout .I have implemented expand and collapse layouts. 我有一个ScrollView ,其中包含有关LinearLayout大量视图。我已经实现了expandcollapse布局。 OnClick of view,it either expand or collapse.So when a view expands,I want to programmatically move ScrollView such that the view comes on top of the screen.See image below OnClick来看,其扩张或collapse.So当一个视图扩展,我想以编程方式移动ScrollView这样的观点来自下面的screen.See图像的顶部

在此处输入图片说明

I want to move the ScrollView to the length shown on red line. 我想将ScrollView移到红线所示的长度。 The child of ScrollView contains a TextView (4:Status bar notification) and an ImageView .Further ScrollView may or maynot contains views above or below the TextView ImageView combination. ScrollViewchild级包含一个TextView (4:状态栏通知)和一个ImageView 。此外, ScrollView可能包含也可能不包含TextView ImageView组合上方或下方的视图。 I have used a CustomScrollView where I findout scroll Y by 我使用了CustomScrollView,在其中找到了scroll Y

public int newY=0;
@Override
protected void onScrollChanged(int X, int Y, int oldX, int oldY) {
    super.onScrollChanged(X, Y, oldX, oldY);
    this.newY=Y;
}

For that I have used following logic 为此,我使用了以下逻辑

 public void translateView(View view){
    int[] xy =new int[2];
   //view = ImageView
    view.getLocationOnScreen(xy);
    int scrollY = xy[1]+view.getBottom();
    //screenHeight =ScreenHeight of the phone
    if(scrollY>screenHeight){
      //heading textView
        textView.getLocationOnScreen(xy);
        int diff = screenHeight-xy[1];
        diff =Math.abs(diff);
        int moveHeight = (int) (scrollView.newY+diff);
        scrollView.smoothScrollTo(0, moveHeight);

    }
}

So basically what I am trying to do is finding the difference between y position of TextView and subtracting it with screen height and scrolling the ScrollView to those difference,so that the view is shown on top.But it is not working.Any Idea on how to achieve this. 所以基本上我想做的是找到TextView y位置之间的差异,并用屏幕高度减去它,然后将ScrollView到这些差异,以使视图显示在顶部。但是它不起作用。为达到这个。

I just found what I was doing wrong. 我只是发现自己做错了。 Each screen comprises of StatusBar and in my case ActionBar .So when I was calculating the View Y position,I was calculating from top of the screen and whenever I use the increment value for ScrollView Scroll ,I was moving my view to top of the screen.So the actual solution would be 每个屏幕都由StatusBarActionBar 。因此,当我计算View Y位置时,我是从屏幕顶部开始计算的,每当我使用ScrollView Scroll的增量值时,我ScrollView Scroll视图移动到屏幕顶部所以实际的解决方案是

public void translateView(final View view){
    int[] xy =new int[2];
    view.getLocationOnScreen(xy);
    int scrollY = xy[1]+view.getBottom();


    if(scrollY>DataHolder.screenHeight){
        handle.getLocationOnScreen(xy);
        int actualY=xy[1]-DataHolder.actionbarHeight-DataHolder.statusBarHeight;
        int moveHeight = (int) (scrollView.newY+actualY);
        scrollView.smoothScrollTo(0, moveHeight);   
    }
}

So by doing this my view automatically reaches the top of the screen 这样,我的视图就会自动到达屏幕顶部

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM