java - 当线性布局在按钮单击上可见时如何向下滚动线性布局 [kotlin]

标签 java android kotlin scrollview android-linearlayout

我在 ScrollView 内有一堆 TextView 和编辑文本表单,并且在同一 ScrollView 内的表单下方还有一个线性布局,在此线性布局中具有垂直的一堆 TextView 。最初,当用户按下“添加更多信息”时,线性布局的可见性消失,该信息就在此线性布局上方,按钮然后布局可见性可见。这按预期工作正常,但我想要的是当线性布局可见时, ScrollView 应该向下滚动,以便用户可以看到扩展数据。我尝试了一切,但没有任何东西可以帮助我。 请帮我解决这个问题。下面是 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)  })

关于java - 当线性布局在按钮单击上可见时如何向下滚动线性布局 [kotlin],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54716405/

相关文章:

java - 这里有多少个线程?

java - Arrays.copyOfRange() 的运行时

android - 是否可以从 cordova 的现有 .ipa 中读出代码

android - 在谷歌地图中沿着道路画一条线

java - 错误 : Entities and Pojos must have a usable public constructor.

android - 将数据从 Angular6 微应用程序传递到移动应用程序

kotlin - 将NumberFormat.parse()的结果转换为BigDecimal

java GUI编程工具?

java - 数字金字塔,采用递归方法。 Java初学者

android - 在 android 中使用分页的 ScrollView