Android slide examples

Source: Internet
Author: User

Android slide examples

Android slide examples. You can set the slide effect on your own. Here I can implement both the left and right sides. You can start the activity or load the view. The slide effect is handled by fragment rules. Detailed comments are included in the Code. Let's first look at the implementation, and then look at the project structure and resource file distribution. Only part of the code is pasted. The source code download link of this article is provided.

Download source code: Click

1. Implementation

2. Project Structure

3. Resource Distribution chart

4. LeftFragment

 

Package com.org. fragment; import com.org. activity. r; import com.org. activity. testActivity; import android. content. intent; import android. OS. bundle; import android. support. v4.app. fragment; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. view. view. onClickListener; public class extends Fragment implements OnClickListener {private LayoutInflater mInflater; @ Overridepublic View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle plugin) {View = inflater. inflate (R. layout. main_left_fragment, container, false); initView (view); return view ;}@ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState);} @ Overridepublic void onResume () {super. onResume ();} private void initView (View view) {// you need to obtain the Context handle from getactivity to load other view laminflater = LayoutInflater. from (getActivity (); view. findViewById (R. id. button1 ). setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. button1: // start activityIntent toChatIntent = new Intent (getActivity (), TestActivity in fragment processing. class); startActivity (toChatIntent); break; default: break ;}}}

 

5. RightFragment

 

 

Package com.org. fragment; import com.org. activity. r; import com.org. slidinglayer. slidingLayer; import com.org. slidinglayer. slidingLayer. onInteractListener; import android. OS. bundle; import android. support. v4.app. fragment; import android. view. layoutInflater; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup; import android. widget. textView; public class RightFragm Ent extends Fragment implements OnClickListener, OnInteractListener {private extends mInflater; private SlidingLayer mSlidingLayer; private View mTestView; @ Overridepublic void onCreate (Bundle attributes) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState);} @ Overridepublic View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View v Iew = inflater. inflate (R. layout. main_right_fragment, container, false); initView (view); return view;} private void initView (View view) {view. findViewById (R. id. button1 ). setOnClickListener (this); // implementation class mSlidingLayer = (SlidingLayer) view of page sliding in RightFragment. findViewById (R. id. right_sliding_layer); mSlidingLayer. setOnInteractListener (this); // get the Context handle mInflater = LayoutInflater from getactivity. from (getAc Tietong (); // display the view page of the test. The mTestView = mInflater page of the view is loaded through LayoutInflater. inflate (R. layout. activity_test, null); // The mTestView of the user. findViewByIdTextView testTextView = (TextView) mTestView. findViewById (R. id. textView1); testTextView. setText (test display view);} @ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. button1: mSlidingLayer. removeAllViews (); // first remove all views. Otherwise, an if (! MSlidingLayer. isOpened () {mSlidingLayer. addView (mTestView); mSlidingLayer. openLayer (true);} break; default: break; }}@ Overridepublic void onOpen () {// TODO Auto-generated method stub} @ Overridepublic void onClose () {// TODO Auto-generated method stub} @ Overridepublic void onOpened () {// TODO Auto-generated method stub} @ Overridepublic void onClosed () {// TODO Auto-generated method stubmSlidingLayer. removeAllViews ();}}

 

6. MainActivity

 

 

Package com.org. activity; import com.org. fragment. leftFragment; import com.org. fragment. rightFragment; import com.org. slidingmenu. baseSlidingFragmentActivity; import com.org. slidingmenu. slidingMenu; import android. content. context; import android. OS. bundle; import android. support. v4.app. fragment; import android. support. v4.app. fragmentTransaction; import android. util. displayMetrics; import android. view. view; im Port android. view. view. onClickListener; import android. view. window; import android. widget. button; import android. widget. toast; public class MainActivity extends BaseSlidingFragmentActivity implements OnClickListener {protected SlidingMenu mSlidingMenu; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); initSlidingMen U (); setContentView (R. layout. activity_main); initView (savedInstanceState);} private void initSlidingMenu () {// This is set. Press the key to implement the default layout for loading and replace it with fragments, only in this way can LeftFragment control setBehindContentView (R. layout. main_left_layout); // set the left menu // This is a fragment processing rule, replace the main_left_fragment layout, and implement this layout through the left slide. // if this is not available, the loading layout is only the default main_left_fragmentFragmentTransaction mFragementTransaction = getSupportFragmentManager (). beginTransaction (); Fragment mFr Ag = new LeftFragment (); mFragementTransaction. replace (R. id. main_left_fragment, mFrag); mFragementTransaction. commit (); // customize the SlidingMenuDisplayMetrics dm = new DisplayMetrics (); getWindowManager (). getdefadisplay display (). getMetrics (dm); int mScreenWidth = dm. widthPixels; // obtain the screen resolution width mSlidingMenu = getSlidingMenu (); mSlidingMenu. setMode (SlidingMenu. LEFT_RIGHT); // you can set whether to slide left or right or left or right. mSlidi can be slide right or left. NgMenu. setShadowWidth (mScreenWidth/40); // set the shadow width mSlidingMenu. setBehindOffset (mScreenWidth/8); // set the menu width mSlidingMenu. setFadeDegree (0.35f); // set the percentage of mSlidingMenu to fade in and out. setTouchModeAbove (sregistringmenu. TOUCHMODE_MARGIN); mSlidingMenu. setShadowDrawable (R. drawable. slidingmenu_shadow); // sets the left menu shadow image mSlidingMenu. setSecondaryShadowDrawable (R. drawable. right_shadow); // set the right menu shadow image mSlidingMenu. setFadeEnabled (true );// Set whether to fade in or out mSlidingMenu when sliding. setBehindScrollScale (0.333f); // set the drag-and-drop effect when sliding} private void initView (Bundle savedInstanceState) {// set the default layout for loading by pressing the key, then replace mSlidingMenu with fragments. setSecondaryMenu (R. layout. main_right_layout); // This is a fragment processing rule. Replace the main_right_fragment layout and implement this layout by right sliding. // if this is not available, the loading layout is only the default javasmfragementtransaction = getsuppfrfragmentmanager (). beginTransaction (); Fragment mFrag = New RightFragment (); mFragementTransaction. replace (R. id. main_right_fragment, mFrag); mFragementTransaction. commit (); (Button) findViewById (R. id. buttonLeft )). setOnClickListener (this); (Button) findViewById (R. id. buttonRight )). setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. buttonLeft: mSlidingMenu. showMenu (true); break; case R. id. buttonRight: mSlidingMenu. sh OwSecondaryMenu (true); break; default: break;}/*** press the return key twice in a row to exit */private long firstTime; @ Overridepublic void onBackPressed () {// TODO Auto-generated method stubif (System. currentTimeMillis ()-firstTime <3000) {finish ();} else {firstTime = System. currentTimeMillis (); showShort (this, quit again !);}} Static Toast toast; public static void showShort (Context context, String message) {if (null = toast) {toast = Toast. makeText (context, message, Toast. LENGTH_SHORT); // toast. setGravity (Gravity. CENTER, 0, 0);} else {toast. setText (message);} toast. show ();}}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.