[英]How to make a recyclerview reload only if a button is pressed?
我有一些 recyclerviews 的活動,但只有一個很重要。 我希望這個 recyclerview 不會在我每次進入活動時自動上傳信息。 我已經調用了用來讓它刷新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();
}
}
如果有人知道該怎么做,請幫助我。 如果您需要更多代碼,請詢問。
謝謝。
我不確定我是否正確理解了您的想法。
recyclerview1
在開始時顯示沒有任何項目。R.id.lv_1
按鈕時,recyclerview 將通過pickEntidad()
方法按照隨機優先級填充 entidad1~3 中的隨機項。只要我理解,這就是我的解決方案。
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();
}
}
您可以通過檢查適配器中的項目計數來檢查您的 recyclerview 是否有項目。 如果它的 null 或空添加項目到您的回收站視圖。 否則,什么也不做。 你的刷新按鈕應該有 pickEntidad() 方法來刷新;
//if list is empty add items
if (youradapter.itemCount == null || youradapter.itemCount == 0){
pickEntidad ()
}
yourRefreshButton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
pickEntidad()
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.