In the development process of android application, I have used fragment for rapid development, we know iframe in java web. At this time, fragment has this purpose.
This fragment is android.support.v4.app.Fragmet
1. Define the basic BaseFragmentActivity
import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.support.v4.app.FragmentActivity; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Toast; public abstract class BaseFragmentActivity extends FragmentActivity implements OnClickListener{ protected boolean savedState = false; protected CommandListener commandlistener; //It is very important to pass the command in the activity to the fragment /********************************************/ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate (savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); } /***********************************************/ @Override public void onClick(View v) { // TODO Auto-generated method stub if(commandlistener!=null){ commandlistener.execute(v); } } public void setCommandlistener(CommandListener commandlistener) { this.commandlistener = commandlistener; } public interface CommandListener { public void execute(View v); } }
Second, define your baselayout layout file
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> </LinearLayout>
3. Define BaseFragment
import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class BaseFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.baselayout, container, false); } }
Fourth, define the main interface fragment_main of the application
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/control_popuplay" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#eee" android:orientation="vertical" > <include android:id="@+id/fragment_main_top" android:layout_width="match_parent" android:layout_height="@dimen/title_height" android:layout_alignParentTop="true" layout="@layout/title_view" /> <include android:id="@+id/bottom" android:layout_width="match_parent" android:layout_height="55dp" android:layout_alignParentBottom="true" layout="@layout/bottom_view" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_above="@id/bottom" android:layout_below="@+id/fragment_main_top" android:orientation="vertical" > <fragment android:id="@+id/mainfragment" android:name="com.company.waiqing.activity.fragments.BaseFragment" android:layout_width="fill_parent" android:layout_height="match_parent" /> </LinearLayout> </RelativeLayout>
As can be seen from the above code, the main interface at this time
The effect diagram is as follows:
Five, define your own BaseMainActivity
import android.content.Context; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.util.DisplayMetrics; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.ViewGroup.LayoutParams; import android.view.WindowManager; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; public abstract class BaseMainActivity extends BaseFragmentActivity { protected ImageView left_button; //The button on the left side of the header bar protected TextView title; //Title of the header bar protected ImageView rightbutton; //The right button of the header bar protected RelativeLayout bottom; //The layout of the bottom bar private Popup_holder holder; //Popup box protected PopupWindow window; @Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_UP && event.getKeyCode() == KeyEvent.KEYCODE_BACK) { BaseMainActivity.this.finish(); return true; } if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_MENU) { return true; } return super.dispatchKeyEvent(event); // Press other buttons, call the parent class for default processing } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate (savedInstanceState); //Open Activity to hide the soft keyboard; getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); setContentView(R.layout.fragment_main); Logger.i("test", "initUI"); initUI (); initTitle(); run(); initFragment(); // initialize fragment } private void initUI() { bottom=(RelativeLayout) findViewById(R.id.bottom); } public abstract void run(); protected void initTitle() { left_button = (ImageView) findViewById(R.id.iv_left_button); title = (TextView) findViewById(R.id.title_text); rightbutton = (ImageView) findViewById(R.id.iv_right_button); left_button.setOnClickListener(topviewlisterner); rightbutton.setOnClickListener(topviewlisterner); // back.setImageResource(R.drawable.back_icon); // back.setOnClickListener(new OnClickListener() { // // @Override // public void onClick(View v) { // // TODO Auto-generated method stub // finish(); // } // }); } private void initFragment() { Logger.i("test", "initFragment"); CommonFragment f = (CommonFragment) setFragment(); Logger.i("test", "status:" + savedState); if (f != null) { f.setRefreshListener(this); fragmentchage(f); } } // set the middle fragment protected Fragment setFragment() { return null; } // Set the current fragment interface private void fragmentchage(Fragment newFragment) { FragmentManager fragmentManager = getSupportFragmentManager(); fragmentTransaction fragmentTransaction = fragmentManager .beginTransaction(); fragmentTransaction.replace(R.id.mainfragment, newFragment); fragmentTransaction.commit(); } @Override protected void onPause() { // TODO Auto-generated method stub // FragmentManager fragmentManager = getSupportFragmentManager(); // Logger.i("test", fragmentManager.getBackStackEntryCount() + ""); // fragmentManager.popBackStack(); super.onPause(); } //Top button listener, here you can process the top button OnClickListener topviewlisterner=new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { // make a popup case R.id.iv_left_button: initControlPopupView (v); break; case R.id.iv_right_button: BaseMainActivity.this.finish(); break; } } }; }
Six, define the basic CommonFragment
import android.app.Activity; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Intent; import android.content.IntentFilter; import android.content.ServiceConnection; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.util.Log; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * * @author arrau * The commandlistener must be implemented, which is used to receive commands from the main interface */ public abstract class CommonFragment extends Fragment implements CommandListener{ protected BaseFragmentActivity context; protected DestoryListener destorylistener; protected View view; @Override public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); Logger.i(log, "onAttach"); context = (BaseFragmentActivity) activity; } public void setFragmentContentView(int layout) { view = LayoutInflater.from(context).inflate(layout, null); context.setCommandlistener(this); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub Logger.i(log, "onCreateView"); return view; } public FragmentManager getSupportFragmentManager() { return context.getSupportFragmentManager(); } public View findViewById(int id) { return view.findViewById(id); } public FragmentActivity getContext() { return context; } @Override public void onDestroy() { // TODO Auto-generated method stub if(destorylistener!=null) { destorylistener.FragmentDestory(); //When the fragment exits, let the activity exit } super.onDestroy (); } @Override public void onPause() { // TODO Auto-generated method stub Logger.i(log, "onPause"); super.onPause(); } @Override public void onResume() { // TODO Auto-generated method stub Logger.i(log, "onResume"); super.onResume(); } @Override public void onStart() { // TODO Auto-generated method stub Logger.i(log, "onStart"); super.onStart(); } @Override public void onStop() { // TODO Auto-generated method stub Logger.i(log, "onStop"); super.onStop(); } @Override public void onDestroyView() { // TODO Auto-generated method stub Logger.i(log, "move to other view"); super.onDestroyView (); } @Override public void execute(View v) { // TODO Auto-generated method stub } //fragment destruction notification public interface DestoryListener { public void FragmentDestory(); } public DestoryListener getDestorylistener() { return destorylistener; } public void setDestorylistener(DestoryListener destorylistener) { this.destorylistener = destorylistener; } }
7. How to use
Some people may ask, with so much code above, I might as well write one activity one by one. My point is that although there is a lot of code above, if there are a lot of interfaces, then a lot of time can be saved for the convenience of future development.
In the future, each activity must inherit BaseMainActivity
ClientPoolActivity extends BaseMainActivity{ @Override public void run() { // TODO Auto-generated method stub title.setText("Client Pool"); } @Override protected Fragment setFragment() { // TODO Auto-generated method stub ClientPoolFragment cf=new ClientPoolFragment(); return cf; } }
Each fragment must inherit ClientNewCreateFragment
import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.LinearLayout; import android.widget.TextView; import com.company.core.utils.IntentUtil; public class ClientNewCreateFragment extends CommonFragment{ private Client_Holder holder; private ClickListener clistener; @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate (savedInstanceState); holder=new Client_Holder(); clistener=new ClickListener(); setFragmentContentView(R.layout.fragment_client_newcreate);//The layout of the middle part holder.t_company=(LinearLayout)findViewById(R.id.fragment_client_newcreate_companylin); holder.t_person=(LinearLayout)findViewById(R.id.fragment_client_newcreate_personlin); holder.t_company.setOnClickListener(clistener); holder.t_person.setOnClickListener(clistener); } private class Client_Holder{ LinearLayout t_company,t_person; } private class ClickListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub } } //This is the view after the main interface has been monitored, and it is BaseMainActivity that passes the button event over @Override public void execute(View v) { // TODO Auto-generated method stub } }
At this time, the header bar does not need to be configured by the developer in the future. If there is a return, then it has been configured after inheriting this.