【发布时间】:2015-10-10 16:08:25
【问题描述】:
我无法使用这里的大多数解决方案来完成这项工作。请检查我在 Activity 上启动片段的代码。
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
<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 来监听关闭片段的点击?我只有一个片段,上面的代码没有任何反应。
【问题讨论】:
-
@Taras 检查代码以关闭我使用的片段,它与您链接的副本完全相同。这不适用于我的 ImageView。
-
但是添加片段的代码是不同的。注意在commit()之前使用addToBackstack()部分接受的答案。