[CymChad/BaseRecyclerViewAdapterHelper](RecyclerView嵌套RecyclerView)Item中有Item RecyclerView时,上下滑动布局发生变化。

2024-08-08 496 views
9
(RecyclerView嵌套RecyclerView)Item中有Item RecyclerView时,上下滑动布局发生变化。

先上图:第一次进入时,布局是正常的。

当向下滑动之后,又向上滑动时,布局大小发生变化。如图:

代码如下:

private void initRecyclerView() {
    mAdapter = new SchoolmateAdapter(R.layout.item_topic_details, mData, this);
    mAdapter.setOnLoadMoreListener(this, mRecyclerView);
    mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
//        mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mRecyclerView.setAdapter(mAdapter);
    mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            Toaster.showShort("当前位置" + position);
            SchoolmateBean.ContentBean bean = (SchoolmateBean.ContentBean) adapter.getItem(position);
            Intent intent = new Intent(SchoolmateActivity.this, SchoolmateDetailsActivity.class);
            intent.putExtra(TEACHING_SHOWID, bean.getTeachingShowId());
            startActivity(intent);
        }
    });

//SchoolmateAdapter中主要代码
@Override
protected void convert(BaseViewHolder helper, final SchoolmateBean.ContentBean item) {
    ImageLoader.getInstance().loadCircleImage(mContext, item.getPhoto(), (ImageView) helper.getView(R.id.iv_avatar));
    helper.setText(R.id.tv_name, item.getPersonName())
            .setText(R.id.tv_devices, "")
            .setText(R.id.tv_time, "")
            .setText(R.id.tv_content, item.getText());
    //显示图片,最多6张
    if (item != null && item.getPhotopath() != null && item.getPhotopath().size() > 0) {

        RecyclerView recyclerView = helper.getView(R.id.recycler_view_img);
        initImageRecyclerView(recyclerView, item.getPhotopath(), mContext);

    }
}

/**
 * 初始化图片
 * 图片最多6张
 *
 * @param recyclerView
 * @param images
 */
private void initImageRecyclerView(RecyclerView recyclerView, List<String> images, Context context) {
    ImageAdapter imageAdapter = new ImageAdapter(R.layout.item_student_style_image, images, context);
    GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(gridLayoutManager);
    recyclerView.setAdapter(imageAdapter);
    imageAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
        @Override
        public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
            Toaster.showShort("position=" + position);
        }
    });
}
//ImageAdapter主要代码
@Override
protected void convert(BaseViewHolder helper, String item) {
    ImageView imageView = helper.getView(R.id.iv_img);
    ImageLoader.getInstance().loadRoundImage(mContext, item, DisplayUtils.dp2px(5), imageView);
    //为图片添加点击事件
    helper.addOnClickListener(R.id.iv_img);
}

//SchoolmateAdapter的Item布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/dp_10"
android:background="@drawable/selector_recycler_view_bg"
android:orientation="vertical">

<ImageView
    android:id="@+id/iv_avatar"
    android:layout_width="@dimen/dp_40"
    android:layout_height="@dimen/dp_40"
    android:layout_marginLeft="@dimen/dp_16"
    android:layout_marginStart="@dimen/dp_16"
    android:layout_marginTop="@dimen/dp_16"
    android:scaleType="centerCrop"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/gys" />

<TextView
    android:id="@+id/tv_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/dp_24"
    android:layout_marginStart="@dimen/dp_24"
    android:text="张琳琳 · 妈妈"
    android:textColor="@color/black_333_color"
    android:textSize="@dimen/text_size_18"
    app:layout_constraintLeft_toRightOf="@+id/iv_avatar"
    app:layout_constraintTop_toTopOf="@+id/iv_avatar" />

<TextView
    android:id="@+id/tv_devices"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="来自iphone 7"
    android:textColor="@color/black_999_color"
    android:textSize="@dimen/text_size_14"
    app:layout_constraintBottom_toBottomOf="@+id/iv_avatar"
    app:layout_constraintLeft_toLeftOf="@+id/tv_name"
    app:layout_constraintTop_toBottomOf="@+id/tv_name" />

<TextView
    android:id="@+id/tv_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="@dimen/dp_16"
    android:layout_marginRight="@dimen/dp_16"
    android:text="五个小时之前"
    android:textColor="@color/black_999_color"
    android:textSize="@dimen/text_size_14"
    app:layout_constraintBottom_toBottomOf="@+id/tv_devices"
    app:layout_constraintRight_toRightOf="parent" />

<android.support.constraint.Guideline
    android:id="@+id/guideline3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_begin="@dimen/dp_80"
    tools:layout_editor_absoluteX="@dimen/dp_80"
    tools:layout_editor_absoluteY="0dp" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view_img"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="@dimen/dp_16"
    android:layout_marginTop="0dp"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="@+id/guideline3"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tv_devices" />

<TextView
    android:id="@+id/tv_content"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    android:layout_marginTop="@dimen/dp_16"
    android:text="小星星不会了,动手做一个小星星还是可以的。"
    android:textColor="@color/black_333_color"
    android:textSize="@dimen/text_size_16"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="@+id/guideline3"
    app:layout_constraintRight_toRightOf="@+id/recycler_view_img"
    app:layout_constraintTop_toBottomOf="@+id/recycler_view_img" />

<ImageView
    android:id="@+id/view4"
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    android:layout_marginTop="@dimen/dp_16"
    android:background="#000000"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="@+id/guideline3"
    app:layout_constraintRight_toRightOf="@+id/tv_content"
    app:layout_constraintTop_toBottomOf="@+id/tv_content" />

<com.kuojie.family_xiaoyinxing.widget.MyClickTools
    android:id="@+id/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="0dp"
    android:layout_marginTop="0dp"
    app:layout_constraintRight_toRightOf="@+id/view4"
    app:layout_constraintTop_toBottomOf="@+id/view4" />

<TextView
    android:id="@+id/tv_delete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/dp_8"
    android:layout_marginLeft="@dimen/dp_24"
    android:layout_marginTop="@dimen/dp_8"
    android:text="删除"
    android:textColor="@color/black_666_color"
    android:textSize="@dimen/text_size_16"
    android:visibility="invisible"
    app:layout_constraintBottom_toBottomOf="@+id/tools"
    app:layout_constraintLeft_toLeftOf="@+id/guideline3"
    app:layout_constraintTop_toTopOf="@+id/tools" />
</android.support.constraint.ConstraintLayout>

//ImageAdapter的Item布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/selector_recycler_view_bg"
android:orientation="vertical">

<ImageView
    android:id="@+id/iv_img"
    android:layout_width="@dimen/dp_60"
    android:layout_height="@dimen/dp_60"
    android:layout_gravity="center_horizontal"
    android:layout_marginBottom="@dimen/dp_10"
    android:layout_marginLeft="@dimen/dp_10"
    android:layout_marginRight="@dimen/dp_10"
    android:layout_marginTop="@dimen/dp_10"
    android:scaleType="centerCrop"
    android:src="@drawable/gys" />
</LinearLayout>

回答

0

补充:当前使用版本号:2.9.13

5

这个布局一个RecyclerView就可以了。。

3

@287138531 item中图片最多六张,数量不确定,这个该用什么控件呢?别说用GridView 。

9

这个控件的意义就在于复杂布局啊。。一个RecyclerView搞定啊。。就用这个控件 你好好看下MultipleItemUseActivity 。。

6

你检查一下你的布局,和你的代码,adapter是不会改变你的itemview的宽度的

8

@CymChad item布局代码我已经贴出来了,下面贴出页面布局代码。如果adapter不会改变你的itemview的高度,为什么第一次向下滑动时,布局高度正常,再向上滑动时就出现高度不一样的问题呢? <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.kuojie.family_xiaoyinxing.activity.SchoolmateActivity">

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipeLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.v4.widget.SwipeRefreshLayout>

4

demo发我一下,我这边来调试。

4

@CymChad 怎么发给你,发你邮箱吗?

2

恩,it_chenyuming@163.com

0

请问这是个什么问题,怎么解决的

2

@Troll4it 将代码发给作者了,他正在看。

3

我没收到你的源码邮件

7

@ListenerGao

1

@ListenerGao 要不你上传个github项目?

4

@CymChad 上传到了https://github.com/ListenerGao/MyDemo,不过因为网络那部分用的是公司本地的服务器,数据可能得麻烦你自己造。多谢了。

8

我自己拿你的布局写了一个demo,去除了多余代码,没发现你说的问题

7

你自己检查一下你的代码

1

2

2

@CymChad 好的,我看下,多谢。