用Android实现代码使用TabLayout和ViewPager2实现如下图所示界面效果。其中导航标签至少有3个,推荐页显示商品列表,每个商品包括图片、商品名称、介绍、价格;其他页面不做详细要求,自行模拟实现即可。
时间: 2024-02-16 11:03:10 浏览: 137
好的,这是一个常见的Android开发问题,需要使用TabLayout和ViewPager2来实现多个页面的导航和切换。以下是实现步骤的大致流程:
1. 创建一个新的Android项目,并在布局文件中添加TabLayout和ViewPager2控件。
2. 在MainActivity中,创建一个FragmentPagerAdapter类来管理ViewPager2中的不同页面。
3. 在FragmentPagerAdapter类中,创建多个Fragment类来显示不同的页面内容。
4. 在每个Fragment中,使用RecyclerView来显示商品列表,并在RecyclerView的Adapter中设置商品列表项的布局和内容。
5. 在MainActivity中,将TabLayout和ViewPager2绑定在一起,并将FragmentPagerAdapter中的Fragment添加到ViewPager2中。
6. 运行应用程序,查看效果。
以下是一个示例代码,供您参考:
MainActivity.java
```
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.tabs.TabLayout;
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager2 viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = findViewById(R.id.tab_layout);
viewPager = findViewById(R.id.view_pager);
// 添加页面
viewPager.setAdapter(new MyPagerAdapter(this));
// 将TabLayout和ViewPager2绑定在一起
tabLayout.setupWithViewPager(viewPager);
}
private static class MyPagerAdapter extends FragmentStateAdapter {
public MyPagerAdapter(MainActivity activity) {
super(activity);
}
@Override
public Fragment createFragment(int position) {
switch (position) {
case 0:
return new RecommendFragment();
case 1:
return new CategoryFragment();
case 2:
return new CartFragment();
default:
return new Fragment();
}
}
@Override
public int getItemCount() {
return 3;
}
}
}
RecommendFragment.java
```
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class RecommendFragment extends Fragment {
private RecyclerView recyclerView;
private List<Goods> goodsList = new ArrayList<>();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_recommend, container, false);
// 初始化商品列表数据
initGoods();
// 初始化RecyclerView
recyclerView = view.findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
GoodsAdapter adapter = new GoodsAdapter(goodsList);
recyclerView.setAdapter(adapter);
return view;
}
private void initGoods() {
for (int i = 0; i < 20; i++) {
Goods goods = new Goods();
goods.setImage(R.drawable.ic_launcher_foreground);
goods.setName("商品" + i);
goods.setDesc("这是一件很好的商品!");
goods.setPrice(100.00);
goodsList.add(goods);
}
}
}
```
fragment_recommend.xml
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
```
Goods.java
```
public class Goods {
private int image;
private String name;
private String desc;
private double price;
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
```
GoodsAdapter.java
```
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.ViewHolder> {
private List<Goods> goodsList;
static class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView nameText;
TextView descText;
TextView priceText;
public ViewHolder(View view) {
super(view);
imageView = view.findViewById(R.id.image_view);
nameText = view.findViewById(R.id.name_text);
descText = view.findViewById(R.id.desc_text);
priceText = view.findViewById(R.id.price_text);
}
}
public GoodsAdapter(List<Goods> goodsList) {
this.goodsList = goodsList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.goods_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Goods goods = goodsList.get(position);
holder.imageView.setImageResource(goods.getImage());
holder.nameText.setText(goods.getName());
holder.descText.setText(goods.getDesc());
holder.priceText.setText("¥" + String.format("%.2f", goods.getPrice()));
}
@Override
public int getItemCount() {
return goodsList.size();
}
}
```
goods_item.xml
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<ImageView
android:id="@+id/image_view"
android:layout_width="80dp"
android:layout_height="80dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="16dp">
<TextView
android:id="@+id/name_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="商品名称" />
<TextView
android:id="@+id/desc_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="商品介绍" />
<TextView
android:id="@+id/price_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="商品价格" />
</LinearLayout>
</LinearLayout>
```
以上是一个简单的示例,您可以根据自己的需求和设计来修改和扩展。
阅读全文