Open-source database Fab-Transformation simple use of parsing, transformation
Reprinted, please indicate the path of the great ox from Wang Xiaoying
Similar to the IPhone's floating button operation, the operation is only fixed. Of course, after modification, it can also be changed. Here, we just want to give the party a sense of well-being and add some understanding of ourselves, let everyone use it. Once you read it, you can understand it. If you don't talk nonsense, go first !!
The implementation of the author is roughly like this. After reading the Sample used, I will conduct a small experiment on some of the content, and I will also offer it later.
Directory structure
Gradle builds a project (Git is like this, everyone knows) An SmpleModule and a Lib. for the party, we are more concerned with the specific operation code, there is time for implementation.
I mentioned the content in Gradle, because the author used many labels similar to Java Web development to solve the problem of FindViewById and a series of OnclickListener operations, hi, it's full-saving code. I'll post it. (It is also a third-party library. I have seen it before, but I have not had time to study it carefully)
apply plugin: 'com.android.application'android { compileSdkVersion COMPILE_SDK_VERSION as int buildToolsVersion BUILD_TOOLS_VERSION defaultConfig { minSdkVersion MIN_SDK_VERSION as int targetSdkVersion TARGET_SDK_VERSION as int versionCode VERSION_CODE as int versionName VERSION_NAME renderscriptTargetApi RENDERSCRIPT_TARGET_API as int renderscriptSupportModeEnabled true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }}dependencies { compile project(':fab-transformation') compile "com.android.support:appcompat-v7:${SUPPORT_PACKAGE_VERSION}" compile "com.android.support:cardview-v7:${SUPPORT_PACKAGE_VERSION}" compile 'de.hdodenhof:circleimageview:1.3.0' compile 'com.android.support:design:22.2.1' compile 'com.jakewharton:butterknife:6.1.0'}
It is compile 'com. jakewharton: butterknife: 6.1.0 '. You can search this library on Git, which is quite practical and recommended.
The models and adapters packages in the Directory won't be explained. The original author's layered understanding is quite good when it is used to fill data and build pages.
AppUtil and BaseActivity are only auxiliary classes and are not pasted. For details about the old rules of online storage, see! (Recently, I have been considering whether to store Git or CSDN .)
Main Activity
Public class MainActivity extends BaseActivity {private static final String SPEC_URL = "http://www.google.com/design/spec/components/buttons-floating-action-button.html#"; @ InjectView (R. id. list_view) ListView; private MenuArrayAdapter adapter; private static void showWebPage (String url, Context context) {if (TextUtils. isEmpty (url) return; Uri uri = Uri. parse (url); Intent intent = new Intent (Intent. ACTION_VIEW, uri); context. startActivity (intent) ;}@ Override int getLayoutResId () {return R. layout. activity_main ;}@ Override protected void initView () {adapter = new MenuArrayAdapter (this); listView. setAdapter (adapter); adapter. addAll (createMenuList ();}/* fill in the homepage ListView data source */private List <SampleMenu> createMenuList () {List <SampleMenu> menuList = new ArrayList <> (2 ); menuList. add (new SampleMenu (getString (R. string. description_sheet), R. drawable. img_thumb_sheet); menuList. add (new SampleMenu (getString (R. string. description_toolbar), R. drawable. img_thumb_toolbar); menuList. add (new SampleMenu (getString (R. string. description_player), R. drawable. img_thumb_player); return menuList;} @ OnItemClick (R. id. list_view) void onItemClickListView (int position) {SampleMenu sampleMenu = adapter. getItem (position); switch (position) {case 0: TransformToSheetActivity. start (this, sampleMenu. getTitle (); break; case 1: TransformToToolbarActivity. start (this, sampleMenu. getTitle (); break; case 2: TransformToPlayerActivity. start (this, sampleMenu. getTitle (); break ;}@override public boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. menu_main, menu); return true;} @ Override public boolean onOptionsItemSelected (MenuItem item) {int id = item. getItemId (); switch (id) {case R. id. action_link: showWebPage (SPEC_URL, this); break;} return super. onOptionsItemSelected (item );}}
Analysis:
1. The SPEC_URL constant is only a Url used to access the network on the Menu. The Menu function is also to simply open a connection, which can be ignored.
2. createMenuList () fills in the data source used by Item, and the Title of each subsequent page also comes from this
3. Here, tags such as @ OnItemClick and @ InjectView can be used to replace some of the original complicated code. This operation is highly recommended and easy to read.
4. The author makes some Intent redirects the start () method of each small class, and then performs some jump operations, which is much simpler than three new Intent () operations in MainActivity.
TransformToPlayerActivity
public class TransformToPlayerActivity extends BaseActivity { @InjectView(R.id.fab) FloatingActionButton fab; @InjectView(R.id.container_player) View containerPlayer; public static void start(Context context, String title) { Intent intent = new Intent(context, TransformToPlayerActivity.class); intent.putExtra(EXTRA_TITLE, title); context.startActivity(intent); } @Override int getLayoutResId() { return R.layout.activity_transform_to_player; } @Override protected void initView() { // } @OnClick(R.id.fab) void onClickFab() { if (fab.getVisibility() == View.VISIBLE) { FabTransformation.with(fab).transformTo(containerPlayer); } } @OnClick(R.id.container_player) void onClickSheet() { if (fab.getVisibility() != View.VISIBLE) { FabTransformation.with(fab).transformFrom(containerPlayer); } } @Override public void onBackPressed() { if (fab.getVisibility() != View.VISIBLE) { FabTransformation.with(fab).transformFrom(containerPlayer); return; } super.onBackPressed(); }}
Analysis:
1. getLayoutResId () and initView () are all methods that can be used after inheriting BaseActivity for initialization. getLayoutResId () Here is equivalent to setContentView (), he only performed a round of encapsulation, such as setContentView (getLayoutResId ());
2. all the operations to activate or hide the fab only require FabTransformation. with (fab ). transformTo (containerPlayer); The encapsulation is thorough. The first parameter is the View with penalty effect, and the second parameter is the View to be presented, if you want to continue the operation in the View, you only need @ OnClick (R. id. specific controls), even do not need to be in the previous @ InjectView (R. id. specific controls), very simple and practical.
TransformToSheetActivity
public class TransformToSheetActivity extends BaseActivity { @InjectView(R.id.fab) FloatingActionButton fab; @InjectView(R.id.sheet) View sheet; @InjectView(R.id.overlay) View overlay; public static void start(Context context, String title) { Intent intent = new Intent(context, TransformToSheetActivity.class); intent.putExtra(EXTRA_TITLE, title); context.startActivity(intent); } @Override int getLayoutResId() { return R.layout.activity_transform_to_sheet; } @Override protected void initView() { // } @OnClick(R.id.fab) void onClickFab() { if (fab.getVisibility() == View.VISIBLE) { FabTransformation.with(fab).setOverlay(overlay).transformTo(sheet); } } @OnClick(R.id.row_1) void onClickRow1() { AppUtil.showToast("Row 1 clicked!", this); } @OnClick(R.id.row_2) void onClickRow2() { AppUtil.showToast("Row 2 clicked!", this); } @OnClick(R.id.row_3) void onClickRow3() { AppUtil.showToast("Row 3 clicked!", this); } @OnClick(R.id.row_4) void onClickRow4() { AppUtil.showToast("Row 4 clicked!", this); } @OnClick(R.id.row_5) void onClickRow5() { AppUtil.showToast("Row 5 clicked!", this); } @OnClick(R.id.overlay) void onClickOverlay() { if (fab.getVisibility() != View.VISIBLE) { FabTransformation.with(fab).setOverlay(overlay).transformFrom(sheet); } } @Override public void onBackPressed() { if (fab.getVisibility() != View.VISIBLE) { FabTransformation.with(fab).setOverlay(overlay).transformFrom(sheet); return; } super.onBackPressed(); }}
Analysis:
The difference between this class and the previous class is that we can add the specific things we just mentioned, and only need @ OnClick (R. id. row_1)
Void onClickRow1 () {specific service} is just fine, and the points are simple.
TransformToToolbarActivity
Public class TransformToToolbarActivity extends BaseActivity {@ InjectView (R. id. fab) FloatingActionButton fab; @ InjectView (R. id. toolbar_footer) View toolbarFooter; @ InjectView (R. id. scroll_view) ScrollView scrollView; private boolean isTransforming; public static void start (Context context, String title) {Intent intent = new Intent (context, TransformToToolbarActivity. class); intent. putExtra (EXTRA_T ITLE, title); context. startActivity (intent) ;}@ Override int getLayoutResId () {return R. layout. activity_transform_to_toolbar;} @ Override protected void initView () {scrollView. getViewTreeObserver (). addOnScrollChangedListener (new ViewTreeObserver. onScrollChangedListener () {@ Override public void onScrollChanged () {if (fab. getVisibility ()! = View. VISIBLE &&! IsTransforming) {FabTransformation. with (fab ). setListener (new FabTransformation. onTransformListener () {@ Override public void onStartTransform () {isTransforming = true ;}@ Override public void onEndTransform () {isTransforming = false ;}}). transformFrom (toolbarFooter) ;}}) ;}@ OnClick (R. id. fab) void onClickFab () {if (fab. getVisibility () = View. VISIBLE) {FabTransformation. with (fab ). transfo RmTo (toolbarFooter); }}@ OnClick (R. id. imagebutton) void onClickImageButton () {Toast. makeText (TransformToToolbarActivity. this, "Xu xyi's brain has holes", Toast. LENGTH_SHORT ). show () ;}@ Override public void onBackPressed () {if (fab. getVisibility ()! = View. VISIBLE) {FabTransformation. with (fab). transformFrom (toolbarFooter); return ;}super. onBackPressed ();}}
Analysis:
1. this section is in a ScrollView, so when you have operations to go up and down, it will also affect our toolbarFooter menu, therefore, a listener event is added to process the business.
2. The OnClick event is added to the first Image of this toolbarFooter to check whether there is a toast. The effect is yes, so our task is completed.
I 've been writing for a day, and I'm a little tired, hey.
Source Code address: http://yunpan.cn/cdmBGVw4PfFry access password 135e
If you have any questions or cooperate, please contact QQ 452270579. Thank you.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.