gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:28:56 27 4
gpt4 key购买 nike

我希望 recyclerview 仅显示一个数组,但在数组列表中包含更多数组。必须显示的内容并不总是相同,而是取决于某些百分比。

例如:第一个数组有 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);
}
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();
}
}

这就是适配器:



public class Adaptador extends RecyclerView.Adapter<Adaptador.ViewHolder> //implements View.OnClickListener
{
private List<Entidad> listItems;
private OnRecipeListener mOnRecipeListener;

public Adaptador(List<Entidad> listItems, OnRecipeListener onRecipeListener) {
this.listItems = listItems;
this.mOnRecipeListener = onRecipeListener;
}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.elemento_lista, parent, false);
//view.setOnClickListener(this);
return new ViewHolder(view, mOnRecipeListener);
}

@Override
public void onBindViewHolder(ViewHolder viewholder, int position) {
int resource = listItems.get(position).getImgFoto();
String title = listItems.get(position).getTitulo();
String time = listItems.get(position).getTiempo();
int barra = listItems.get(position).getRating();
int fondo = listItems.get(position).getColorfondo();
viewholder.setData(resource, title, time, barra, fondo);
}

@Override
public int getItemCount() {
return listItems.size();
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

private ImageView imgFoto;
private TextView titulo;
private TextView tiempo;
private RatingBar ratingBar;
private ImageView colorfondo;
OnRecipeListener onRecipeListener;

public ViewHolder(View itemView, OnRecipeListener onRecipeListener) {
super(itemView);

imgFoto = itemView.findViewById(R.id.imgFoto);
titulo = itemView.findViewById(R.id.tvTitulo);
tiempo = itemView.findViewById(R.id.tvTiempo);
ratingBar = itemView.findViewById(R.id.ratingBarVerd);
colorfondo = itemView.findViewById(R.id.colorfondo);
this.onRecipeListener = onRecipeListener;

itemView.setOnClickListener(this);
}

private void setData(int resource, String title, String time, int barra, int fondo){
imgFoto.setImageResource(resource);
titulo.setText(title);
tiempo.setText(time);
ratingBar.setRating(barra);
colorfondo.setImageResource(fondo);

}

@Override
public void onClick(View v) {
onRecipeListener.OnRecipe(getAdapterPosition());
}
}

public interface OnRecipeListener{
void OnRecipe(int position);
}
}

这是 Entidad 文件:


public class Entidad2 {

private int imgFoto;
private String titulo;
private String tiempo;
private int ratingBar;
private int colorfondo;

public Entidad2(int imgFoto, String titulo, String tiempo, int ratingBar, int colorfondo) {

this.imgFoto = imgFoto;
this.titulo = titulo;
this.tiempo = tiempo;
this.ratingBar = ratingBar;
this.colorfondo = colorfondo;
}

public int getImgFoto() {
return imgFoto;
}

public String getTitulo() {
return titulo;
}

public String getTiempo() {
return tiempo;
}

public int getRating() { return ratingBar; }

public int getColorfondo() {
return colorfondo;
}
}

如果有人知道如何做,请告诉我。如果您需要更多代码,请告诉我,

谢谢。

enter image description here

这里@Emir 看看它是否在里面

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

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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, R.drawable.color_carnes, 50));
entidad2 = new Entidad2(R.drawable.patatas_deluxe_especiadas_70523_300_150, "Entrecot", " 15 min.", 2, R.drawable.color_carnes, 200));
entidad3 = new Entidad2(R.drawable.tomate, "Hamburguesa", " 2 min.", 5, R.drawable.color_carnes, 350));


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

Adaptador2 Adaptador2 = new Adaptador2(listItems, this);
recyclerView1.setAdapter(Adaptador2);
Adaptador2.notifyDataSetChanged();
pickEntidad();
}
@Override
public void OnRecipe(int position) {

if (position == 0) {
Intent in = new Intent(this, Solomillo.class);
startActivity(in);
}
}

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

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

}
Adaptador2.notifyDataSetChanged();
}
}

最佳答案

我的理解是,您想要以不同的显示机会随机显示 Entidad 列表中的单个 Entidad 对象。

例如:在 Entidad 对象上创建一个名为 Priority 的整数字段。将第一个 Entidad 的优先级设置为 50,第二个 200,第三个 350。创建以下方法以根据优先级随机选择。(创建 Entidad 对象后,将其引用保存在主 Activity 中。)

编辑:Entidad.java

public 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) {
this.imgFoto = imgFoto;
this.titulo = titulo;
this.tiempo = tiempo;
this.ratingBar = ratingBar;
this.colorfondo = colorfondo;
this.priority = priority;
}

public int getRatingBar() {
return ratingBar;
}

public int getPriority() {
return priority;
}

public int getImgFoto() {
return imgFoto;
}

public String getTitulo() {
return titulo;
}

public String getTiempo() {
return tiempo;
}

public int getRating() { return ratingBar; }

public int getColorfondo() {
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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<>();
//Initialize entidad objects and keep references of them to use in pickEntidad() method

entidad1 = new Entidad(R.drawable.calabacines_3, "Solomillo a la plancha", " 10 min.", 4, R.drawable.color_carnes,50);
entidad2 = new Entidad(R.drawable.patatas_deluxe_especiadas_70523_300_150, "Entrecot", " 15 min.", 2, R.drawable.color_carnes,200);
entidad3 = new Entidad(R.drawable.tomate, "Hamburguesa", " 2 min.", 5, R.drawable.color_carnes,350);

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

adaptor = new Adaptador(listItems, this);
recyclerView1.setAdapter(adaptador);
adaptor.notifyDataSetChanged();
//Call pickEntidad() to randomly pick one entidad and show on the list
pickEntidad();
}

@Override
public void OnRecipe(int position) {

if (position == 0) {
Intent in = new Intent(this, Solomillo.class);
startActivity(in);
}
}
private void pickEntidad(){
//Generates random int value between 0-350
final 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 priority
if(random < priority1){

listItems.add(entidad1)

}else if(random < priority2){

listItems.add(entidad2)

}else if (random <= priority3){

listItems.add(entidad3)

}

//update your adapter

adaptor.notifyDataSetChanged()
}
}

每次您想要刷新列表时,请调用此 pickEntidad() 方法。不过,这是一种硬编码方法,如果您有太多项目并且不知道优先级值或范围,则该方法将毫无用处。您还可以通过生成 0-351 之间的整数来随机设置项目优先级。希望我正确理解你的问题。

另外,不要给变量名使用大写的后者。将“适配器”变量更改为“适配器”

关于java - 如何使 recyclerview 根据百分比仅显示一个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57356247/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com