如何使 recyclerview 根据百分比只显示一个数组?

我想要一个 recyclerview 只显示一个数组,但在 arraylist 中有更多。必须显示的那个不能总是相同的,但这将取决于某些百分比。


例如:第一个数组有 20% 的机会,第二个 50% 和第三个 50% 也有。并随机显示一个。但是当我刷新屏幕时它会改变。


我想我应该做的是为数组列表中的每个数组设置百分比,但我不知道该怎么做以及下一步该做什么。


那是我的java文件。



public class Comida extends AppCompatActivity implements Adaptador2.OnRecipeListener {

    private RecyclerView recyclerView1;

    List<Entidad2> listItems;

    Adaptador2 adaptor;

    private Entidad2 entidad1,entidad2,entidad3;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_comida);


        recyclerView1 = findViewById(R.id.lv_1);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);

        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);


        recyclerView1.setLayoutManager(layoutManager);


        listItems = new ArrayList<>();

        entidad1 = new Entidad2(R.drawable.calabacines_3, "Solomillo a la plancha", " 10 min.", 4, 20);

        entidad2 = new Entidad2(R.drawable.patatas_deluxe_especiadas_70523_300_150, "Entrecot", " 15 min.", 2, 50);

        entidad3 = new Entidad2(R.drawable.tomate, "Hamburguesa", " 2 min.", 5, 100);



        listItems.add(entidad1);

        listItems.add(entidad2);

        listItems.add(entidad3);


        adaptor = new Adaptador2(listItems, this);

        recyclerView1.setAdapter(adaptor);

        adaptor.notifyDataSetChanged();

        pickEntidad();

    }

    @Override

    public void OnRecipe(int priority) {


        if (priority == 20) {

            Intent in = new Intent(this, Solomillo.class);

            startActivity(in);

        }

        if (priority == 50) {

            Intent in = new Intent(this, Entrecot.class);

            startActivity(in);

        }

    }

}


小唯快跑啊
浏览 131回答 1
1回答

三国纷争

我的理解是,您想以不同的显示机会随机显示 Entidad 列表中的单个 Entidad 对象。例如:在您的 Entidad 对象上创建一个名为 Priority 的整数字段。将第一个 Entidad 的优先级设置为 50,第二个 200,第三个 350。创建以下方法以根据优先级随机选择。(在创建它们后将 Entidad 对象的引用保存在主活动中。)编辑:Entidad.javapublic class Entidad {private int imgFoto;private String titulo;private String tiempo;private int ratingBar;private int colorfondo;private int priority;public Entidad(int imgFoto, String titulo, String tiempo, int ratingBar, int colorfondo, int priority) {&nbsp; &nbsp; this.imgFoto = imgFoto;&nbsp; &nbsp; this.titulo = titulo;&nbsp; &nbsp; this.tiempo = tiempo;&nbsp; &nbsp; this.ratingBar = ratingBar;&nbsp; &nbsp; this.colorfondo = colorfondo;&nbsp; &nbsp; this.priority = priority;}public int getRatingBar() {&nbsp; &nbsp; return ratingBar;}public int getPriority() {&nbsp; &nbsp; return priority;}public int getImgFoto() {&nbsp; &nbsp; return imgFoto;}public String getTitulo() {&nbsp; &nbsp; return titulo;}public String getTiempo() {&nbsp; &nbsp; return tiempo;}public int getRating() { return ratingBar; }public int getColorfondo() {&nbsp; &nbsp; return colorfondo;}}编辑 2:使用 getEntidad()public class Comida extends AppCompatActivity implements Adaptador.OnRecipeListener {private RecyclerView recyclerView1;List<Entidad> listItems;Adaptador adaptor;private Entidad entidad1,entidad2,entidad3;@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_comida);&nbsp; &nbsp; recyclerView1 = findViewById(R.id.lv_1);&nbsp; &nbsp; LinearLayoutManager layoutManager = new&nbsp;&nbsp; &nbsp; LinearLayoutManager(this);&nbsp; &nbsp; layoutManager.setOrientation(LinearLayoutManager.VERTICAL);&nbsp; &nbsp; recyclerView1.setLayoutManager(layoutManager);&nbsp; &nbsp; listItems = new ArrayList<>();//Initialize entidad objects and keep references of them to use in pickEntidad() method&nbsp; &nbsp;entidad1 = new Entidad(R.drawable.calabacines_3, "Solomillo a la plancha", " 10 min.", 4, R.drawable.color_carnes,50);&nbsp; &nbsp;entidad2 = new Entidad(R.drawable.patatas_deluxe_especiadas_70523_300_150, "Entrecot", " 15 min.", 2, R.drawable.color_carnes,200);&nbsp; &nbsp; entidad3 = new Entidad(R.drawable.tomate, "Hamburguesa", " 2 min.", 5, R.drawable.color_carnes,350);&nbsp; &nbsp; listItems.add(entidad1);&nbsp; &nbsp; listItems.add(entidad2);&nbsp;&nbsp; &nbsp; listItems.add(entidad3);&nbsp; &nbsp; adaptor = new Adaptador(listItems, this);&nbsp; &nbsp; recyclerView1.setAdapter(adaptador);&nbsp; &nbsp; adaptor.notifyDataSetChanged();//Call pickEntidad() to randomly pick one entidad and show on the list&nbsp; &nbsp; pickEntidad();}@Overridepublic void OnRecipe(int position) {&nbsp; &nbsp; if (position == 0) {&nbsp; &nbsp; &nbsp; &nbsp; Intent in = new Intent(this, Solomillo.class);&nbsp; &nbsp; &nbsp; &nbsp; startActivity(in);&nbsp; &nbsp; }}private void pickEntidad(){//Generates random int value between 0-350final int random = new Random().nextInt(351);//get priorities from your created Entidad objects.int priority1 = entidad1.getPriority();int priority2 = entidad2.getPriority();int priority3 = entidad3.getPriority();listItems.clear()//Pick one randomly depending on priorityif(random < priority1){listItems.add(entidad1)}else if(random < priority2){listItems.add(entidad2)}else if (random <= priority3){listItems.add(entidad3)}//update your adapteradaptor.notifyDataSetChanged()}}每次您想刷新列表时,调用此 pickEntidad() 方法。不过,这是一种硬编码方法,如果您有太多项目并且不知道优先级值或范围,它将毫无用处。您还可以通过生成 0-351 之间的整数来随机设置项目优先级。希望我能正确理解你的问题。另外,不要给变量名加上大写字母。将您的“适配器”变量更改为“适配器”
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java