如何正确填充 RecyclerView?
Posted
技术标签:
【中文标题】如何正确填充 RecyclerView?【英文标题】:How to correctly populate RecyclerView? 【发布时间】:2021-05-27 10:00:18 【问题描述】:我正在尝试创建一个简单的应用程序,其中在主要活动中有一个 ViewPager2 与链接到 TabLayout 的 3 个页面。在每个页面中都有一个由 Cards 填充的 RecyclerView。仅出于测试目的,我尝试使用相同的 3 张卡片填充每个页面,但出于某种原因,在第一页中我得到了 3 张卡片,但在接下来的页面中,我得到了 3 张卡片,但每张卡片之间有很多空白空间所以你一次只能看到一张卡片,我不明白为什么会这样。如果我使用 4 张或更多卡而不是 3 张卡,则在我向下滚动的第一页中它看起来不错,但在我向上滚动后它与其他页面有相同的问题,有很多空白空间。这是我的代码:
public class MainActivity extends AppCompatActivity
String data[] = "Destaque", "Perto de Si", "Brevemente";
String evento[] = "Jola", "Bowling", "Concerto";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager2 viewPager = findViewById(R.id.pager);
viewPager.setAdapter( new SwipeAdapter(this, evento));
TabLayout tabLayout = findViewById(R.id.tabLayout);
new TabLayoutMediator(
tabLayout,
viewPager,
new TabLayoutMediator.TabConfigurationStrategy()
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position)
tab.setText(data[position]);
).attach();
//
public class SwipeAdapter extends FragmentStateAdapter
private String evento[];
public SwipeAdapter(MainActivity mainActivity, String evento[])
super(mainActivity);
this.evento = evento;
@NonNull
@Override
public Fragment createFragment(int position)
return new RecyclerFragment(evento);
@Override
public int getItemCount()
return 3;
//
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>
String eventos[];
RecyclerAdapter(String eventos[])
this.eventos = eventos;
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View cardView = inflater.inflate(R.layout.card_layout, parent, false);
// Return a new holder instance
return new ViewHolder(cardView);
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position)
String evento = eventos[position];
TextView textView = holder.textView;
ImageView imageView = holder.imageView;
textView.setText(evento);
@Override
public int getItemCount()
return eventos.length;
public static class ViewHolder extends RecyclerView.ViewHolder
ImageView imageView;
TextView textView;
public ViewHolder(@NonNull View itemView)
super(itemView);
imageView = itemView.findViewById(R.id.card_image);
textView = itemView.findViewById(R.id.card_text);
//
public class RecyclerFragment extends Fragment
String eventos[];
public RecyclerFragment(String eventos[])
this.eventos = eventos;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_recycler, container, false);
RecyclerView recyclerView = view.findViewById(R.id.recycler_fragment);
RecyclerAdapter adapter = new RecyclerAdapter(eventos);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
return view;
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:orientation="vertical"
tools:context=".MainActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/pager"
android:layout_
android:layout_
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tabLayout"
app:layout_constraintVertical_bias="0.0" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_
android:layout_
android:layout_marginBottom="680dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<com.google.android.material.tabs.TabItem
android:layout_
android:layout_
android:text="Destaque" />
<com.google.android.material.tabs.TabItem
android:layout_
android:layout_
android:text="Perto de Si" />
<com.google.android.material.tabs.TabItem
android:layout_
android:layout_
android:text="Brevemente" />
</com.google.android.material.tabs.TabLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
//
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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_
tools:context=".TestFragment">
<com.google.android.material.card.MaterialCardView
android:id="@+id/card_fragment"
android:layout_
android:layout_
android:layout_margin="16dp"
app:cardElevation="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_
android:layout_>
<ImageView
android:id="@+id/card_image"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:src="@mipmap/beer"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/card_text"
android:layout_
android:layout_
android:layout_margin="8dp"
android:text="TextView"
app:layout_constraintStart_toStartOf="@+id/card_image"
app:layout_constraintTop_toBottomOf="@+id/card_image" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
</FrameLayout>
//
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".RecyclerFragment">
<androidx.recyclerview.widget.RecyclerView
android:layout_
android:layout_
android:id="@+id/recycler_fragment"
/>
</FrameLayout>
【问题讨论】:
可能是match_parent
,表示持有卡片的片段中的高度。试试wrap_content
吗?
制作项目的FrameLayout
高度wrap_content
【参考方案1】:
card_layout
.xml 的变化framelayout
高度android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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_>
<com.google.android.material.card.MaterialCardView
android:id="@+id/card_fragment"
android:layout_
android:layout_
android:layout_margin="16dp"
app:cardElevation="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_
android:layout_>
<ImageView
android:id="@+id/card_image"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:src="@mipmap/ic_launcher"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/card_text"
android:layout_
android:layout_
android:layout_margin="8dp"
android:text="TextView"
app:layout_constraintStart_toStartOf="@+id/card_image"
app:layout_constraintTop_toBottomOf="@+id/card_image" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
</FrameLayout>
【讨论】:
以上是关于如何正确填充 RecyclerView?的主要内容,如果未能解决你的问题,请参考以下文章
ArrayList<String> 使用改造 android 解析成 recyclerview
片段内的 RecyclerView 的 Kotlin OnItemClickListener