自定义视图更改尺寸时滚动NestedScrollView

时间:2019-11-07 19:01:47

标签: android android-custom-view

我有带有按钮和自定义视图的布局。自定义视图具有默认情况下可见的文本视图和另一个已消失的文本视图的布局(就像带有问题和答案的视图一样,当用户单击它时,答案是可见的-展开视图)。我需要这样的行为:单击按钮时,第二个文本视图变为可见(视图被展开)并且父级布局滚动,因此所有自定义视图在屏幕上变为可见。 但是,确实,屏幕滚动到从一开始就可见的textview,而带有答案的textview不在屏幕的可见空间内。 注意,我展示了最小的代码版本,片段的实际布局还有更多其他视图,因此滚动很有必要。 有人能弄清楚我做错了吗?感谢您的关注。

调用requestLayout()和invalidate()无法解决问题。

class ExpandableQuestionView : ConstraintLayout{

    private var isViewExpanded: Boolean = false
    private var questionTitle: String = ""
    private var questionMessage: String = ""

    constructor(context: Context) : super(context) {
        initializeViews(null)
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        initializeViews(attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr) {
        initializeViews(attrs)
    }

    private fun initializeViews(attrs: AttributeSet?) {
        val inflater = 
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        inflater.inflate(R.layout.view_expandable_question, this)

        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.ExpandableQuestionView)

        isViewExpanded = typedArray.getBoolean(R.styleable.ExpandableQuestionView_isViewExpanded, false)
        questionTitle = typedArray.getString(R.styleable.ExpandableQuestionView_question_title)?: ""
        questionMessage = typedArray.getString(R.styleable.ExpandableQuestionView_question_message)?: ""

        typedArray.recycle()

        question_title.text = questionTitle
        question_message.text = questionMessage
    }

    fun onClick() {
        if (isViewExpanded) collapseView() else expandView()
    }

    fun expandView() {
        isViewExpanded = true
        question_title.isVisible = true
        question_message.isVisible = true
        question_image.background = ContextCompat.getDrawable(context, R.drawable.remove)
        requestLayout()
        //invalidate()
    }

    private fun collapseView() {
        isViewExpanded = false
        question_title.isVisible = true
        question_message.isVisible = false
        question_image.background = ContextCompat.getDrawable(context, R.drawable.add)
        requestLayout()
        //invalidate()
    }
}

view_expandable_question布局:

<merge xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       xmlns:tools="http://schemas.android.com/tools"
       android:id="@+id/expandable_question_container"
       android:layout_height="match_parent"
       android:layout_width="match_parent"
     tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">

    <TextView
        android:id="@+id/question_title"
        style="@style/GreenTitleStyle"
        android:gravity="start"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/padding_guideline"
        app:layout_constraintBottom_toTopOf="@+id/question_message"
        app:layout_constraintTop_toTopOf="parent"/>

    <ImageView
        android:id="@+id/question_image"
        android:layout_width="20dp"
        android:layout_height="20dp"
        app:layout_constraintTop_toTopOf="@id/question_title"
        app:layout_constraintBottom_toBottomOf="@id/question_title"
        app:layout_constraintEnd_toEndOf="parent"
        android:background="@drawable/add"/>

    <TextView
        android:id="@+id/question_message"
        style="@style/BlackTitleStyle"
        android:gravity="start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:layout_marginEnd="40dp"
        android:layout_marginBottom="16dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/padding_guideline"
        app:layout_constraintTop_toBottomOf="@id/question_title"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/padding_guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.9"/>

</merge>

片段布局:

<androidx.core.widget.NestedScrollView
        android:id="@+id/transaction_detail_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <com.company.ui.internal.ExpandableQuestionView
            android:id="@+id/question"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            app:question_title="calculate_cashback_question_title"
            app:question_message="calculate_cashback_question_message"
            app:isViewExpanded="false"/>

    </LinearLayout>

</androidx.core.widget.NestedScrollView>

我的片段:

class TransactionDetailedFragment {

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        button.setOnClickListener {
            question.expandView()
            transaction_detail_container.requestLayout()
            //transaction_detail_container.invalidate()
            val location = IntArray(2)
            question.getLocationOnScreen(location)
            val y = location[1]
            scrollScreen(y /*transaction_detail_container.bottom*/)
        }
    }

    private fun scrollScreen(y: Int) {
        transaction_detail_container.smoothScrollTo(0, y)
   }
}

0 个答案:

没有答案
相关问题