代码之家  ›  专栏  ›  技术社区  ›  woshitom

RecyclerView中的异步RecyclerView

  •  0
  • woshitom  · 技术社区  · 6 年前

    我的产品存储在ElasticSearch数据库中。所有这些产品都有1级分类(例如运动)和2级分类(例如足球)。

    我想将所有类别级别2都按它们各自的类别级别1进行存储,以获得此类输出:

    Category 1
    Category 1_1
    category 1_2
    
    Category 2
    category 2_1
    Category 2_2
    
    ...
    
    Category n
    Category n_1
    Category n_2
    

    为此,我首先运行一个查询,以获取存储在json数组categorylevelone中的所有不同类别级别1的列表,使用这些数据,我正在填充一个名为categorylevelonerecyclerviewapter的回收器视图:

    RecyclerView CategoryLevelOneRecyclerViewAdapter = view.findViewById(R.id.rvCategoryLevelOne);
    CategoryLevelOneRecyclerViewAdapter.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
    adapterCategoryLevelOne = new CategoryLevelOneRecyclerViewAdapter(getApplicationContext(), categoryLevelOne, mIdShop);
    CategoryLevelOneRecyclerViewAdapter.setAdapter(adapterCategoryLevelOne);
    

    在这些类别级别1的onBindViewHolder上,我正在运行一个新查询,以获取属于该类别级别1的所有类别级别2,并填充一个名为categoryleveltworyclerviewapter的新recyclerview:

    @Override
        public void onBindViewHolder(CategoryLevelOneRecyclerViewAdapter.ViewHolder holder, int position) {
            final ViewHolder holderTemp = holder;
            String currentCategoryLevelOne = null;
            try {
    
                currentCategoryLevelOne = mCategoryLevelOne.getString(position);
    
                String query = "some elasticsearch query to get the corresponding categories level 2";
                String endPoint = "some elasticsearch endpoing";
    
                final String finalCurrentCategoryLevelOne = currentCategoryLevelOne;
                NetworkManager.getInstance().GetElasticSearchData(query, endPoint, new VolleyListener<String>()
                {
                    @Override
                    public void getResult(String result)
                    {
                        if (!result.isEmpty()) {
                            try {
                                JSONArray JSONresult = new JSONArray(result);
    
                                holderTemp.textViewCategoryName.setText(finalCurrentCategoryLevelOne);
                                RecyclerView.LayoutManager gridCategory = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
                                CategoryLevelTwoRecyclerViewAdapter.setLayoutManager(gridCategory);
                                CategoryLevelTwoRecyclerViewAdapter adapter = new CategoryLevelTwoRecyclerViewAdapter(getApplicationContext(), JSONresult);
    
                                CategoryLevelTwoRecyclerViewAdapter.setAdapter(adapter);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
    
                        }
                    }
                });
    
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    

    我想,因为对elasticsearch的调用是异步的,所以只有最后一个类别级别1需要由某些类别级别2(甚至不属于它)填充。

    enter image description here

    当我向下滚动时,它们都被填充,但顺序不受尊重(例如,应该属于类别1的类别2最终会出现在类别1中)

    enter image description here

    0 回复  |  直到 6 年前