在尝试长时间无法在水平和垂直的回收器视图内的 2 个卡片视图元素之间提供空间 [重复]
Posted
技术标签:
【中文标题】在尝试长时间无法在水平和垂直的回收器视图内的 2 个卡片视图元素之间提供空间 [重复]【英文标题】:After trying a long not able to provide space between 2 card view elements inside a recycler view for both horizontal and vertical [duplicate] 【发布时间】:2021-01-27 05:51:57 【问题描述】:我想在水平回收器视图和垂直回收器视图中的 2 张卡之间添加一些空间,但无法添加尝试过的所有内容,但无法获得溶胶,如下图所示对于水平的两张卡彼此重叠。
我在其中定义了回收站视图的 XML 代码和版本。 :-
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:background="@color/AppBackground">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBar"
android:layout_
android:layout_
tools:ignore="MissingConstraints">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_
android:layout_
android:background="@color/AppBackground">
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedsview"
android:layout_
android:layout_
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/appBar">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraint"
android:layout_
android:layout_
android:padding="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="@+id/text"
android:layout_
android:layout_
android:layout_marginTop="10dp"
android:fontFamily="@font/playfair_display_bold_italic"
android:text="Looking for a delecious cake?"
android:textColor="@android:color/black"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/linear"
android:layout_
android:layout_
android:layout_marginTop="10dp"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@id/recyclerViewHorizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text">
<TextView
android:id="@+id/textView3"
android:layout_
android:layout_
android:layout_marginStart="10dp"
android:fontFamily="@font/playfair_display_bold_italic"
android:text="More"
android:textColor="@android:color/black"
android:textSize="20sp"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_
android:layout_
android:layout_marginStart="200dp"
android:layout_marginEnd="20dp"
android:text="Button"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="266dp" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewHorizontal"
android:layout_
android:layout_
android:layout_marginTop="10dp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
app:layout_constraintBottom_toTopOf="@+id/textView6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.526"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linear"
tools:context=".FirstActivity">
</androidx.recyclerview.widget.RecyclerView>
<TextView
android:id="@+id/textView6"
android:layout_
android:layout_
android:layout_marginStart="0dp"
android:layout_marginTop="10dp"
android:fontFamily="@font/playfair_display_bold_italic"
android:text="Best Selling"
android:textColor="@android:color/black"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/recyclerViewVertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/recyclerViewHorizontal" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewVertical"
android:layout_
android:layout_
android:layout_marginHorizontal="20dp"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView6"
tools:context=".FirstActivity"
>
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
我的卡片 Xml 代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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_
android:layout_
android:padding="20dp"
>
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_
android:layout_
android:padding="10dp"
android:elevation="10dp"
app:cardBackgroundColor="@color/CardColor"
app:cardCornerRadius="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraint"
android:layout_
android:layout_
android:layout_gravity="center"
android:background="@color/CardColor"
android:padding="5dp">
<ImageView
android:id="@+id/imageView"
android:layout_
android:layout_
android:foregroundGravity="center_horizontal"
android:scaleType="fitXY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView4"
android:layout_
android:layout_
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:fontFamily="@font/playfair_display_bold_italic"
android:gravity="center"
android:text="Great"
android:textColor="@android:color/black"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<TextView
android:id="@+id/textViewForPieceInfo"
android:layout_
android:layout_
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:fontFamily="@font/playfair_display_bold_italic"
android:text="Ram"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
<TextView
android:id="@+id/textViewforPrice"
android:text="Shyam"
android:layout_
android:layout_
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:fontFamily="@font/playfair_display_bold_italic"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewForPieceInfo" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_
android:layout_
android:layout_marginEnd="10dp"
android:layout_marginBottom="2dp"
app:fabSize="mini"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/ic_baseline_add_24" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
我的回收站查看类代码:
package com.example.cako;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.widget.Toolbar;
import android.os.Bundle;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class FirstActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
String []x="Aaran", "Aaren", "Aarez", "Aarman", "Aaron", "Aaron-James", "Aarron", "Aaryan", "Abhinav","Ankit";
String []image= "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Aloo_Tikki_served_with_chutneys.jpg/180px-Aloo_Tikki_served_with_chutneys.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Aloo_Mattar.jpg/180px-Aloo_Mattar.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Aloo_Methi_%28Aaloo_Methi%29.JPG/180px-Aloo_Methi_%28Aaloo_Methi%29.JPG",
"https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Spicy_alloo_with_tadka_mirchi.jpg/180px-Spicy_alloo_with_tadka_mirchi.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Aloo_Tikki_served_with_chutneys.jpg/180px-Aloo_Tikki_served_with_chutneys.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Aloo_Mattar.jpg/180px-Aloo_Mattar.jpg"
;
Toolbar toolbar=findViewById(R.id.toolbar) ;
setSupportActionBar(toolbar);
RecyclerView horizontalrecyclerView =(RecyclerView)findViewById(R.id.recyclerViewHorizontal);
horizontalrecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false));
horizontalrecyclerView.setAdapter(new RecyclerVview(this,x,image));
RecyclerView verticalrecyclerView =(RecyclerView)findViewById(R.id.recyclerViewVertical);
verticalrecyclerView.setLayoutManager(new GridLayoutManager(this,2));
verticalrecyclerView.setAdapter(new RecyclerVview(this,x,image));
class RecyclerVview extends RecyclerView.Adapter<RecyclerVview.Holder>
Activity co;
String[] name;
String[] image;
public RecyclerVview(Context applicationContext, String[] x, String[] image)
co=(Activity)applicationContext;
name=x;
this.image=image;
@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View v=LayoutInflater.from(co).inflate(R.layout.layout_for_horizontal_recyclerview,parent,false);
return new Holder(v);
@Override
public void onBindViewHolder(@NonNull final Holder holder, int position)
holder.textViewForItemName.setText(name[position]);
Glide.with(co).load(image[position]).into(holder.image);
holder.floatingActionButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Toast.makeText(co, "yesWorking", Toast.LENGTH_SHORT).show();
Log.i("yes","log");
);
holder.textViewForPrice.setText("30$");
holder.textViewForPieceInfo.setText("Single Piece Only.");
@Override
public int getItemCount()
return name.length;
public class Holder extends RecyclerView.ViewHolder
ImageView image;
TextView textViewForItemName;
FloatingActionButton floatingActionButton;
TextView textViewForPrice;
TextView textViewForPieceInfo;
public Holder(@NonNull View itemView)
super(itemView);
image=itemView.findViewById(R.id.imageView);
textViewForItemName=itemView.findViewById(R.id.textView4);
floatingActionButton=itemView.findViewById(R.id.floatingActionButton);
textViewForPieceInfo=itemView.findViewById(R.id.textViewForPieceInfo);
textViewForPrice=itemView.findViewById(R.id.textViewforPrice);
【问题讨论】:
【参考方案1】:为 CarView
布局和 ContraintLayout 内部添加边距
并将其用于您的卡。
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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_
android:layout_
android:margin="8dp"
>
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_
android:layout_
android:margin="8dp"
android:elevation="10dp"
app:cardBackgroundColor="@color/CardColor"
app:cardCornerRadius="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraint"
android:layout_
android:layout_
android:layout_gravity="center"
android:background="@color/CardColor"
android:padding="5dp">
<ImageView
android:id="@+id/imageView"
android:layout_
android:layout_
android:foregroundGravity="center_horizontal"
android:scaleType="fitXY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView4"
android:layout_
android:layout_
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:fontFamily="@font/playfair_display_bold_italic"
android:gravity="center"
android:text="Great"
android:textColor="@android:color/black"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<TextView
android:id="@+id/textViewForPieceInfo"
android:layout_
android:layout_
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:fontFamily="@font/playfair_display_bold_italic"
android:text="Ram"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
<TextView
android:id="@+id/textViewforPrice"
android:text="Shyam"
android:layout_
android:layout_
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:fontFamily="@font/playfair_display_bold_italic"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewForPieceInfo" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_
android:layout_
android:layout_marginEnd="10dp"
android:layout_marginBottom="2dp"
app:fabSize="mini"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/ic_baseline_add_24" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
【讨论】:
以上是关于在尝试长时间无法在水平和垂直的回收器视图内的 2 个卡片视图元素之间提供空间 [重复]的主要内容,如果未能解决你的问题,请参考以下文章
垂直 RecyclerView 内的水平 ViewPager2
Android 布局:具有滚动行为的 Viewpager 内的垂直 Recyclerview 内的水平 Recyclerview