我不能在这里使用大多数解决方案来实现这个工作。请检查我的代码,在活动上开始片段。如何在ImageView中设置onClick关闭片段?
public void selectFrag() {
Fragment fr;
fr = new SelectionFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
//I removed this since my animations give error with FrameLayout
//fragmentTransaction.setCustomAnimations(R.anim.slide_in_up, R.anim.slide_out_down);
fragmentTransaction.replace(R.id.fragment_selection, fr);
fragmentTransaction.commit();
}
在我的片段,我有一个扩展android.support.v4.app.Fragment;
public class SelectionFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
View view = inflater.from(getActivity()).inflate(
R.layout.fragment_attributes, container, false);
//Close fragment using close icon
ImageView close = (ImageView) view.findViewById(R.id.close_fragment);
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
closefragment();
}
});
return view;
}//End onCreateView Method
private void closefragment() {
getActivity().getFragmentManager().popBackStack();
}
}
我的XML以下内部包含一个RelativeLayout的
<ImageView
android:id="@+id/close_fragment"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:paddingTop="3dp"
android:paddingRight="3dp"
android:src="@drawable/close_cross"/>
如何设置ImageView的到的ImageView听点击关闭片段?我只有一个片段,上面的代码没有任何反应。
[如何使用按钮像后退按钮来关闭当前片段?(HTTP的可能重复: //stackoverflow.com/questions/20812922/how-to-close-the-current-fragment-by-using-button-like-the-back-button) – Taras
@Taras检查代码以关闭片段我都用过,它正是山姆e作为您链接的副本。这不适用于我的ImageView。 – Sunday
但ADD代码片段不同。注意_use addToBackstack()在commit()_部分被接受的答案之前。 – Taras