![](/img/trans.png)
[英]How to fix my LinearLayout to scroll down when content exceeds view
[英]How to Scroll Down Linear Layout When linearLayout is visible on button click [kotlin]
我在滚动视图中有一堆文本视图和编辑文本表单,并且在具有一堆文本视图垂直的线性布局中,在同一滚动视图中的表单内部也有一个线性布局。 最初,当用户按下“添加更多信息”时,线性布局的可见性消失了,该状态正好在此线性布局上方,然后单击按钮即可看到可见的布局。 这按预期工作正常,但是我想要的是当线性布局变得可见时,滚动视图应向下滚动,以便用户可以看到扩展的数据。 我尽了一切,但没有任何办法可以帮助我。 请帮我解决这个问题。下面是ShowHideLayout方法。 需要帮忙
fun showHideLayout() {
if (linearLayoutAddMoreInfoExpand.visibility == GONE) {
linearLayoutAddMoreInfoExpand.visibility = VISIBLE
mTextViewAddMoreInfo.setText("Close Add More Info")
scrollView.post(Runnable {
scrollView.fullScroll(ScrollView.FOCUS_DOWN)
//scrollView.scrollTo(0,scrollView.bottom)
})
// scrollView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
scrollView.scrollTo(0,scrollView.bottom)
//scrollView.smoothScrollBy(R.id.linearLayoutAddMoreInfoExpand,R.id.scrollBottom)
} else if (linearLayoutAddMoreInfoExpand.visibility== VISIBLE) {
linearLayoutAddMoreInfoExpand.visibility = GONE
mTextViewAddMoreInfo.setText("+ Add More Info")
}
}
您可以解决此问题,方法是在xml文件中的exapandLinearLayout之前添加另一个scrollview,并在按钮单击时调用此scrollview,这样您的线性布局就会向下滚动。 但这可能不是最佳实践。 您的代码如下所示
<Scrollview
android:id = @+id/scrollViewLayout
.....
....>
<LinearLayout....>
<--Remaining view-->
</LinearLayout>
</Scrollview>
然后在Java中的addmoreinfo按钮上,单击调用此scrollview,如下所示:-
fun showHideLayout() {
if(linearLayoutAddMoreInfoExpand.visibility == GONE){
linearLayoutAddMoreInfoExpand.visibility = VISIBLE
mTextViewAddMoreInfo.setText("Close Add More Info")
scrollViewLayout.post(Runnable { scrollView.fullScroll(ScrollView.FOCUS_DOWN) })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.