android - 如何正确填充 RecyclerView?
问题描述
我正在尝试创建一个简单的应用程序,其中在主要活动中有一个 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_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
android:text="Destaque" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Perto de Si" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="match_parent"
tools:context=".TestFragment">
<com.google.android.material.card.MaterialCardView
android:id="@+id/card_fragment"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_margin="16dp"
app:cardElevation="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/card_image"
android:layout_width="match_parent"
android:layout_height="200dp"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="match_parent"
tools:context=".RecyclerFragment">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_fragment"
/>
</FrameLayout>
解决方案
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_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.card.MaterialCardView
android:id="@+id/card_fragment"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_margin="16dp"
app:cardElevation="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/card_image"
android:layout_width="match_parent"
android:layout_height="200dp"
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_width="wrap_content"
android:layout_height="wrap_content"
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>
推荐阅读
- flutter - Flutter:MultipartRequest返回403 Forbidden ... CURL工作正常
- javascript - React native, Search filter not working properly
- c++ - 使用递归在 bst 中插入元素
- reactjs - 如何在 React 本机 webview 中执行异步任务?
- java - PEM 文件到 Java 中的 RSA 私钥
- sql - Alteryx Designer 与 MS SQL Server 聚合差异
- speech-recognition - 有没有办法自动将 m4a 文件转换为 wav - 用于 Azure 语音服务
- postgresql - 截断 Postgres 中的所有表,列表中提供的表除外
- google-sheets - 将其封装在 ArrayFormula 中时公式更改的结果
- java - Spring - 在子线程中具有请求上下文的替代方法