fragment-实例创建跟使用
今天接触到了fragment,在网上看到了它的使用方法。现在把它的创建跟使用记录下,方便以后查询。
1.创建一个自己的fragment,继承自fragment
package com.android.calculator2; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ListView; import android.view.animation.Animation; import android.view.animation.AnimationUtils; public class HistoryFragment extends Fragment { private View mView; private ListView mListView; private Button mClearHistoryBtn; private Button mBackCalculatorBtn; private HistoryAdapter mHistoryAdapter; private Animation myAnimationIn; private Animation myAnimationOut; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mView = inflater.inflate(R.layout.history_display, container, false); myAnimationIn = AnimationUtils.loadAnimation(getActivity(), R.anim.history_fragment_in_anim); myAnimationOut = AnimationUtils.loadAnimation(getActivity(),R.anim.history_fragment_out_anim); myAnimationIn.setFillAfter(true); myAnimationOut.setFillAfter(true); if (mView != null) { mView.startAnimation(myAnimationIn); mClearHistoryBtn = (Button) mView.findViewById(R.id.history_clear); mBackCalculatorBtn = (Button) mView.findViewById(R.id.history_back); mListView = (ListView) mView.findViewById(R.id.history_list); if (getActivity() instanceof HistoryClickListener) { mHistoryAdapter = ((HistoryClickListener) getActivity()).getHistoryAdapter(); } mListView.setAdapter(mHistoryAdapter); mBackCalculatorBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { if (getActivity() instanceof HistoryClickListener) { ((HistoryClickListener) getActivity()).historyClickHide(); } } }); mClearHistoryBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub if (getActivity() instanceof HistoryClickListener) { ((HistoryClickListener) getActivity()).historyClickClear(); } } }); return mView; } else { return super.onCreateView(inflater, container, savedInstanceState); } } public interface HistoryClickListener { void historyClickHide(); void historyClickClear(); HistoryAdapter getHistoryAdapter(); } @Override public void onDestroyView() { if (mView != null) { mView.startAnimation(myAnimationOut); } super.onDestroyView(); } }
然后在代码中使用
FragmentManager mHistoryFm = getFragmentManager(); FragmentTransaction mHistoryTx = mHistoryFm.beginTransaction(); mHistoryTx.replace(R.id.frame_layout, mHistoryFragment, "history"); mHistoryTx.addToBackStack(null); mHistoryTx.commit();
它的使用也很简单。只需要创建,然后在activity中加载到一个布局里面就行了。
有以下几点需要注意
1.你必须在activity的xml中分配一个布局容器给你的fragment,就是方法里面的
mHistoryTx.replace(R.id.frame_layout, mHistoryFragment, "history");
R.id.frame_layout
2.如果你想移除一个fragment,你必须拥有这个fragment的实例,也就是
mHistoryTx.replace(R.id.frame_layout, mHistoryFragment, "history");
mHistoryTx.remove( mHistoryFragment);
上面的两个mHistoryFragment是同一个实例
3.尽量让fragment中事件的处理,交给activity来处理。常用的方法就是
在fragment想要实现的操作,定义为一个接口,然后在activity中实现这个接口
定义接口
public interface HistoryClickListener { void historyClickHide(); void historyClickClear(); HistoryAdapter getHistoryAdapter(); }
实现接口
public class Calculator extends Activity implements Logic.Listener, HistoryClickListener{ @Override public void historyClickHide() { android.util.Log.e("zhangshuli", "mmmm"); FragmentManager mHistoryFm = getFragmentManager(); FragmentTransaction mHistoryTx = mHistoryFm.beginTransaction(); mHistoryTx.remove(mHistoryFragment); mHistoryTx.commit(); } @Override public void historyClickClear() { android.util.Log.e("zhangshuli", "nnnn"); mHistory.clear(); mLogic.onClear(); } @Override public HistoryAdapter getHistoryAdapter() { return mAdapter; } }
在fragment中回调activity中的方法
if (getActivity() instanceof HistoryClickListener) { ((HistoryClickListener) getActivity()).historyClickClear(); }