回收者视图显示重复的项目

时间:2020-06-20 11:18:20

标签: android android-recyclerview

我的回收器视图有问题,它两次显示所有物品。 该活动有两个回收者视图,每个视图都有不同的适配器,但是两者都是重复项。 但是,当我使用onRefreshListener刷新回收者视图时,它可以正确显示。

片段类

public class MyMallFragment extends Fragment {

public static SwipeRefreshLayout swipeRefreshLayout;
private RecyclerView categoryRecyclerView, homepageRecyclerview;
private CategoryAdapter categoryAdapter;
private MyMallAdapter myMallAdapter;
private ImageView noInternet;
private Button retryBtn;

private List<CategoryModel> categoryModelFakeList = new ArrayList<>();
private List<MyMallModel> myMallModelFakeList = new ArrayList<>();


NetworkInfo networkInfo;
ConnectivityManager connectivityManager;

public MyMallFragment() {

}

public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_my_mall, container, false);
    swipeRefreshLayout = view.findViewById(R.id.refresh_layout);
    noInternet = view.findViewById(R.id.no_internet);
    retryBtn = view.findViewById(R.id.retry_btn);
    homepageRecyclerview = view.findViewById(R.id.my_mall_recyclerview);
    categoryRecyclerView = view.findViewById(R.id.category_recycler_view);
    swipeRefreshLayout.setColorSchemeColors(getContext().getResources().getColor(R.color.colorPrimary), getContext().getResources().getColor(R.color.colorPrimary), getContext().getResources().getColor(R.color.colorPrimary));


    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
    linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    categoryRecyclerView.setLayoutManager(linearLayoutManager);

    LinearLayoutManager testingLayoutManager = new LinearLayoutManager(getContext());
    testingLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    homepageRecyclerview.setLayoutManager(testingLayoutManager);


     //////////category fake list
    categoryModelFakeList.add(new CategoryModel("null", ""));
    categoryModelFakeList.add(new CategoryModel("", ""));
    categoryModelFakeList.add(new CategoryModel("", ""));
    categoryModelFakeList.add(new CategoryModel("", ""));
    categoryModelFakeList.add(new CategoryModel("", ""));
    categoryModelFakeList.add(new CategoryModel("", ""));
    categoryModelFakeList.add(new CategoryModel("", ""));
    categoryModelFakeList.add(new CategoryModel("", ""));
    categoryModelFakeList.add(new CategoryModel("", ""));
    categoryModelFakeList.add(new CategoryModel("", ""));
    categoryModelFakeList.add(new CategoryModel("", ""));

  //////////category fake list


  //////////home page fake list

    List<SliderModel> sliderModelFakeList = new ArrayList<>();

    sliderModelFakeList.add(new SliderModel("null", "#dfdfdf"));
    sliderModelFakeList.add(new SliderModel("null", "#dfdfdf"));
    sliderModelFakeList.add(new SliderModel("null", "#dfdfdf"));
    sliderModelFakeList.add(new SliderModel("null", "#dfdfdf"));
    sliderModelFakeList.add(new SliderModel("null", "#dfdfdf"));

    List<HorizontalProductScrollModel> horizontalProductScrollModelFakeList = new ArrayList<>();
    horizontalProductScrollModelFakeList.add(new HorizontalProductScrollModel("", "", "", "", ""));
    horizontalProductScrollModelFakeList.add(new HorizontalProductScrollModel("", "", "", "", ""));
    horizontalProductScrollModelFakeList.add(new HorizontalProductScrollModel("", "", "", "", ""));
    horizontalProductScrollModelFakeList.add(new HorizontalProductScrollModel("", "", "", "", ""));
    horizontalProductScrollModelFakeList.add(new HorizontalProductScrollModel("", "", "", "", ""));
    horizontalProductScrollModelFakeList.add(new HorizontalProductScrollModel("", "", "", "", ""));
    horizontalProductScrollModelFakeList.add(new HorizontalProductScrollModel("", "", "", "", ""));

    myMallModelFakeList.add(new MyMallModel(0, sliderModelFakeList));
    myMallModelFakeList.add(new MyMallModel(1, "", "#dfdfdf"));
     myMallModelFakeList.add(new MyMallModel(2, "", "#dfdfdf", 
  horizontalProductScrollModelFakeList, new ArrayList<WishlistModel>()));
    myMallModelFakeList.add(new MyMallModel(3, "", "#dfdfdf", 
     horizontalProductScrollModelFakeList));

    //home page fake list

   connectivityManager = (ConnectivityManager) 
   getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
    networkInfo = connectivityManager.getActiveNetworkInfo();

    
    swipeRefreshLayout.setRefreshing(true);
    reloadPage();

    ///refresh layout
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            swipeRefreshLayout.setRefreshing(true);
            reloadPage();
        }
    });

    //refresh layout

    retryBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            reloadPage();
        }
    });


    return view;
}

private void reloadPage() {
    networkInfo = connectivityManager.getActiveNetworkInfo();
    //categoryModelList.clear();
    //lists.clear();
    //loadedCategoriesNames.clear();
    DBquerries.clearData();
    if (networkInfo != null && networkInfo.isConnected() == true) {
        MainActivity.drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
        noInternet.setVisibility(View.GONE);
        retryBtn.setVisibility(View.GONE);
        categoryRecyclerView.setVisibility(View.VISIBLE);
        homepageRecyclerview.setVisibility(View.VISIBLE);
        categoryAdapter = new CategoryAdapter(categoryModelFakeList);
        myMallAdapter = new MyMallAdapter(myMallModelFakeList);
        categoryRecyclerView.setAdapter(categoryAdapter);
        homepageRecyclerview.setAdapter(myMallAdapter);


        loadCategories(categoryRecyclerView, getContext());
        loadedCategoriesNames.add("HOME");
        lists.add(new ArrayList<MyMallModel>());
        loadFragmentData(homepageRecyclerview, getContext(), 0, "Home");
    } else {
        MainActivity.drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        Toast.makeText(getContext(), "No internet Connction!", Toast.LENGTH_SHORT).show();
        categoryRecyclerView.setVisibility(View.GONE);
        homepageRecyclerview.setVisibility(View.GONE);
        Glide.with(getContext()).load(R.drawable.no_internet).into(noInternet);
        noInternet.setVisibility(View.VISIBLE);
        retryBtn.setVisibility(View.VISIBLE);
        swipeRefreshLayout.setRefreshing(false);
    }
  }

 }

适配器类

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {

    private List <CategoryModel> categoryModelList;
    private int lastPos=-1;

    public CategoryAdapter(List<CategoryModel> categoryModelList) {
        this.categoryModelList = categoryModelList;
    }


    @NonNull
    @Override
    public CategoryAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.category_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CategoryAdapter.ViewHolder holder, int position) {
        String icon=categoryModelList.get(position).getCategoryIconLink();
        String name=categoryModelList.get(position).getCategoryName();
        holder.setCategory(name,position);
        holder.setCategoryIcon(icon);

        if(lastPos<position) {
            Animation animation = AnimationUtils.loadAnimation(holder.itemView.getContext(), R.anim.fade_in);
            holder.itemView.setAnimation(animation);
            lastPos=position;
        }
    }

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

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void setHasStableIds(boolean hasStableIds) {
        super.setHasStableIds(hasStableIds);
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        private ImageView categoryIcon;
        private TextView categoryName;

        public ViewHolder(@NonNull final View itemView) {
            super(itemView);

            categoryIcon=itemView.findViewById(R.id.category_icon);
            categoryName=itemView.findViewById(R.id.category_name);
        }

        private void setCategoryIcon(String iconUrl){
            if(!iconUrl.equals("null")) {
                Glide.with(itemView.getContext()).load(iconUrl).apply(new RequestOptions().placeholder(R.mipmap.pic)).into(categoryIcon);
            }else {
                categoryIcon.setImageResource(R.mipmap.homeicon);
            }
        }

        private void setCategory(final String name,final int position){
            categoryName.setText(name);

            if(!name.equals("")) {
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (position != 0) {
                            Intent categoryIntent = new Intent(itemView.getContext(), CategoryActivity.class);
                            categoryIntent.putExtra("CategoryName", name);
                            itemView.getContext().startActivity(categoryIntent);
                        }
                    }
                });
            }
        }
    }
}

我使用的是onRefreshListener中使用的相同方法,但是当我的片段开始并调用相同的方法时,它会显示重复项,但是当我在onRefreshListener上使用相同的方法时,它将正确显示

here is the image

0 个答案:

没有答案