2017-03-18 53 views
1

我有一个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); 
} 

} 

回答

2

尝试getActivity().onBackPressed();。它将模拟后退按钮上的按下。我认为这是处理它的最好方法,因为您希望此按钮的行为与后退按钮完全相同。

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

相关问题