Requirements Description:
As the mobile phone functions more and more perfect, the corresponding preferences are more and more, from the user experience point of view, in order to enable users in a short period of time for common preferences to operate, such as WiFi, cellular data. A separate display of some of the most common settings features has been achieved to reduce the likelihood of user action. The System Settings page is therefore paginated and can be toggled.
:
To understand settings source of the classmate should know, settings design is relatively not very simple, bold on settings may cause more bugs. Therefore, if the requirement is fulfilled, the modification of low-coupling high cohesion is carried out in the case of maintaining the original settings function intact.
Since settings is an activity, I use Viewpager to combine activity to implement this function. That is, the settings as a sub-view embedded in the Viewpager, while cloning a new settings for the offenusedsettings as another word view, The offenusedsettings uses the same design idea as the settings, or it can be completely duplicated, just as it is loaded into the layout. Specific architecture design such as:
Here Settingsactivity.java all the code and Offenusedactivity.java key part of the code, the other parts of the same as the original settings.
Settingsactivity.java Code:
Package com.android.settings;
Import java.util.ArrayList;
Import android.app.Activity;
Import Android.app.LocalActivityManager;
Import Android.content.Context;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.support.v4.view.ViewPager;
Import Android.support.v4.view.ViewPager.OnPageChangeListener;
Import Android.view.View;
Import Android.view.Window;
Import Android.widget.CompoundButton;
Import Android.widget.RadioGroup;
Import Android.widget.CompoundButton.OnCheckedChangeListener;
Import Android.widget.RadioButton;
public class Settingsactivity extends Activity implements Oncheckedchangelistener, Onpagechangelistener {
Private Radiogroup Mradiogroup;
Private RadioButton moffenused, MAll;
Private Viewpager Mviewpager;
Private Context Mcontext;
Private Localactivitymanager Mmanager;
Private Settingsadapter Madapter;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Requestwindowfeature (Window.feature_no_title);
Setcontentview (r.layout.activity_settings);
Mcontext = Settingsactivity.this;
Mmanager = new Localactivitymanager (this, false);
Mmanager.dispatchcreate (savedinstancestate);
Initview ();
}
@Override
protected void Onresume () {
Super.onresume ();
Mmanager.dispatchresume ();
if (Mviewpager! = null) {
Switch (Mviewpager.getcurrentitem ()) {
Case 0:
Activity offenusedactivity = mmanager.getactivity ("offenused");
if (offenusedactivity! = null && offenusedactivity instanceof offenusedsettings) {
((offenusedsettings) offenusedactivity). Invisibleonscreen ();
}
Break
Case 1:
Activity allactivity = mmanager.getactivity ("All");
if (allactivity! = null && allactivity instanceof Settings) {
((Settings) allactivity). Invisibleonscreen ();
}
Break
}
}
}
private void Initview () {
Mradiogroup = (radiogroup) Findviewbyid (r.id.rg_settings);
moffenused = (RadioButton) Findviewbyid (r.id.rb_offen_used);
Moffenused.setoncheckedchangelistener (this);
MAll = (RadioButton) Findviewbyid (R.id.rb_all);
Mall.setoncheckedchangelistener (this);
Mviewpager = (Viewpager) Findviewbyid (r.id.vp_settings);
Mviewpager.setonpagechangelistener (this);
Final arraylist<view> list = new arraylist<view> ();
Intent Intentcommon = new Intent (Mcontext, Offenusedsettings.class);
List.add (GetView ("offenused", Intentcommon));
Intent Intentmain = new Intent (Mcontext, Settings.class);
List.add (GetView ("All", Intentmain));
Madapter = new Settingsadapter (mcontext, list);
Mviewpager.setadapter (Madapter);
if (moffenused.ischecked ()) {
Mviewpager.setcurrentitem (0);
Mradiogroup.setbackgroundresource (R.drawable.settings_common);
} else if (mall.ischecked ()) {
Mviewpager.setcurrentitem (1);
Mradiogroup.setbackgroundresource (R.drawable.settings_main);
}
}
Private View GetView (String ID, Intent Intent) {
return mmanager.startactivity (ID, intent). Getdecorview ();
}
@Override
public void OnCheckedChanged (Compoundbutton arg0, Boolean arg1) {
if (arg1) {
Switch (Arg0.getid ()) {
Case r.id.rb_offen_used:
Mviewpager.setcurrentitem (0);
Activity offenusedactivity = mmanager.getactivity ("offenused");
if (offenusedactivity! = null && offenusedactivity instanceof offenusedsettings) {
((offenusedsettings) offenusedactivity). Invisibleonscreen ();
}
Break
Case R.id.rb_all:
Mviewpager.setcurrentitem (1);
Activity allactivity = mmanager.getactivity ("All");
if (allactivity! = null && allactivity instanceof Settings) {
((Settings) allactivity). Invisibleonscreen ();
}
Break
}
}
}
@Override
public void onpagescrollstatechanged (int arg0) {
}
@Override
public void onpagescrolled (int arg0, float arg1, int arg2) {
}
@Override
public void onpageselected (int arg0) {
Switch (arg0) {
Case 0:
if (!moffenused.ischecked ())
Moffenused.setchecked (TRUE);
Mradiogroup.setbackgroundresource (R.drawable.settings_common);
Break
Case 1:
if (!mall.ischecked ())
Mall.setchecked (TRUE);
Mradiogroup.setbackgroundresource (R.drawable.settings_main);
Break
}
}
}
This is mainly in the code aboveBlueAndRedsection for a brief description, others are very basic parts I believe I do not need to repeat this.
The Red Code section mainly takes the current activity as a container so that other activity can be embedded as a child view. Note that the second parameter, the source of the explanation is @param singlemode Ture if the Localactivitymanager should keep a maximum of one activity resume. I understand it here. The approximate meaning of the child activity is then created if the Onresume method of all child activity is executed, and if true, only the Onresume () method of the last activity created in Viewpager is executed. (This conclusion is also confirmed that if this is true, then only the last child activity in Viewpager is called by the Onresume () method) so the actual argument we give here is false.
The reason for the blue part of the code is that the viewpager+activity way causes the life cycle function in the child activity to be disturbed, of course, the Onresume () method executes normally when the child activity is first created, but when we create the success, When you press the home key to switch the application to the background, the Onresume () method of the child activity is not called correctly when switching back to active state, but the activity in the outermost layer That is, the Onruesme () method in the Settingsactivity method here will be called normally, so this part of the code can solve the problem.
Offenusedsettings.java
/**
* Populate the activity with the top-level headers.
*/
@Override
public void Onbuildheaders (listif (!onishidingheaders ()) {
Pdebug.start ("Loadheadersfromresource");
Loadheadersfromresource (r.xml.offen_used_settings_headers, headers);
Pdebug.end ("Loadheadersfromresource");
Updateheaderlist (headers);
}
}
The above purple code is mostly loaded into different layout files. Other than the original settings.
So the code is explained here has been an abridged, so it is worth to say that, since Offenusedsettings.java is only modified to implement loading different layout files to achieve the requirements described above, why not use two settings as a sub-activity?
The answer is to be determined. We can completely use two settings as a sub-activity, just load the layout file to meet the requirements. I have already verified that, in order to make it easy for everyone to understand, it is implemented by copying settings. Interested friends can do their own implementation.
Implementation of Android4.4 system set page sliding browsing function