簡體   English   中英

如何向下滾動線性布局當按鈕上顯示linearLayout時,單擊[kotlin]

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM