Android 左右侧滑示例。可以自已设置侧滑的效果,我在这里实现是左右都可以。侧滑出来的界面可以启动activity,或者加载view。侧滑效果是用了碎片规则来处理的。详细注释在代码里面,先看实现效果图,再看项目结构以及资源文件分布。代码只贴部分,提供本文源码下载链接。
源码下载:点击
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; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.main_left_fragment, container, false); initView(view); return view; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onResume() { super.onResume(); } private void initView(View view) { //要从getactivity获取 Context上下文句柄,用来加载其他需要的view布局 mInflater = LayoutInflater.from(getActivity()); view.findViewById(R.id.button1).setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: //在碎片处理中启动activity Intent toChatIntent = new Intent(getActivity(), TestActivity.class); startActivity(toChatIntent); break; default: break; } } }
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; @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } @Override public 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.findViewById TextView testTextView = (TextView)mTestView.findViewById(R.id.textView1); testTextView.setText("测试显示view"); } @Override public 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; } } @Override public void onOpen() { // TODO Auto-generated method stub } @Override public void onClose() { // TODO Auto-generated method stub } @Override public void onOpened() { // TODO Auto-generated method stub } @Override public void onClosed() { // TODO Auto-generated method stub mSlidingLayer.removeAllViews(); } }
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; @Override public 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_fragment FragmentTransaction mFragementTransaction = getSupportFragmentManager() .beginTransaction(); Fragment mFrag = new LeftFragment(); mFragementTransaction.replace(R.id.main_left_fragment, mFrag); mFragementTransaction.commit(); // customize the SlidingMenu DisplayMetrics 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_fragment FragmentTransaction 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); } @Override public 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; @Override public void onBackPressed() { // TODO Auto-generated method stub if (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(); } }
源码下载:点击
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/qq_16064871/article/details/49716629