简体   繁体   中英

How to Scroll Down Linear Layout When linearLayout is visible on button click [kotlin]

I have bunch of text view and edit text form inside a scroll view and also having a linear Layout below form inside same scroll view in this linear layout having bunch of text view vertical. Initially linear layout has visibility gone when user press "add more info",which is aling just above this linear layout, button then layout visibility is Visible. This is working fine as expected but what i want is when linear layout get visible then scroll view should scroll down so that user can see the extended data. I tried all but nothing can help me. Please help me to solve this.Below is ShowHideLayout method. Need Help

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")
    }
}

You can solve this adding another scrollview before your exapandLinearLayout in xml file and call this scrollview on button click so your whenever your linear layout is it will scroll down. But it may not be best practice. Your code will be like below

<Scrollview
     android:id = @+id/scrollViewLayout
       .....
       ....>
    <LinearLayout....>

<--Remaining view-->

</LinearLayout>
       </Scrollview>

And in Java on addmoreinfo button click call this scrollview like below:-

    fun showHideLayout() { 
if(linearLayoutAddMoreInfoExpand.visibility == GONE){ 
linearLayoutAddMoreInfoExpand.visibility = VISIBLE
mTextViewAddMoreInfo.setText("Close Add More Info")
scrollViewLayout.post(Runnable { scrollView.fullScroll(ScrollView.FOCUS_DOWN)  })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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