gpt4 book ai didi

android - 返回上一个 fragment 的正确方法是什么?

转载 作者:行者123 更新时间:2023-11-29 19:19:00 31 4
gpt4 key购买 nike

我有一个包含两个 fragment 的 MainActivity:一个在列表中显示图像,另一个显示列表中被单击的图像。内置后退按钮可以很好地从我的 DetailFragment 导航回我的 ImageListFragment,但我还想在 DetailFragment 的左上角有一个按钮 允许导航回 ImageListFragment。我已经尝试了通常的 getActivity().getSupportFragmentManager().popBackStack(); 但它不会将我送回 ImageListFragment

这是我的 MainActivity:

  public class MainActivity extends AppCompatActivity implements ImageListFragment.OnGalleryImageSelected {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.add(R.id.root_layout, ImageListFragment.newInstance(), "galleryImageList")
.commit();
}
}
@Override
public void OnGalleryImageSelected(int imageResId) {
final ImageDetailFragment detailsFragment = ImageDetailFragment.newInstance(imageResId);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.root_layout, detailsFragment, "galleryImageDetails")
.addToBackStack(null)
.commit();
}



}

这是DetailFragment:

  public class ImageDetailFragment extends Fragment {
private static final String IMAGE_ID = "imageResId";

public static ImageDetailFragment newInstance(int imageResId) {

final Bundle args = new Bundle();
args.putInt(IMAGE_ID, imageResId);
final ImageDetailFragment fragment = new ImageDetailFragment();
fragment.setArguments(args);
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_image_details, container, false);
final ImageView imageView = (ImageView) view.findViewById(R.id.gallery_image);
final Bundle args = getArguments();
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("button clicked");
FragmentManager fm = getActivity().getSupportFragmentManager();
fm.popBackStack();
}

});
imageView.setImageResource(args.getInt(IMAGE_ID));
return view;
}

}

这里是 ImageListFragment:

 public class ImageListFragment extends Fragment {
private int[] mImageResIds;
private OnGalleryImageSelected mListener;
public static ImageListFragment newInstance() {
return new ImageListFragment();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);

mListener = (OnGalleryImageSelected) context;

final Resources resources = context.getResources();
final TypedArray typedArray = resources.obtainTypedArray(R.array.images);
final int imageCount = 18;
mImageResIds = new int[imageCount];
for (int i = 0; i < imageCount; i++) {
mImageResIds[i] = typedArray.getResourceId(i, 0);
}
typedArray.recycle();
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_image_list, container, false);

final Activity activity = getActivity();
final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(activity));
recyclerView.setAdapter(new GalleryImageAdapter(activity));
return view;
}

class GalleryImageAdapter extends RecyclerView.Adapter<ViewHolder> {

private LayoutInflater mLayoutInflater;

public GalleryImageAdapter(Context context) {
mLayoutInflater = LayoutInflater.from(context);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
return new ViewHolder(mLayoutInflater
.inflate(R.layout.recycler_view, viewGroup, false));
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {

final int imageResId = mImageResIds[position];

viewHolder.setData(imageResId);
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = FooPager.newIntent(getActivity(), position);
startActivity (intent);
}
});
}

@Override
public int getItemCount() {
return mImageResIds.length;
}
}

class ViewHolder extends RecyclerView.ViewHolder {
private ImageView mImageView;
private ViewHolder(View itemView) {
super(itemView);
mImageView = (ImageView) itemView.findViewById(R.id.gallery_image);

}
private void setData(int imageResId) {
mImageView.setImageResource(imageResId);
}
}
public interface OnGalleryImageSelected {
void OnGalleryImageSelected(int imageResId);
}

}

最佳答案

试试 getActivity().onBackPressed();。它将模拟按下后退按钮。我认为这是处理它的最佳方式,因为您希望此按钮的行为与后退按钮完全一样。

这样做将确保如果您稍后对“onBackPressed()”进行更改,这些更改也将应用到您的按钮。

关于android - 返回上一个 fragment 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42869444/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com