如何查看按ID排序的RecyclerView项目?

时间:2018-06-15 07:29:52

标签: android android-recyclerview

在我的应用程序中,我有一个菜单,可以将SharedPreferences中存储的数据加载到构造函数并从中构建2 RecyclerView's

现在,如果我按下recyclerView底部的按钮,就像&#34; CICCIO &#34;在其中有构造函数项,如 ID由1设置我会在另一个recyclerView中,其中有很多彩色按钮可视化,只有按钮的ID也由1设置< / em>在他们的构造函数中,但我不熟悉android,我无法获得如何制作&#34; 排序方法&#34;像那样。

我还在onClick底部设置了RecyclerView方法,我还有来自底部 getID的方法recyclerView来自顶部 getID的构造函数和RecyclerView

ACTIVITY SCREENSHOT HERE

这是我活动的代码:

//  Builder del BOTTOM recyclerView
public void buildRecyclerViewMenu(){
    RecyclerView mRecyclerView = findViewById(R.id.recyclerViewMenu);
    mRecyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false);
    RecyclerViewMenu recyclerViewMenu = new RecyclerViewMenu(menuConstructors);

    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(recyclerViewMenu);

    recyclerViewMenu.setOnItemClickListener(new RecyclerViewMenu.OnItemClickListener() {
        @Override
        public void onItemClick(int position) {

        }
    });

}

// Builder del TOP recyclerView
public void buildRecyclerView(){
    mRecyclerViewBOT = findViewById(R.id.recyclerView);
    mRecyclerViewBOT.setHasFixedSize(true);
    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 4);
    Adapter mAdapter = new Adapter(items);
    mRecyclerViewBOT.setLayoutManager(mLayoutManager);
    mRecyclerViewBOT.setAdapter(mAdapter);

    mAdapter.setOnItemClickListener(new Adapter.OnItemClickListener() {
        @SuppressLint("SetTextI18n")
        @Override
        public void onItemClick(final int position) {

        }
    });

}

虽然这里是我将数据加载到Bottom和top RecyclerView

的方法
private void loadDataMenu(){
    new ArrayList<MenuConstructor>();
    SharedPreferences sharedPreferences = getSharedPreferences("MENU_SAVE", MODE_PRIVATE);
    Gson gson = new Gson();
    String json = sharedPreferences.getString("menu list",null);
    Type type = new TypeToken<ArrayList<MenuConstructor>>() {}.getType();
    menuConstructors = gson.fromJson(json, type );

    if(menuConstructors == null){

        menuConstructors = new ArrayList<>();
        Toast.makeText(cassa.this,"NESSUN TASTO DA VISUALIZZARE" ,Toast.LENGTH_SHORT).show();


    }

}

private void loadDataTasti(){
    new ArrayList<Item>();
    SharedPreferences sharedPreferences = getSharedPreferences("TASTI_SAVE", MODE_PRIVATE);
    Gson gson = new Gson();
    String json = sharedPreferences.getString("tasti list",null);
    Type type = new TypeToken<ArrayList<Item>>() {}.getType();
    items = gson.fromJson(json, type );

    if(items == null){

        items = new ArrayList<>();
        Toast.makeText(cassa.this,"NESSUN TASTO DA VISUALIZZARE" ,Toast.LENGTH_SHORT).show();


    }

}

虽然这是我的Item.java,它是TOP recycleler View的模型类:

公共类项目{

private int menu;

private String tipo;

private String codice;

private String deskT;

private String deskS;

private String sfondo;

private String font;

private String qta;

private double pre;



Item(int id, String typo, String codice_t, String desk_T,String desk_S,String sfondo_c,String font_c,String quant,double prezzo){
    menu = id;
    tipo = typo;
    codice = codice_t;
    deskT = desk_T;
    deskS = desk_S;
    sfondo = sfondo_c;
    font = font_c;
    qta = quant;
    pre = prezzo;
}

public int getMenu(){return menu;}

public String getTipo(){return tipo;}

public String getCodice(){return codice;}

public String getDeskT(){return deskT;}

public String getDeskS(){return deskS;}

public String getSfondo(){return sfondo;}

public String getFont(){return font;}

public String getQuant(){return qta;}

public double getPrice(){return pre;}

}

这是MenuConstructor的模型(底部的recyclerView)

public class MenuConstructor {

    int id;

    private String deskT;

    private String sfondo;

    private String font;

            MenuConstructor(int idID,String Desk,String Sfondo,String Font){
                id = idID;
                deskT = Desk;
                sfondo = Sfondo;
                font = Font;
    }


    public int getBtnID(){
        return id;
    }

    public String getDesk(){
        return deskT;
    }

    public String getSfondoColor(){
        return sfondo;
    }

    public String getFontColor(){
        return font;
    }

}

所以我想将MenuConstructor中的id与Item.java中的menu(id)进行比较,并显示具有特定ID的项目。

2 个答案:

答案 0 :(得分:1)

您可以使用Collections.sort()

private List<Item> getSortedItems(List<Item> items) {
    Collections.sort(items, new Comparator<Item>() {
        @Override
        public int compare(Item item, Item nextItem) {
            return ComparisonChain.start()
                    .compare(item.getMenu(), nextItem.getMenu())
                    .result();
        }
    });
    return items;
}

此方法将按item list

返回menu排序

希望这会有所帮助

答案 1 :(得分:0)

通过创建一个新的ArrayList并在一个for循环中比较item.getMenu()和menuConstructors.getBtnID()来解决这个问题,该循环滚动相同的ArrayList长度。

  recyclerViewMenu.setOnItemClickListener(new RecyclerViewMenu.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {

                Toast.makeText(cassa.this,String.valueOf(menuConstructors.get(position).getBtnID()),Toast.LENGTH_SHORT).show();

                ArrayList<Item> filteredList = new ArrayList<>();

                for(Item item : items) {

                    if(item.getMenu() == menuConstructors.get(position).getBtnID() ) {
                        filteredList.add(item);
                    }
                }

                mAdapter.filterList(filteredList);

            }
        });