引用活动中的片段元素 [英] Reference to elements of fragment from activity

查看:113
本文介绍了引用活动中的片段元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以引用片段的xml布局中定义的元素(如按钮)并将其用于其他活动?

It is possible to get reference to element(like button) defined in xml layout of fragment and use it in another activity?

我试图这样做,但是对象引用为空.

I tried to do that but have null object reference.

fragment_date_picker.xml

fragment_date_picker.xml

<TimePicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/timePicker"
    android:layout_gravity="center_horizontal|top" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ok"
    android:id="@+id/bt_ok"
    android:layout_gravity="center" />

MainActivity.java

MainActivity.java

btPickTime=(Button)findViewById(R.id.bt_pickTime);
    AlarmManager alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);
    final DatePickerFragment dp=new DatePickerFragment();

    btOk=(Button)findViewById(R.id.bt_ok);
    btOk.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.remove(dp);
            ft.commit();
        }
    });
    btPickTime.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(R.id.ll_main
                    ,dp);
            ft.commit();

        }
    });

推荐答案

要删除 Fragment ,可通过在该Fragment内按按钮来删除.

For removing a Fragment, via a button press inside said Fragment.

方法一(可能是最简单的方法)

我尚未对此进行测试,因此可能无法...但是您可以尝试从此片段内部访问 FragmentManager ,然后将其删除.在这种情况下,您可以在 onClick()内部调用它.您可能需要在此处将 getActivity()放在 getFragmentManager()前面.

I haven't tested this, so it may not be possible...but you could try and access the FragmentManager from inside this fragment, and then have it remove itself. In this case, you would call this inside your onClick(). You may need to place getActivity() in front of getFragmentManager() here.

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

方法二(几乎很简单,但是很糟糕的做法)

将以上逻辑放在 Fragment 所附加的 Activity 类的公共方法中,并在 Fragment onClick()就像这样:

Place the above logic inside a public method in the Activity class your Fragment is attached to, and access it inside your Fragment onClick() like so:

((MyActivityName)getActivity()).nameOfPublicMethodToRemoveFragment();

方法三( Fragment 与它的 Activity 进行通信的推荐方式)

Method Three (Recommended way for a Fragment to Communicate with its Activity)

使用界面(示例摘自此处):

    public class BlankFragment extends Fragment implements View.OnClickListener{

    private View rootView;
    private Button button;

    private OnFragmentInteractionListener mListener;

    public static BlankFragment newInstance() {
        return new BlankFragment();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_blank, container, false);
        button = (Button) rootView.findViewById(R.id.fragment_button);
        button.setOnClickListener(this);

        return rootView;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onClick(View v) {
        mListener.onFragmentInteraction();
    }

    public interface OnFragmentInteractionListener {
        void onFragmentInteraction();
    }

}

主要活动

public class MainActivity extends AppCompatActivity implements BlankFragment.OnFragmentInteractionListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (getFragmentManager().findFragmentById(R.id.fragment_container) == null) {
            getFragmentManager()
                    .beginTransaction()
                    .add(R.id.fragment_container, BlankFragment.newInstance())
                    .commit();
        }
    }

    @Override
    public void onFragmentInteraction() {
        //Remove Fragment Here
    }
}

方法四(替代方法)

使用 EventBus 从片段到活动进行通信

Use an EventBus to communicate from the Fragment to the Activity

这篇关于引用活动中的片段元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆