Android CardView的基本使用和Fragment

目录

CardView

  1. CardView的基本使用      

   2.CardView常见的使用方法以及其说明

   3.CardView与RecycleView的结合使用

        1.主布局文件 

        2.子布局文件 

        3.实体类

        4.适配器

        5.在主活动中使用

Fragment

        1.Fragment的简介

        总结一下:(重点)

        2.Fragment生命周期

        3.Fragment的静态加载

               1. 先创建一个Fragment

                 2.在创建的Fragment文件里操作

                3. 在Fragment所依赖的 Activity中的activity.xml文件中调用

            3.Fragment的动态加载

                       1.在activity_mian.xml文件中写

                        2.创建一个menu包

                         3.在包里面创建一个.xml文件,在文件下写

                        4.之后再创建三个Fragment,并且在他们的.xml文件上设置背景颜色

                        5.在Acticit中写以下代码


CardView

  1. CardView的基本使用      

                CardView是用于实现卡片式布局效果的重要控件,实际上也是一个frameLayout, 只是额外提供了圆角和 阴影,看上去有立体效果。

   2.CardView常见的使用方法以及其说明

   3.CardViewRecycleView的结合使用

        1.主布局文件 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity3"
    android:padding="5dp">

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

        2.子布局文件 

                这里运用到了CardView

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="150dp"
    app:cardCornerRadius="4dp"
    app:cardElevation="6dp"
    android:layout_marginTop="5dp"
    android:padding="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="horizontal"
        android:padding="5dp">

        <ImageView
            android:id="@+id/iv_img"
            android:layout_width="wrap_content"
            android:layout_height="140dp"
            app:srcCompat="@mipmap/touxiang1" />

        <TextView
            android:id="@+id/tv_name"
            android:layout_marginStart="8dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="TextView" />


    </LinearLayout>
</androidx.cardview.widget.CardView>

        3.实体类

public class News {
    private int imgID;
    private String name;
    public News(int imgID,String name){
        this.name=name;
        this.imgID=imgID;
    }

    public int getImgID() {
        return imgID;
    }

    public String getName() {
        return name;
    }
}

        4.适配器

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
    List<News> list;
    public NewsAdapter(List<News> list){
        this.list=list;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item,parent,false);
        ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        News news=list.get(position);
        holder.iv_img.setImageResource(news.getImgID());
        holder.tv_name.setText(news.getName());
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_img;
        TextView tv_name;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_img=itemView.findViewById(R.id.iv_img);
            tv_name=itemView.findViewById(R.id.tv_name);
        }
    }
}

        5.在主活动中使用

public class MainActivity3 extends AppCompatActivity {

    public String[] names={"你","好","啊","你","好","啊"};
    public int[] imgs={R.mipmap.touxiang1,R.mipmap.touxiang2,
            R.mipmap.touxiang3,R.mipmap.touxiang1,R.mipmap.touxiang2,R.mipmap.touxiang3,};

    RecyclerView rv_news;
    List<News> list=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        rv_news=findViewById(R.id.rv_news);
        initData();
        NewsAdapter newsAdapter=new NewsAdapter(list);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        rv_news.setLayoutManager(layoutManager);
        rv_news.setAdapter(newsAdapter);
    }

    private void initData() {
        for (int i=0;i<names.length;i++){
            News news=new News(imgs[i],names[i]);
            list.add(news);
        }
    }
}

Fragment

        1.Fragment的简介

                Fragment

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 首先,在布局文件中添加一个RecyclerView: ```xml <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp" /> ``` 2. 在Fragment中,定义一个变量来保存RecyclerView的实例: ```java private RecyclerView recyclerView; ``` 3. 在Fragment的onCreateView方法中,初始化RecyclerView: ```java @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_product_list, container, false); recyclerView = view.findViewById(R.id.recyclerView); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); recyclerView.setAdapter(new ProductListAdapter(getProductList())); return view; } ``` 这里我们使用LinearLayoutManager来实现垂直方向的列表布局,并将适配器设置为ProductListAdapter,该适配器需要传入一个商品列表的数据源。 4. 创建一个ProductListAdapter类来实现RecyclerView的适配器: ```java public class ProductListAdapter extends RecyclerView.Adapter<ProductListAdapter.ViewHolder> { private List<Product> productList; public ProductListAdapter(List<Product> productList) { this.productList = productList; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_product, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { Product product = productList.get(position); holder.productName.setText(product.getName()); holder.productPrice.setText(product.getPrice()); holder.productImage.setImageResource(product.getImageResource()); } @Override public int getItemCount() { return productList.size(); } public static class ViewHolder extends RecyclerView.ViewHolder { private CardView cardView; private ImageView productImage; private TextView productName; private TextView productPrice; public ViewHolder(@NonNull View itemView) { super(itemView); cardView = itemView.findViewById(R.id.cardView); productImage = itemView.findViewById(R.id.productImage); productName = itemView.findViewById(R.id.productName); productPrice = itemView.findViewById(R.id.productPrice); } } } ``` 在ProductListAdapter中,我们需要实现三个方法: - onCreateViewHolder:创建ViewHolder实例,并将item_product布局文件作为其视图。 - onBindViewHolder:将数据绑定到ViewHolder实例上。在这里,我们将商品名称、价格和图像资源绑定到ViewHolder的各个控件上。 - getItemCount:返回商品列表的大小。 5. 在item_product布局文件中,添加一个CardView来显示商品信息: ```xml <androidx.cardview.widget.CardView android:id="@+id/cardView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" app:cardCornerRadius="8dp" app:cardElevation="4dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="8dp"> <ImageView android:id="@+id/productImage" android:layout_width="80dp" android:layout_height="80dp" android:layout_gravity="center_vertical" android:scaleType="centerCrop" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginStart="16dp" android:orientation="vertical"> <TextView android:id="@+id/productName" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="16sp" /> <TextView android:id="@+id/productPrice" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="14sp" /> </LinearLayout> </LinearLayout> </androidx.cardview.widget.CardView> ``` 在CardView中,我们使用了LinearLayout来实现水平方向的布局,并在其中添加了一个ImageView和两个TextView来显示商品图像、名称和价格。在CardView中,我们还设置了圆角半径和阴影效果。 6. 最后,定义一个Product类来保存商品信息: ```java public class Product { private String name; private String price; private int imageResource; public Product(String name, String price, int imageResource) { this.name = name; this.price = price; this.imageResource = imageResource; } public String getName() { return name; } public String getPrice() { return price; } public int getImageResource() { return imageResource; } } ``` 在这个类中,我们定义了商品名称、价格和图像资源三个属性,并提供了对应的get方法。 这样,一个使用CardView实现商品列表的Fragment就完成了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值