Android 左右側滑樣本

來源:互聯網
上載者:User

標籤:結構   源碼   布局   fragment   

Android 左右側滑樣本。可以自已設定側滑的效果,我在這裡實現是左右都可以。側滑出來的介面可以啟動activity,或者載入view。側滑效果是用了片段規則來處理的。詳細注釋在代碼裡面,先看實現,再看項目結構以及資源檔分布。代碼只貼部分,提供本文源碼下載連結。

源碼下載:點擊

1、實現


2、項目結構圖


3、資源分布圖


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 LeftFragment extends Fragment implements OnClickListener{private LayoutInflater mInflater;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View 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) {//要從getactivity擷取 Context上下文控制代碼,用來載入其他需要的view布局mInflater = LayoutInflater.from(getActivity());view.findViewById(R.id.button1).setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.button1://在片段處理中啟動activityIntent toChatIntent = new Intent(getActivity(),TestActivity.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 RightFragment extends Fragment implements OnClickListener ,OnInteractListener{private LayoutInflater mInflater;private SlidingLayer mSlidingLayer;private View mTestView;@Overridepublic void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = 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);//RightFragment裡面的頁面的滑動的實作類別mSlidingLayer = (SlidingLayer) view.findViewById(R.id.right_sliding_layer);mSlidingLayer.setOnInteractListener(this);//要從getactivity擷取 Context上下文控制代碼mInflater = LayoutInflater.from(getActivity());//顯示測試的view頁面,通過LayoutInflater載入view的頁面mTestView = mInflater.inflate(R.layout.activity_test, null);//是誰的view就用誰的mTestView.findViewByIdTextView testTextView = (TextView)mTestView.findViewById(R.id.textView1);testTextView.setText("測試顯示view");}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.button1:mSlidingLayer.removeAllViews();// 先移除所有的view,不然會報錯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;import 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);initSlidingMenu();setContentView(R.layout.activity_main);initView(savedInstanceState);}private void initSlidingMenu() {//這個是設定進去,通過按鍵實現載入的預設布局,然後用片段替換掉 ,只有這樣才有LeftFragment控制setBehindContentView(R.layout.main_left_layout);// 設定左菜單//這個都是片段處理規則,替換掉main_left_fragment布局,通過左滑實現此布局//若是沒有這個,載入布局只是預設的main_left_fragmentFragmentTransaction mFragementTransaction = getSupportFragmentManager().beginTransaction();Fragment mFrag = new LeftFragment();mFragementTransaction.replace(R.id.main_left_fragment, mFrag);mFragementTransaction.commit();// customize the SlidingMenuDisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);int mScreenWidth = dm.widthPixels;// 擷取螢幕解析度寬度mSlidingMenu = getSlidingMenu();mSlidingMenu.setMode(SlidingMenu.LEFT_RIGHT);// 設定是左滑還是右滑,還是左右都可以滑,我這裡左右都可以滑mSlidingMenu.setShadowWidth(mScreenWidth / 40);// 設定陰影寬度mSlidingMenu.setBehindOffset(mScreenWidth / 8);// 設定菜單寬度mSlidingMenu.setFadeDegree(0.35f);// 設定淡入淡出的比例mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);mSlidingMenu.setShadowDrawable(R.drawable.slidingmenu_shadow);// 設定左菜單陰影圖片mSlidingMenu.setSecondaryShadowDrawable(R.drawable.right_shadow);// 設定右菜單陰影圖片mSlidingMenu.setFadeEnabled(true);// 設定滑動時菜單的是否淡入淡出mSlidingMenu.setBehindScrollScale(0.333f);// 設定滑動時拖拽效果}private void initView(Bundle savedInstanceState) {//這個是設定進去,通過按鍵實現載入的預設布局,然後用片段替換掉mSlidingMenu.setSecondaryMenu(R.layout.main_right_layout);//這個都是片段處理規則,替換掉main_right_fragment布局,通過右滑實現此布局//若是沒有這個,載入布局只是預設的main_right_fragmentFragmentTransaction mFragementTransaction = getSupportFragmentManager().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.showSecondaryMenu(true);break;default:break;}}/** * 連續按兩次返回鍵就退出 */private long firstTime;@Overridepublic void onBackPressed() {// TODO Auto-generated method stubif (System.currentTimeMillis() - firstTime < 3000) {finish();} else {firstTime = System.currentTimeMillis();showShort(this, "再按一次退出!");}}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();}}

源碼下載:點擊


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Android 左右側滑樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.