У меня проблема с представлением моего ресайклера, он показывает все элементы два раза. У Activity есть два вида ресайклера, оба с разными адаптерами, но оба являются дублирующими элементами. Но когда я обновляю sh свое представление ресайклера с помощью 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, он отображается правильно
вот изображение