Weiyi Address Book of Android Project (1), androidweiyi
I haven't written a blog for a long time. I 've been busy looking for a job for the past few days and have no time to write a blog. This week I want to take a break and I have done this small project. When I want to practice it
In the early morning, I am still writing a blog to help people who need it.
Let's talk about the functions of this project, including the contact list, call records, SMS records, and settings. Some functions may be added later. This project mainly uses similar UI effects. I personally think it is not bad.
You have used custom controls in the contact list.
----------- Welcome to join the discussion group 386451316 for further discussion.
Please note that it is not easy to write the reprinted address so late.
Let's take a look. This is the first module and will be updated later.
Today, we will introduce Fragment + RadioButton to replace tabhost, which seems to be the most popular mode ,,
Next, we will introduce the layout.
Activity_main.xml File
<RelativeLayout xmlns: andactivity_main.xmlroid = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: background = "# FFF" android: layout_height = "match_parent"> <android. support. v4.view. viewPager android: id = "@ + id/pager" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_above = "@ + id/titlelayout"> </android. support. v4.view. viewPager> <LinearLayout android: id = "@ + id/titlelayout" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_alignParentBottom = "true"> <RadioGroup android: id = "@ + id/tab_menu" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: background = "@ drawable/tab_bg_normal" android: orientation = "horizontal"> <RadioButton android: id = "@ + id/title1" style = "@ style/tab_style" android: checked = "true" android: drawableTop = "@ drawable/tab_activity_selector" android: text = "Contact"/> <RadioButton android: id = "@ + id/title2" style = "@ style/tab_style" android: drawableTop = "@ drawable/tab_activity_selector" android: text = "call history"/> <FrameLayout android: layout_width = "wrap_content" android: layout_height = "wrap_content"> <RadioButton android: id = "@ + id/title3" style = "@ style/tab_styles" android: drawableBottom = "@ drawable/tab_activity_selector"/> </FrameLayout> <RadioButton android: id = "@ + id/title4" style = "@ style/tab_style" android: drawableTop = "@ drawable/tab_activity_selector" android: text = "SMS"/> <RadioButton android: id = "@ + id/title5" style = "@ style/tab_style" android: drawableTop = "@ drawable/tab_activity_selector" android: text = "set"/> </RadioGroup> </LinearLayout> </RelativeLayout>
File per page
<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent"> <TextViewandroid: id = "@ + id/TV _tab4" android: layout_width = "fill_parent" android: layout_height = "match_parent" android: text = "_height =" android: textSize = "28sp"/> </RelativeLayout>
All of the above are basic layout files. You can understand them. In fact, this phase is all basic.
Package com. zw. weiyi. phonesms; import java. util. arrayList; import android. OS. bundle; import android. support. v4.app. fragment; import android. support. v4.view. pagerAdapter; import android. support. v4.view. viewPager; import android. support. v4.view. viewPager. onPageChangeListener; import android. view. keyEvent; import android. view. motionEvent; import android. view. view; import android. view. view. onClickListener; import android. widget. radioButton; import android. widget. toast; import com. zw. weiyi. fragment. callFragment; import com. zw. weiyi. fragment. infoFragment; import com. zw. weiyi. fragment. settingFragment; import com. zw. weiyi. fragment. smsFragment; import com. zw. weiyi. util. myApplication;/***** @ author Striver_zw * Date 2014-11-19 */public class MainActivity extends BaseActivity implements OnPageChangeListener {private ViewPager pager; private PagerAdapter mAdapter; private ArrayList <Fragment> Fragment; private ArrayList <RadioButton> title = new ArrayList <RadioButton> (); // four titles // dialing button private RadioButton bu; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // initView () is rewritten in sjavasingmenu; // The initTitle (); initViewPager (); bu. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {Toast. makeText (MainActivity. this, "intermediate", 1 ). show () ;}}) ;}/ *** initialize view */private void initView () {bu = (RadioButton) findViewById (R. id. title3); pager = (ViewPager) findViewById (R. id. pager); // initialize the control fragments = new ArrayList <Fragment> (); // initialize the data fragments. add (new InfoFragment (this); fragments. add (new CallFragment (); // fragments. add (new PhoneFragment (); fragments. add (new SmsFragment (); fragments. add (new SettingFragment (this);}/*** initialize ViewPager */private void initViewPager () {mAdapter = new MyViewPagerAdapter (getsuppfrfragmentmanager (), fragments); pager. setAdapter (mAdapter); pager. setOnPageChangeListener (this); pager. setCurrentItem (0); // set to the current first}/*** initialize several RadioButton/private void initTitle () {title. add (RadioButton) findViewById (R. id. title1); // three title tags: title. add (RadioButton) findViewById (R. id. title2); // title. add (RadioButton) findViewById (R. id. title3); title. add (RadioButton) findViewById (R. id. title4); title. add (RadioButton) findViewById (R. id. title5); title. get (0 ). setOnClickListener (new MyOnClickListener (0); // set the response title. get (1 ). setOnClickListener (new MyOnClickListener (1); title. get (2 ). setOnClickListener (new MyOnClickListener (2); title. get (3 ). setOnClickListener (new MyOnClickListener (3);}/*** rewrite the OnClickListener response function. The main purpose is to click the title, pager will switch with the response ** @ author **/private class MyOnClickListener implements OnClickListener {private int index; public MyOnClickListener (int index) {this. index = index ;}@ Overridepublic void onClick (View v) {pager. setCurrentItem (index); // cut the viewpager view to produce the title associated with pager. get (index ). setChecked (true); // set to be selected, otherwise, the background in the layout will not be switched}/*** the following three interface functions of OnPageChangeListener */@ Overridepublic void onPageScrollStateChanged (int arg0) {}@ Overridepublic void onPageScrolled (int arg0, float arg1, int arg2) {}@ Overridepublic void onPageSelected (int arg0) {title. get (arg0 ). setChecked (true); // linkage between the page and button} // record exit times protected int dFilnish = 0; // intercept the touch event @ Overridepublic boolean dispatchTouchEvent (MotionEvent ev) {dFilnish = 0; // return super. dispatchTouchEvent (ev) ;}@ Overridepublic boolean onKeyUp (int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_BACK) {// return key dFilnish ++; if (dFilnish = 1) {Toast. makeText (this, "click to exit", Toast. LENGTH_LONG ). show ();} else if (dFilnish = 2) {MyApplication. getInstance (). appExit (); // super. finish ();} return true;} return super. onKeyUp (keyCode, event );}}
It should not be difficult to annotate all the above Code
Java code on each page
Package com. zw. weiyi. fragment; import android. OS. bundle; import android. support. v4.app. fragment; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import com. zw. weiyi. phonesms. r; public class PhoneFragment extends Fragment {@ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState);} @ Overridepublic View onCreateView (LayoutInflate R inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater. inflate (R. layout. phone_fragment, container, false); // The cached rootView must be used to determine whether a parent has been added. If a parent needs to be deleted from the parent, IllegalStateException may occur. ViewGroup parent = (ViewGroup) view. getParent (); if (parent! = Null) {parent. removeView (view) ;}return view ;}}
Use the adapter FragmentPagerAdapter to load it and read the code.
Package com. zw. weiyi. phonesms; import java. util. arrayList; import android. support. v4.app. fragment; import android. support. v4.app. fragmentManager; import android. support. v4.app. fragmentPagerAdapter;/*** This adapter contains the Fragment array * @ author Striver_zw * Date 2014-11-19 */public class MyViewPagerAdapter extends FragmentPagerAdapter {private ArrayList <Fragment> fragments; // Fragmentpublic MyViewPagerAdapter (FragmentManager fm) {super (fm );} /*** custom constructor * @ param fm * @ param fragments ArrayList <Fragment> */public MyViewPagerAdapter (FragmentManager fm, ArrayList <Fragment> fragments) {super (fm); this. fragments = fragments;} @ Overridepublic Fragment getItem (int arg0) {return fragments. get (arg0); // returns the Fragment object} @ Overridepublic int getCount () {return fragments. size (); // number of returned Fragment }}
Let's get here today. Set up the framework first.