java - 如何仅在按下按钮时才重新加载 recyclerview?

标签 java android android-recyclerview

我有一项 Activity ,其中包含一些回收者 View ,但只有一个是重要的。我希望这个 recyclerview 不要在我每次进入 Activity 时自动上传信息。我已经调用了用于刷新 pickEntidad 的方法,并且我想将此方法放在 if 中,在另一个方法中,如果按下此按钮,则将调用 pickEntidad 方法,否则什么也不会发生并且信息recyclerview 将显示的内容将是上次刷新时显示的内容。

我尝试将名为 cambiarmenu 的按钮放入 if 内,但这些按钮仅接受 boolean 值。这就是为什么我尝试在按下按钮时将变量值设置为 1,然后在 if 中使用该变量,但我未能正确执行此操作。 (我称这个方法为Clicado)

这里我给你留下java.file的代码。

public class Comida2 extends AppCompatActivity implements Adaptador2.OnRecipeListener {
    private RecyclerView recyclerView1;
    List<Entidad2> listItems;
    Adaptador2 adaptor;
    private Entidad2 entidad1,entidad2,entidad3;
    Button cambiarmenu;


    @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);

        cambiarmenu = (Button) findViewById(R.id.boton_cambiarmenu);

        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();
        //Clicado();
    }

    @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);
        }
        if (priority == 100) {
            Intent in = new Intent(this, Hamburguesa.class);
            startActivity(in);
        }
    }

    private void Clicado(){

        final boolean[] numerillo = new boolean[1];

        cambiarmenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numerillo[0] = true;
            }
        });

            if (numerillo[0]) {
            pickEntidad();
        }
    }

    private void pickEntidad(){
        final int random = new Random().nextInt(101);

        int priority1 = entidad1.getPriority();
        int priority2 = entidad2.getPriority();
        int priority3 = entidad3.getPriority();


        listItems.clear();
        if(random < priority1){

            listItems.add(entidad1);

        }else if(random < priority2){

            listItems.add(entidad2);

        }else if (random <= priority3){

            listItems.add(entidad3);

        }
        adaptor.notifyDataSetChanged();
    }
}

如果有人知道怎么做,请帮助我。如果您需要更多代码,请询问。

谢谢。

最佳答案

我不确定我是否正确理解了你的想法。

  1. recyclerview1 显示时开头没有任何项目。
  2. 当您点击布局中的 R.id.lv_1 按钮时,recyclerview 会通过 pickEntidad() 方法根据随机优先级填充 entidad1~3 中的随机项目.
  3. 如果您再次点击此按钮,回收器 View 将填充新挑选的随机项目,如上所示。
  4. 如果您点击列出的项目之一,您将根据您的选择转到另一项 Activity 。

只要我理解,这就是我的解决方案。

public class Comida2 extends AppCompatActivity implements Adaptador2.OnRecipeListener {
    private RecyclerView recyclerView1;
    List<Entidad2> listItems;
    Adaptador2 adaptor;
    private Entidad2 entidad1,entidad2,entidad3;
    Button cambiarmenu;


    @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);

        cambiarmenu = (Button) findViewById(R.id.boton_cambiarmenu);

        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);


        /* Do not fill listItems here yet. */

        // listItems.add(entidad1);
        // listItems.add(entidad2);
        // listItems.add(entidad3);

        adaptor = new Adaptador2(listItems, this);
        recyclerView1.setAdapter(adaptor);
        adaptor.notifyDataSetChanged();


        /* Clicado() method is useless in your mechanism */

        cambiarmenu.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            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);
        }
        if (priority == 100) {
            Intent in = new Intent(this, Hamburguesa.class);
            startActivity(in);
        }
    }

    private void pickEntidad(){
        final int random = new Random().nextInt(101);

        int priority1 = entidad1.getPriority();
        int priority2 = entidad2.getPriority();
        int priority3 = entidad3.getPriority();


        listItems.clear();
        if(random < priority1){

            listItems.add(entidad1);

        }else if(random < priority2){

            listItems.add(entidad2);

        }else if (random <= priority3){

            listItems.add(entidad3);

        }
        adaptor.notifyDataSetChanged();
    }
}

关于java - 如何仅在按下按钮时才重新加载 recyclerview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58207072/

相关文章:

android - 在 Recyclerview 中拖动项目时自动滚动滚动条

android - RecyclerView - 删除行总是删除最后一行

java - 线程与 CompletableFuture

java - 如何在 Kotlin 中填充 String[] 数组

安卓定时器停止运行

android - 如何在 Android Oreo 中运行隐式广播?

Android:滚动时 ListView xml 布局不显示背景图像

java - 如何按添加顺序绘制 X、Y 坐标?

java - Drools 6 - 如何解雇规则

Android 如何在单击 RecyclerView 项目时从 fragment 发送 Intent