[英]Fragment to Fragment transaction overlap
因此,在嘗試用RecyclerView Adapter類中的新片段替換當前片段之后,我得到的是一個片段出現在另一個片段之上。 我不確定是否必須銷毀當前片段以及回收站視圖中的所有數據才能顯示新片段。
RecyclerViewAdapter類
public class TouristLocationCategoryAdapter extends RecyclerView.Adapter<TouristLocationCategoryAdapter.TouristLocationCategoryViewHolder> {
private Context context;
private List<LocationCategory> categoryList;
private Fragment fragment;
public TouristLocationCategoryAdapter(Context context, List<LocationCategory> categoryList, Fragment fragment){
this.context = context;
this.categoryList = categoryList;
this.fragment = fragment;
}
@NonNull
@Override
public TouristLocationCategoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.tourist_location_category_layout, null);
return new TouristLocationCategoryViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final TouristLocationCategoryViewHolder holder, int position) {
LocationCategory category = categoryList.get(position);
holder.categoryImage.setImageDrawable(context.getResources().getDrawable(category.getCategoryImage()));
holder.categoryName.setText(category.getCategoryName());
holder.categoryImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentTransaction ft = fragment.getChildFragmentManager().beginTransaction();
Fragment newFragment = categoryList.get(holder.getAdapterPosition()).getCategoryFragment();
ft.replace(R.id.tourist_explore_frame_layout, newFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
});
}
@Override
public int getItemCount() {
return categoryList.size();
}
public class TouristLocationCategoryViewHolder extends RecyclerView.ViewHolder{
ImageView categoryImage;
TextView categoryName;
public TouristLocationCategoryViewHolder(View itemView) {
super(itemView);
categoryImage = itemView.findViewById(R.id.tourist_location_category_image);
categoryName = itemView.findViewById(R.id.tourist_location_category_name);
}
}
}
片段類
public class TouristExplore extends Fragment{
RecyclerView recyclerView;
GridLayoutManager gridLayoutManager;
TouristLocationCategoryAdapter touristLocationCategoryAdapter;
List<LocationCategory> categoryList;
Museum museumFragment;
Shopping shoppingFragment;
public TouristExplore() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tourist_explore, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.tourist_explore_recycler_view);
categoryList = new ArrayList<>();
museumFragment = new Museum();
shoppingFragment = new Shopping();
categoryList.add(new LocationCategory("Museum",R.drawable.test_image_one, museumFragment));
categoryList.add(new LocationCategory("Shopping", R.drawable.test_image, shoppingFragment));
gridLayoutManager = new GridLayoutManager(getActivity(),2);
touristLocationCategoryAdapter = new TouristLocationCategoryAdapter(getActivity(), categoryList,TouristExplore.this);
recyclerView.setAdapter(touristLocationCategoryAdapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(gridLayoutManager);
return rootView;
}
}
片段的XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/tourist_explore_frame_layout">
<android.support.v7.widget.RecyclerView
android:id="@+id/tourist_explore_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v7.widget.RecyclerView>
</FrameLayout>
LocationCategory類別
public class LocationCategory {
private String categoryName;
private int categoryImage;
private Fragment categoryFragment;
public LocationCategory(String categoryName, int categoryImage, Fragment categoryFragment) {
this.categoryName = categoryName;
this.categoryImage = categoryImage;
this.categoryFragment = categoryFragment;
}
public Fragment getCategoryFragment() {
return categoryFragment;
}
public void setCategoryFragment(Fragment categoryFragment) {
this.categoryFragment = categoryFragment;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public int getCategoryImage() {
return categoryImage;
}
public void setCategoryImage(int categoryImage) {
this.categoryImage = categoryImage;
}
}
我的目的是刪除單擊適配器之一中設置的imageView和TextView並啟動適當的片段。 有什么想法可能是問題所在,為什么我要移動的片段的內容只是顯示在適配器中的頂部?
據我了解,您想替換一個新片段時會忘記一個片段的狀態。 使用fragmentTransaction.addToBackStack(null);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.