【问题标题】:How can I set onClick to close fragment in ImageView?如何设置 onClick 以关闭 ImageView 中的片段?
【发布时间】: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 来监听关闭片段的点击?我只有一个片段,上面的代码没有任何反应。

【问题讨论】:

标签: android android-fragments


【解决方案1】:

从 closefragment() 中删除 getActivity()

private void closefragment() {
    getFragmentManager().popBackStack();
}

【讨论】:

    【解决方案2】:

    解决方法如下:

    fragmentTransaction.replace(R.id.fragment_selection, fr);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();`
    

    【讨论】:

    • 抱歉,我之前尝试过此代码,但我在没有 addToBackStack() 的情况下进行测试时发布了代码。它仍然没有关闭片段。
    • 这一行:getActivity().getFragmentManager().popBackStack();,不应该是getSupportFragmentManager吗?
    • 或者你可以直接打电话给getFragmentManager().popBackStack()而不用getActivity()
    • 我正在使用 import android.support.v4.app.FragmentManager;因此我调用 getSupportFragmentManager()
    【解决方案3】:

    popBackStack();只会撤销您之前的交易。如果你想关闭这个 Fragment 你应该调用

    getActivity().getFragmentManager().beginTransaction().remove(this).commit();
    

    【讨论】:

      【解决方案4】:

      请试试这个。

      private void closefragment() {
      getParentFragment().getFragmentManager().popBackStack(); }
      

      或者这个

      private void closefragment() {
      getActivity.finish(); }
      

      【讨论】:

      • 试图在空对象上调用 getFragmentManager()。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-02
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 2020-06-27
      相关资源
      最近更新 更多