java - 回收站视图不是第一次加载,但在我刷新页面时它会立即加载
问题描述
片段代码
public class MyMallFragment extends Fragment {
public MyMallFragment() {
}
public static SwipeRefreshLayout swipeRefreshLayout;
private RecyclerView homepageRecyclerview;
private MyMallAdapter myMallAdapter;
private ImageView noInternet;
private Button retryBtn;
private List<MyMallModel> myMallModelFakeList = new ArrayList<>();
NetworkInfo networkInfo;
ConnectivityManager connectivityManager;
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);
swipeRefreshLayout.setColorSchemeColors(getContext().getResources().getColor(R.color.colorPrimary), getContext().getResources().getColor(R.color.colorPrimary), getContext().getResources().getColor(R.color.colorPrimary));
LinearLayoutManager testingLayoutManager = new LinearLayoutManager(getContext());
testingLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
homepageRecyclerview.setLayoutManager(testingLayoutManager);
////////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
myMallAdapter = new MyMallAdapter(myMallModelFakeList);
connectivityManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected() == true) {
MainActivity.drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
noInternet.setVisibility(View.GONE);
retryBtn.setVisibility(View.GONE);
homepageRecyclerview.setVisibility(View.VISIBLE);
if (lists.size() == 0) {
lists.add(new ArrayList<MyMallModel>());
myMallAdapter = new MyMallAdapter(lists.get(0));
loadFragmentData(homepageRecyclerview, getContext(), 0, "Home");
} else {
myMallAdapter = new MyMallAdapter(lists.get(0));
myMallAdapter.notifyDataSetChanged();
}
homepageRecyclerview.setAdapter(myMallAdapter);
} else {
MainActivity.drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
homepageRecyclerview.setVisibility(View.GONE);
Glide.with(this).load(R.drawable.no_internet).into(noInternet);
noInternet.setVisibility(View.VISIBLE);
retryBtn.setVisibility(View.VISIBLE);
}
///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);
homepageRecyclerview.setVisibility(View.VISIBLE);
myMallAdapter = new MyMallAdapter(myMallModelFakeList);
homepageRecyclerview.setAdapter(myMallAdapter);
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.INVISIBLE);
Glide.with(getContext()).load(R.drawable.no_internet).into(noInternet);
noInternet.setVisibility(View.VISIBLE);
retryBtn.setVisibility(View.VISIBLE);
swipeRefreshLayout.setRefreshing(false);
}
}
@Override
public void onStart() {
super.onStart();
}
我尝试在启动方法中添加和设置适配器,就像它在打开时加载内容一样工作,但是当我在回收器视图开始重复内容后向下滚动时,内容加载了 2 次。文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/category_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:elevation="3dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.454"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="gone" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/refresh_layout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/category_recycler_view">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/my_mall_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<ImageView
android:id="@+id/no_internet"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_gravity="center"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/retry_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Retry"
android:textSize="16sp"
android:textAllCaps="false"
android:backgroundTint="@color/black"
android:stateListAnimator="@null"
android:textColor="@color/colorAccent"
android:translationZ="8dp"
android:background="@drawable/slider_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/category_recycler_view"
app:layout_constraintVertical_bias="0.75" />
</androidx.constraintlayout.widget.ConstraintLayout>
适配器
public class MyMallAdapter extends RecyclerView.Adapter {
private List<MyMallModel> myMallModelList;
private RecyclerView.RecycledViewPool recycledViewPool;
private FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
private int lastPos=-1;
public MyMallAdapter(List<MyMallModel> myMallModelList) {
this.myMallModelList = myMallModelList;
recycledViewPool=new RecyclerView.RecycledViewPool();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
switch (myMallModelList.get(position).getType()){
case 0:
return MyMallModel.BANNER_SLIDER;
case 1:
return MyMallModel.STRIP_AD_BANNER;
case 2:
return MyMallModel.HORIZONTAL_PRODUCT_VIEW;
case 3:
return MyMallModel.GRID_PRODUCT_VIEW;
default:
return -1;
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
switch (viewType){
case MyMallModel.BANNER_SLIDER:
View BannerSliderview= LayoutInflater.from(parent.getContext()).inflate(R.layout.sliding_ad_layout,parent,false);
return new BannerSliderViewHolder(BannerSliderview);
case MyMallModel.STRIP_AD_BANNER:
View StripAdview= LayoutInflater.from(parent.getContext()).inflate(R.layout.strip_ad_layout,parent,false);
return new StripAdBannerViewHolder(StripAdview);
case MyMallModel.HORIZONTAL_PRODUCT_VIEW:
View horizontalproductview= LayoutInflater.from(parent.getContext()).inflate(R.layout.horizontal_scoll_layout,parent,false);
return new HorizontalProductViewHolder(horizontalproductview);
case MyMallModel.GRID_PRODUCT_VIEW:
View gridproductview= LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_product_layout,parent,false);
return new GridProductViewHolder(gridproductview);
default:
return null;
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
switch (myMallModelList.get(position).getType()){
case MyMallModel.BANNER_SLIDER:
List<SliderModel> sliderModelList=myMallModelList.get(position).getSliderModelList();
((BannerSliderViewHolder) holder).setBannerSliderViewPager(sliderModelList);
break;
case MyMallModel.STRIP_AD_BANNER:
String resource=myMallModelList.get(position).getResource();
String color=myMallModelList.get(position).getBackgroundColor();
((StripAdBannerViewHolder)holder).StripAd(resource,color);
break;
case MyMallModel.HORIZONTAL_PRODUCT_VIEW:
String title=myMallModelList.get(position).getTitle();
String colorr=myMallModelList.get(position).getBackgroundColor();
List<HorizontalProductScrollModel> horizontalProductScrollModelList=myMallModelList.get(position).getHorizontalProductScrollModelList();
List<WishlistModel> viewAllProductList=myMallModelList.get(position).getViewAllProductList();
((HorizontalProductViewHolder) holder).setHorizontalProductLayout(horizontalProductScrollModelList,title,colorr,viewAllProductList);
break;
case MyMallModel.GRID_PRODUCT_VIEW:
String gridcolorr=myMallModelList.get(position).getBackgroundColor();
String gridtitle=myMallModelList.get(position).getTitle();
List<HorizontalProductScrollModel> gridProductScrollModelList=myMallModelList.get(position).getHorizontalProductScrollModelList();
((GridProductViewHolder) holder).setGridProductLayout(gridProductScrollModelList,gridtitle,gridcolorr);
break;
default:
return;
}
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 myMallModelList.size();
}
public class BannerSliderViewHolder extends RecyclerView.ViewHolder {
private ViewPager bannerSliderViewPager;
private int currentPage;
private Timer timer;
final private long DELAY_TIME=3000;
final private long PERIOD_TIME=3000;
private List<SliderModel> arrangedList;
public BannerSliderViewHolder(@NonNull View itemView) {
super(itemView);
bannerSliderViewPager=itemView.findViewById(R.id.banner_slider_view_pager);
}
public void setBannerSliderViewPager(final List<SliderModel> sliderModelList){
currentPage=2;
if(timer!=null){
timer.cancel();
}
arrangedList=new ArrayList<>();
for(int x=0;x<sliderModelList.size();x++){
arrangedList.add(x,sliderModelList.get(x));
}
arrangedList.add(0,sliderModelList.get(sliderModelList.size()-2));
arrangedList.add(1,sliderModelList.get(sliderModelList.size()-1));
arrangedList.add(sliderModelList.get(0));
arrangedList.add(sliderModelList.get(1));
SliderAdapter sliderAdapter=new SliderAdapter(arrangedList);
bannerSliderViewPager.setAdapter(sliderAdapter);
bannerSliderViewPager.setClipToPadding(false);
bannerSliderViewPager.setPageMargin(20);
bannerSliderViewPager.setCurrentItem(currentPage);
ViewPager.OnPageChangeListener onPageChangeListener=new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
currentPage=position;
}
@Override
public void onPageScrollStateChanged(int state) {
if(state==ViewPager.SCROLL_STATE_IDLE){
pageLooper(arrangedList);
}
}
};
bannerSliderViewPager.addOnPageChangeListener(onPageChangeListener);
startBannerSlideShow(arrangedList);
bannerSliderViewPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
pageLooper(arrangedList);
stopBannerSlideShow();
if(motionEvent.getAction()==MotionEvent.ACTION_UP){
startBannerSlideShow(arrangedList);
}
return false;
}
});
}
private void pageLooper(List<SliderModel> sliderModelList){
if(currentPage==sliderModelList.size()-2){
currentPage=2;
bannerSliderViewPager.setCurrentItem(currentPage,false);
}
if(currentPage==1){
currentPage=sliderModelList.size()-3;
bannerSliderViewPager.setCurrentItem(currentPage,false);
}
}
private void startBannerSlideShow(final List<SliderModel> sliderModelList){
final Handler handler=new Handler();
final Runnable update=new Runnable() {
@Override
public void run() {
if(currentPage>=sliderModelList.size()){
currentPage=1;
}
bannerSliderViewPager.setCurrentItem(currentPage++,true);
}
};
timer=new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(update);
}
},DELAY_TIME,PERIOD_TIME);
}
private void stopBannerSlideShow(){
timer.cancel();
}
}
public class StripAdBannerViewHolder extends RecyclerView.ViewHolder{
private ImageView stripAdImage;
private ConstraintLayout stripAdContainer;
public StripAdBannerViewHolder(@NonNull View itemView) {
super(itemView);
stripAdImage=itemView.findViewById(R.id.strip_ad_image);
stripAdContainer=itemView.findViewById(R.id.strip_ad_container);
}
public void StripAd(String resource,String color){
Glide.with(itemView.getContext()).load(resource).apply(new RequestOptions().placeholder(R.mipmap.pic)).into(stripAdImage);
stripAdContainer.setBackgroundColor(Color.parseColor(color));
}
}
public class HorizontalProductViewHolder extends RecyclerView.ViewHolder{
private ConstraintLayout container;
private TextView horizontallayoutTitle;
private Button horizontalviewAllBtn;
private RecyclerView horizontalRecyclerView;
public HorizontalProductViewHolder(@NonNull View itemView) {
super(itemView);
container=itemView.findViewById(R.id.container);
horizontalRecyclerView=itemView.findViewById(R.id.horizontal_scroll_layout_recycler_view);
horizontalviewAllBtn=itemView.findViewById(R.id.horizontal_scroll_layout_viewall_button);
horizontallayoutTitle=itemView.findViewById(R.id.horizontal_scroll_layout_title);
horizontalRecyclerView.setRecycledViewPool(recycledViewPool);
}
private void setHorizontalProductLayout(final List<HorizontalProductScrollModel> horizontalProductScrollModelList, final String title, String color, final List<WishlistModel> viewAllProductList){
container.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor(color)));
horizontallayoutTitle.setText(title);
for (final HorizontalProductScrollModel model:horizontalProductScrollModelList) {
if (!model.getProductID().isEmpty() && model.getProductTitle().isEmpty()){
firebaseFirestore.collection("PRODUCTS").document(model.getProductID()).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()){
model.setProductTitle(task.getResult().getString("product_title"));
model.setProductPrice(task.getResult().getString("product_price"));
model.setProductImage(task.getResult().getString("product_image_1"));
WishlistModel wishlistModel = viewAllProductList.get(horizontalProductScrollModelList.indexOf(model));
wishlistModel.setTotalRatings(task.getResult().getLong("total_ratings"));
wishlistModel.setRating(task.getResult().getString("average_rating"));
wishlistModel.setProductTitle(task.getResult().getString("product_title"));
wishlistModel.setProductPrice(task.getResult().getString("product_price"));
wishlistModel.setProductImage(task.getResult().getString("product_image_1"));
wishlistModel.setFreeCoupans(task.getResult().getLong("free_coupans"));
wishlistModel.setCuttedPrice(task.getResult().getString("cutted_price"));
wishlistModel.setCOD(task.getResult().getBoolean("COD"));
wishlistModel.setInStock(task.getResult().getLong("stock_quantity") > 0);
if (horizontalProductScrollModelList.indexOf(model) == horizontalProductScrollModelList.size()-1){
if (horizontalRecyclerView.getAdapter()!= null){
horizontalRecyclerView.getAdapter().notifyDataSetChanged();
}
}
}else{
//do nothing
}
}
});
if(horizontalProductScrollModelList.size() > 8){
horizontalviewAllBtn.setVisibility(View.VISIBLE);
horizontalviewAllBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ViewAllActivity.wishlistModelList=viewAllProductList;
itemView.getContext().startActivity(new Intent(itemView.getContext(),ViewAllActivity.class)
.putExtra("layout",0)
.putExtra("title",title)
);
}
});
}else {
horizontalviewAllBtn.setVisibility(View.INVISIBLE);
}
}
}
HorizontalProductScrollAdapter horizontalProductScrollAdapter=new HorizontalProductScrollAdapter(horizontalProductScrollModelList);
LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(itemView.getContext());
linearLayoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);
horizontalRecyclerView.setLayoutManager(linearLayoutManager1);
horizontalRecyclerView.setAdapter(horizontalProductScrollAdapter);
horizontalProductScrollAdapter.notifyDataSetChanged();
}
}
public class GridProductViewHolder extends RecyclerView.ViewHolder{
private ConstraintLayout container;
private TextView gridLayoutTitle;
private Button gridLayoutButton;
private GridLayout gridProductLayout;
public GridProductViewHolder(@NonNull final View itemView) {
super(itemView);
container=itemView.findViewById(R.id.container);
gridLayoutTitle=itemView.findViewById(R.id.grid_product_layout_title);
gridLayoutButton=itemView.findViewById(R.id.grid_product_layout_viewall_button);
gridProductLayout=itemView.findViewById(R.id.grid_layout);
}
private void setGridProductLayout(final List<HorizontalProductScrollModel> horizontalProductScrollModelList, final String title, String color) {
container.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor(color)));
gridLayoutTitle.setText(title);
for(int x=0;x<4;x++){
ImageView productImage=gridProductLayout.getChildAt(x).findViewById(R.id.h_s_product_image);
TextView productTitle=gridProductLayout.getChildAt(x).findViewById(R.id.h_s_product_title);
TextView productDesc=gridProductLayout.getChildAt(x).findViewById(R.id.h_s_product_description);
TextView productPrice=gridProductLayout.getChildAt(x).findViewById(R.id.h_s_product_price);
Glide.with(itemView.getContext()).load(horizontalProductScrollModelList.get(x).getProductImage()).apply(new RequestOptions().placeholder(R.mipmap.pic)).into(productImage);
productTitle.setText(horizontalProductScrollModelList.get(x).getProductTitle());
productDesc.setText(horizontalProductScrollModelList.get(x).getProductDesc());
productPrice.setText("Rs."+horizontalProductScrollModelList.get(x).getProductPrice()+"/-");
gridProductLayout.getChildAt(x).setBackgroundColor(Color.WHITE);
if(!title.equals("")){
final int finalX = x;
gridProductLayout.getChildAt(x).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
itemView.getContext().startActivity(new Intent(itemView.getContext(),ProductDetailsActivity.class).putExtra("PRODUCT_ID",horizontalProductScrollModelList.get(finalX).getProductID()));
}
});
}
}
if(!title.equals("")) {
gridLayoutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ViewAllActivity.horizontalProductScrollModelList = horizontalProductScrollModelList;
itemView.getContext().startActivity(new Intent(itemView.getContext(), ViewAllActivity.class)
.putExtra("layout", 1)
.putExtra("title", title)
);
}
});
}
}
}
}
请尝试帮助我,我一直在寻找解决方案,我尝试了所有方法,但如果你能告诉我我错在哪里,那将非常有用。
解决方案
推荐阅读
- javascript - 承诺 ReaddirSync 不返回值
- html - 删除隐藏溢出的滚动条
- docusignapi - 当信封有多个收件人时,DocuSign Connect 发送重复的 webhook 调用
- javascript - 如何使用 JS 数组的自定义排序进行排序?
- .net - 创建 MMF 以使用 Mediaplayer 类从 MemoryStream 播放声音
- ios - 登录后引用情节提要时不出现 Swift 导航栏
- amazon-web-services - 配置 Concourse CI 以使用 AWS Secrets Manager
- javascript - Django JsonResponse 在被javascript接收时失去精度
- python - 我应该如何在 Python 中使用 Selenium 访问复选框?
- node.js - express cors 仅在某些 api 的根路由上被阻止