其他分享
首页 > 其他分享> > Android 踩坑日记 - RecyclerView 布局问题

Android 踩坑日记 - RecyclerView 布局问题

作者:互联网

Android 踩坑日记(05/05/20) - RecyclerView

Overview

今天来写一篇Android RecyclerView的踩坑日记,这个非常的特别,那么我们现在来看看这是个啥问题把。

问题在什么地方

看图!android_Recyclerview_问题

可以看到这个非常尴尬的问题就是要消失的item会划出界面。**

看看需要啥样的

Android_Recyclerview正常效果

这个就是我们所需要的效果。

问题所在

是不是都以为是Recyclerview 适配器或者属性问题,但是我研究了一波,发现不是。

这样显示看看代码:

package com.example.wanandroidapp_master.adpater.RecyclerView;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wanandroidapp_master.R;
import com.example.wanandroidapp_master.model.entity.ArticleEntity;

import java.util.List;

/**
 * Date 4/29/2020
 * Author CaoJiaHao
 * Description ${Description}
 **/
public class HomePage_RecyclerViewAdapter extends RecyclerView.Adapter<HomePage_RecyclerViewAdapter.ViewHolder> {
    private Context mContext;
    private List<ArticleEntity.articleInfo> mData;

    private TextView txtTitle;
    private TextView txtAuthor;
    private LinearLayout linearLayout;

    public HomePage_RecyclerViewAdapter(Context mContext, List<ArticleEntity.articleInfo> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewHolder viewHolder = new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.recommended_article_layout, parent, false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        txtTitle.setText(mData.get(position).getTitle());
        txtTitle.setTag(mData.get(position).getLink());
        txtAuthor.setText("作者:" + mData.get(position).getAuthor() + mData.get(position).getShareUser());
        linearLayout.removeAllViews();
        LinearLayout.LayoutParams lp = new
                LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(6, 0, 0, 0);
        for (ArticleEntity.tags tag : mData.get(position).getTags()) {
            TextView txtTag = new TextView(mContext);
            txtTag.setBackground(mContext.getResources().getDrawable(R.drawable.article_tag_background));
            txtTag.setText(tag.getName());
            txtTag.setTextSize(10f);
            txtTag.setLayoutParams(lp);
            txtTag.setTextColor(Color.WHITE);
            linearLayout.addView(txtTag);
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
            txtTitle = itemView.findViewById(R.id.recommendedArticle_txtArticleName);
            txtAuthor = itemView.findViewById(R.id.recommendedArticle_txtAuthor);
            linearLayout = itemView.findViewById(R.id.recommendedArticle_LinearLayout);
        }
    }



}

这个就是适配器的代码。

没有问题

所以就是布局问题了。。。

看看问题代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:clipChildren="false"
    android:orientation="vertical">

    <com.lany.banner.BannerView
        android:id="@+id/homePage_BannerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        app:banner_delayTime="5000"
        app:banner_indicatorGravity="center"
        app:banner_indicatorMargin="4dp"
        app:banner_indicatorType="circle_indicator_title_inside"
        app:banner_isAutoPlay="true"
        app:banner_scaleType="fitXY"
        app:banner_scrollTime="1000"
        app:banner_titleBackground="#50000000"
        app:banner_titleTextColor="#ffffff"
        app:banner_titleTextSize="12sp"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/homePage_RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@+id/homePage_BannerView"
        app:layout_constraintTop_toTopOf="@+id/guideline3" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.25" />


</androidx.constraintlayout.widget.ConstraintLayout>

问题代码!

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:clipChildren="false"
    android:orientation="vertical">

    <com.lany.banner.BannerView
        android:id="@+id/homePage_BannerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        app:banner_delayTime="5000"
        app:banner_indicatorGravity="center"
        app:banner_indicatorMargin="4dp"
        app:banner_indicatorType="circle_indicator_title_inside"
        app:banner_isAutoPlay="true"
        app:banner_scaleType="fitXY"
        app:banner_scrollTime="1000"
        app:banner_titleBackground="#50000000"
        app:banner_titleTextColor="#ffffff"
        app:banner_titleTextSize="12sp"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline3">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/homePage_RecyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.25" />


</androidx.constraintlayout.widget.ConstraintLayout>

正确代码!

解决方法

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline3">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/homePage_RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

就是我在外面添加了一个LinearLayout...

这样Recyclerview的父容器就是那个LinearLauout了。而且他们的大小相同,所以就解决了。

写这个是因为我遇到问题百度了一个多小时,就是找不到解决方法。于是就自己解决了一下,顺便发了个博客帮助下同样的小伙伴,要是觉得可以,点个关注哦~

标签:mData,ViewHolder,LinearLayout,public,position,import,Android,RecyclerView,日记
来源: https://www.cnblogs.com/cao-1/p/12833455.html