Android约束布局小毛病记录

博客内容讲述了在使用ViewPager2展示RecyclerView时遇到的问题,即在上划加载更多时,底部视图的位置显示不正确。问题根源在于使用了约束布局(ConstraintLayout)。通过将布局更改为线性布局(LinearLayout),问题得到了解决。博客内容涉及到Android开发中的布局管理,ViewPager2,RecyclerView,以及布局转换。
摘要由CSDN通过智能技术生成

ViewPager2 子page 是 recyclerview, 上划底部view 加载更多,正在加载,位置显示有问题。最后发现,是因为使用了约束布局。

 <fly.component.widgets.EasyRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:finishLoadMore="@{viewModel.finishLoadMore}"
        app:finishLoadMoreWithNoMoreData="@{viewModel.finishLoadMoreWithNoMoreData}"
        app:finishRefresh="@{viewModel.finishRefresh}"
        app:loadMoreAnimation="@{viewModel.loadMoreAnimation}"
        app:onLoadMoreListener="@{viewModel.onLoadMoreListener}"
        app:onRefreshListener="@{viewModel.onRefreshListener}"
        app:refreshAnimation="@{viewModel.refreshAnimation}">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:adapter="@{viewModel.adapter}"
            app:itemBinding="@{viewModel.itemBinding}"
            app:items="@{viewModel.items}"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
    </fly.component.widgets.EasyRefreshLayout>
<!-- 更改之前布局 -->
<?xml version="1.0" encoding="utf-8"?>
<layout 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:fitsSystemWindows="true"
    tools:ignore="ResourceName">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewPager2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:fragments="@{viewModel.items}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/stickylayout_indicator"
            app:userInputEnabled="@{true}" />

        <fly.component.widgets.tabflowlayout.TabFlowLayout
            android:id="@id/stickylayout_indicator"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_marginLeft="@dimen/activity_margin_h"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:tabPager2="@{viewPager2}"
            app:tabSelectedColor="@{@color/indicator_tab_selected_color}"
            app:tabTitles="@{viewModel.pagerTitles}"
            app:tabUnSelectedColor="@{@color/indicator_tab_unselected_color}"
            app:tab_height="3dp"
            app:tab_item_autoScale="true"
            app:tab_item_res="@drawable/home_selectline"
            app:tab_scale_factor="1.4"
            app:tab_type="res"
            app:tab_width="16dp" />

        <TextView
            android:layout_width="35dp"
            android:layout_height="21dp"
            android:layout_marginRight="@dimen/activity_margin_h"
            android:background="@drawable/shape_msg_count"
            android:gravity="center"
            android:text="发布"
            app:bindClick="@{viewModel.publishClick}"
            android:textColor="#ffffff"
            android:textSize="11sp"
            app:layout_constraintBottom_toBottomOf="@id/stickylayout_indicator"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="@id/stickylayout_indicator" />

        <!--        <androidx.recyclerview.widget.RecyclerView-->
        <!--            app:layout_constraintTop_toBottomOf="@id/tabFlowLayout"-->
        <!--            app:layout_constraintLeft_toLeftOf="parent"-->
        <!--            app:layout_constraintRight_toRightOf="parent"-->
        <!--            app:layout_constraintBottom_toBottomOf="parent"-->
        <!--            android:layout_width="match_parent"-->
        <!--            android:layout_height="0dp"-->
        <!--            app:adapter="@{viewModel.adapter}"-->
        <!--            app:itemBinding="@{viewModel.itemBinding}"-->
        <!--            app:items="@{viewModel.items}"-->
        <!--            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />-->

        <ImageView
            android:layout_width="53dp"
            android:layout_height="53dp"
            android:layout_marginRight="9dp"
            android:layout_marginBottom="9dp"
            android:src="@mipmap/dynamic_release"
            app:bindClick="@{viewModel.publishClick}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <data>

        <variable
            name="viewModel"
            type="fly.business.dynamic.viewmodel.DynamicModel" />
    </data>
</layout>

修改后布局,使用线性布局,不再有问题

<?xml version="1.0" encoding="utf-8"?>
<layout 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:fitsSystemWindows="true"
    tools:ignore="ResourceName">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <fly.component.widgets.tabflowlayout.TabFlowLayout
                android:id="@id/stickylayout_indicator"
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_marginLeft="@dimen/activity_margin_h"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:tabPager2="@{viewPager2}"
                app:tabSelectedColor="@{@color/indicator_tab_selected_color}"
                app:tabTitles="@{viewModel.pagerTitles}"
                app:tabUnSelectedColor="@{@color/indicator_tab_unselected_color}"
                app:tab_height="3dp"
                app:tab_item_autoScale="true"
                app:tab_item_res="@drawable/home_selectline"
                app:tab_scale_factor="1.4"
                app:tab_type="res"
                app:tab_width="16dp" />

            <androidx.viewpager2.widget.ViewPager2
                android:id="@+id/viewPager2"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                app:fragments="@{viewModel.items}"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                app:userInputEnabled="@{true}" />
        </LinearLayout>

        <TextView
            android:layout_width="35dp"
            android:layout_height="21dp"
            android:layout_gravity="right"
            android:layout_marginTop="10dp"
            android:layout_marginRight="@dimen/activity_margin_h"
            android:background="@drawable/shape_msg_count"
            android:gravity="center"
            android:text="发布"
            android:textColor="#ffffff"
            android:textSize="11sp"
            app:bindClick="@{viewModel.publishClick}" />

        <ImageView
            android:layout_width="53dp"
            android:layout_height="53dp"
            android:layout_gravity="right|bottom"
            android:layout_marginRight="9dp"
            android:layout_marginBottom="9dp"
            android:src="@mipmap/dynamic_release"
            app:bindClick="@{viewModel.publishClick}" />

    </FrameLayout>

    <data>

        <variable
            name="viewModel"
            type="fly.business.dynamic.viewmodel.DynamicModel" />
    </data>
</layout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值