美文网首页
在Android中使用Recycler View的步骤

在Android中使用Recycler View的步骤

作者: 蜗牛是不是牛 | 来源:发表于2022-10-23 21:57 被阅读0次

    步骤1:制作一个具有空活动的Android项目。
    步骤2:将重新拾取器视图放入项目中。
    步骤3: 转到布局>将新布局设置为“sample_for_recylerview”。
    步骤 4:在此布局中完整设计示例。
    步骤5:转到Java fils制作两个新包,一个用于“模型”,第二个用于“适配器”。
    步骤 6:在模型包中为模型创建 Java 类。

    下面是模型类的示例:

    package com.example.myfoodapp.mymodels;
    
    public class MainModel {
        int image;
        String priduct_Name;
        String price;
        String discription;
    
        public MainModel(int image, String priduct_Name, String price, String discription) {
            this.image = image;
            this.priduct_Name = priduct_Name;
            this.price = price;
            this.discription = discription;
        }
    
        public int getImage() {
            return image;
        }
    
        public void setImage(int image) {
            this.image = image;
        }
    
        public String getPriduct_Name() {
            return priduct_Name;
        }
    
        public void setPriduct_Name(String priduct_Name) {
            this.priduct_Name = priduct_Name;
        }
    
        public String getPrice() {
            return price;
        }
    
        public void setPrice(String price) {
            this.price = price;
        }
    
        public String getDiscription() {
            return discription;
        }
    
        public void setDiscription(String discription) {
            this.discription = discription;
        }
    
    }
    

    步骤 7:在适配器包中创建一个适配器类和一个适配器类,该类将由 RecylerView.ViewHolder 扩展,并使用它来绑定后端 java 中的前端和视图。

    下面是适配器类的示例代码:

    package com.example.myfoodapp.myadapters;
    
    import android.content.Context;
    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 com.example.myfoodapp.R;
    import com.example.myfoodapp.mymodels.MainModel;
    
    import java.util.ArrayList;
    
    public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
    
        ArrayList<MainModel> myList;
        Context context;
    
        public MainAdapter(ArrayList<MainModel> myList, Context context) {
            this.myList = myList;
            this.context = context;
        }
    
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.sample_mainfoodpage,parent,false);
    
            return new ViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
                MainModel model = myList.get(position);
                holder.myImage.setImageResource(model.getImage());
                holder.myName.setText(model.getPriduct_Name());
                holder.myPrice.setText(model.getPrice());
                holder.MyDes.setText(model.getDiscription());
        }
    
        @Override
        public int getItemCount() {
            return myList.size();
        }
    
        //View holding class. . .
        public class ViewHolder extends RecyclerView.ViewHolder{
    
            ImageView myImage;
            TextView myName, myPrice, MyDes;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                myImage = itemView.findViewById(R.id.my_imageview);
                myName = itemView.findViewById(R.id.lbl_itemname);
                myPrice = itemView.findViewById(R.id.lbl_price);
                MyDes = itemView.findViewById(R.id.lbl_discription);
    
            }
        }
    }
    

    步骤8:制作适配器类后,让我们转到文件中的主活动java文件和程序,如下面的示例所示:

    package com.example.myfoodapp;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.recyclerview.widget.LinearLayoutManager;
    import androidx.recyclerview.widget.RecyclerView;
    
    import android.os.Bundle;
    import android.widget.LinearLayout;
    
    import com.example.myfoodapp.myadapters.MainAdapter;
    import com.example.myfoodapp.mymodels.MainModel;
    
    import java.util.ArrayList;
    
    public class MainActivity extends AppCompatActivity {
    
        RecyclerView recyclerView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            recyclerView = findViewById(R.id.myRecylerview);
    
            ArrayList<MainModel> list = new ArrayList<>();
            list.add(new MainModel(R.drawable.food1,"Burger","$2.5","Zinger burger with exta toping of chese"));
            list.add(new MainModel(R.drawable.food2,"Pizz","$3.5","Pizza with exta toping of chese"));
    
            MainAdapter adapter = new MainAdapter(list, this);
            recyclerView.setAdapter(adapter);
    
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            recyclerView.setLayoutManager(linearLayoutManager);
        }
    }
    

    相关文章

      网友评论

          本文标题:在Android中使用Recycler View的步骤

          本文链接:https://www.haomeiwen.com/subject/ozzgzrtx.html