如何根据特定百分比在 Recyclerview 中显示来自不同 ArrayLists 的内容?

How to show content from different ArrayLists in Recyclerview based on specific percentage?

我有这个简单的 recyclerview,它显示 ArrayList 中的内容。我想根据百分比显示来自多个 ArrayLists 的内容。简单来说:我想在我的代码中放入 2 个不同内容的 ArrayList。我希望 Recyclerview 显示来自两个 arrayLists 的内容,但不是全部。我想为两个 ArrayLists 设置特定的百分比。比如说,如果我想显示 ArrayList1 中的 30% 内容(有 10 个项目)和 ArrayList2 中的 70% 内容(有 10 个项目),那么我的 recyclerview 应该显示 ArrayList1 中的 3 个项目和 ArrayList2 中的 7 个项目。 (最好是随机顺序)。

MainActivity.java:

    private void initView() {
    // Initialize RecyclerView and set Adapter
    recyclerView = findViewById(R.id.scout_recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    scoutArrayList = new ArrayList<>();
    adapter = new ScoutAdapter(this,scoutArrayList);
    recyclerView.setAdapter(adapter);
    createList();
}

private void createList() {
    //data to be shown in list
    scoutArrayList.add(new Scout( "Eren Jaeger" ,"Unranked" , "22" ));
    scoutArrayList.add(new Scout ( "Mikasa Ackermann" ,"Unranked" , "12" ) );
    scoutArrayList.add(new Scout ( "Armin Arlelt" ,"Unranked" , "0" ) );
    scoutArrayList.add(new Scout ( "Erwin Smith " , "Commander" , "Unknown" ) );
    scoutArrayList.add(new Scout ( "Levi Ackermann" , "Squad Captain" ,"~58" ) );
    scoutArrayList.add(new Scout ( "Hange Zoe" , "Commander" , "Unknown") );
    scoutArrayList.add(new Scout ( "Jean Kirschtein" , "Unranked" , "1" )   );
    scoutArrayList.add(new Scout ("Conny Springer", "Unranked" , "Unknown") );
    scoutArrayList.add(new Scout ("Sasha Braus","Unranked","1") );

}

private void createList2() {
    //data to be shown in list
    scoutArrayList2.add(new Scout( "Testname2" ,"rankvalue2" , "killscount2" ));
    scoutArrayList2.add(new Scout( "Testname2" ,"rankvalue2" , "killscount2" ));
    scoutArrayList2.add(new Scout( "Testname2" ,"rankvalue2" , "killscount2" ));
    scoutArrayList2.add(new Scout( "Testname2" ,"rankvalue2" , "killscount2" ));
    scoutArrayList2.add(new Scout( "Testname2" ,"rankvalue2" , "killscount2" ));
    scoutArrayList2.add(new Scout( "Testname2" ,"rankvalue2" , "killscount2" ));
    scoutArrayList2.add(new Scout( "Testname2" ,"rankvalue2" , "killscount2" ));
    scoutArrayList2.add(new Scout( "Testname2" ,"rankvalue2" , "killscount2" ));
    scoutArrayList2.add(new Scout( "Testname2" ,"rankvalue2" , "killscount2" ));
}

Adapter.java

   public ScoutAdapter(Context context, ArrayList<Scout> scouts) {
    this.context = context;
    this.scouts = scouts;

}

@NonNull
@Override
public ScoutHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.item_row,parent,false);
    return new ScoutHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ScoutHolder holder, int position) {
    Scout scout = scouts.get(position);
    holder.setDetails(scout);
}

@Override
public int getItemCount() {
    return scouts.size();
}

public class ScoutHolder extends RecyclerView.ViewHolder {
    private TextView txtName, txtRank, txtKill;
    public ScoutHolder(@NonNull View itemView) {
        super(itemView);
        txtName = itemView.findViewById(R.id.txtName);
        txtRank = itemView.findViewById(R.id.txtRank);
        txtKill = itemView.findViewById(R.id.txtKill);
    }

    public void setDetails(Scout scout) {
        txtName.setText(scout.getName());
        txtRank.setText(String.format("Rank: %s", scout.getRank()));
        txtKill.setText(String.format("kill: %s", scout.getKillCount()));
    }
}

在上面的代码(MainActivity.java)中有一个名为createList2()的列表,我想显示其中20%的内容......

有什么解决办法吗?

注意输入百分比时元素个数的计算方式:

public static <T> List<T> getRandomValue (List<T> list, int percent){
        List<T> res = new ArrayList<>();
        Collections.shuffle(list);
        for (int i = 0; i < getIteration(percent, list.size()); i ++){
            res.add(list.get(i));
        }
        return res;
    }

public static int getIteration(int percent, int size){ // for example 97% will be 90%
        float res = (float) percent / 100 * size;
        return (int) res;
    }
public static <T> List<T> getViewList (List<T> firstList, int firstPercent, List<T> secondList, int secondPercent){
        List<T> first = getRandomValue(firstList, firstPercent);
        List<T> second = getRandomValue(secondList, secondPercent);
        first.addAll(second);
        return first;
    }

请告诉我这是否适合你