android的左右側滑選單實現
【原文:http://blog.csdn.net/cuiran/article/details/11973155】
最近看了很多app應用都採用的是左右側滑,比如網易新聞、凡客等
這裡也試著寫一下側滑
首先看一下效果
然後給出xml佈局程式碼
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity" >
- <LinearLayout
- android:id="@+id/layout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal">
- <LinearLayout
- android:id="@+id/leftMenu"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/left_text" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/content"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
- <RelativeLayout
- android:layout_width="fill_parent"
- android:id="@+id/layout01"
- android:layout_height="wrap_content"
- android:background="@drawable/bg_title"
- android:layout_alignParentTop="true"
- android:gravity="center"
- android:orientation="horizontal">
- <ImageView
- android:id="@+id/leftBtn"
- android:layout_alignParentLeft="true"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:padding="10dp"
- android:src="@drawable/home_category"/>
- <ImageView
- android:id="@+id/leftIcon"
- android:layout_toRightOf="@+id/leftBtn"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:src="@drawable/title_icon_line"/>
- <TextView
- android:id="@+id/home_resource"
- android:text="@string/app_name"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textColor="@color/white"
- android:textSize="@dimen/title_size"
- android:textStyle="bold"
- android:padding="10dp"
- android:gravity="center"/>
- <ImageView
- android:id="@+id/rightIcon"
- android:layout_toLeftOf="@+id/rightBtn"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:src="@drawable/title_icon_line"/>
- <ImageView
- android:id="@+id/rightBtn"
- android:padding="10dp"
- android:layout_alignParentRight="true"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:src="@drawable/home_myvancl"
- />
- </RelativeLayout>
- </LinearLayout>
- <LinearLayout
- android:id="@+id/rightMenu"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/right_text" />
- </LinearLayout>
- </LinearLayout>
- </RelativeLayout>
接著寫Activity
- package com.example.viewdemo;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.app.Activity;
- import android.util.Log;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.LinearLayout.LayoutParams;
- /**
- *
- * TODO
- * @author cuiran
- * @version 1.0.0
- */
- public class MainActivity extends Activity implements OnClickListener{
- private static final String TAG="MainActivity";
- private LinearLayout leftMenu;
- private LinearLayout content;
- private LinearLayout rightMenu;
- private LayoutParams leftMenuParams;
- private LayoutParams contentParams;
- private LayoutParams rightMenuParams;
- // menu完全顯示時,留給content的寬度值。
- private static final int menuPadding = 120;
- private ImageView leftBtn,rightBtn;
- // 解析度
- private int disPlayWidth;
- private boolean mIsShow = false;
- private boolean mIsRightShow = false;
- private static final int speed = 50;
- public boolean isMenu=false;
- public boolean isRightMenu=false;
- @SuppressWarnings("deprecation")
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initView();
- }
- /**初始化檢視
- * TODO
- */
- private void initView() {
- // TODO Auto-generated method stub
- disPlayWidth = getWindowManager().getDefaultDisplay().getWidth();
- leftMenu = (LinearLayout) findViewById(R.id.leftMenu);
- leftMenu.setOnClickListener(this);
- content = (LinearLayout) findViewById(R.id.content);
- content.setOnClickListener(this);
- rightMenu = (LinearLayout) findViewById(R.id.rightMenu);
- rightMenu.setOnClickListener(this);
- leftMenuParams = (LayoutParams) leftMenu.getLayoutParams();
- contentParams = (LayoutParams) content.getLayoutParams();
- rightMenuParams= (LayoutParams) rightMenu.getLayoutParams();
- leftMenuParams.width = disPlayWidth - menuPadding;
- contentParams.width = disPlayWidth;
- rightMenuParams.width = disPlayWidth - menuPadding;
- leftBtn=(ImageView)findViewById(R.id.leftBtn);
- rightBtn=(ImageView)findViewById(R.id.rightBtn);
- leftBtn.setOnClickListener(this);
- rightBtn.setOnClickListener(this);
- showMenu(mIsShow);
- showRightMenu(mIsRightShow);
- }
- /**
- * 顯示介面
- * TODO
- * @param isShow
- */
- private void showMenu(boolean isShow)
- {
- if (isShow)
- {
- mIsShow = true;
- leftMenuParams.leftMargin = 0;
- } else
- {
- mIsShow = false;
- leftMenuParams.leftMargin = 0 - leftMenuParams.width;
- }
- leftMenu.setLayoutParams(leftMenuParams);
- }
- private void showRightMenu(boolean isShow)
- {
- if (isShow)
- {
- mIsRightShow = true;
- contentParams.leftMargin = 0 ;
- } else
- {
- mIsRightShow = false;
- contentParams.leftMargin = 0;
- }
- content.setLayoutParams(contentParams);
- }
- /**
- *
- * TODO 控制左側View
- * @author cuiran
- * @version 1.0.0
- */
- class showMenuAsyncTask extends AsyncTask<Integer, Integer, Integer>
- {
- @Override
- protected Integer doInBackground(Integer... params)
- {
- int leftMargin =leftMenuParams.leftMargin;
- Log.i(TAG, "leftMargin="+leftMargin);
- while (true)
- {
- leftMargin += params[0];
- if (params[0] > 0 && leftMargin >= 0)
- {
- break;
- } else if (params[0] < 0 && leftMargin <= -leftMenuParams.width)
- {
- break;
- }
- Log.i(TAG, "doInBackground:leftMargin"+leftMargin);
- publishProgress(leftMargin);
- try
- {
- Thread.sleep(30);
- } catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- return leftMargin;
- }
- @Override
- protected void onProgressUpdate(Integer... values)
- {
- super.onProgressUpdate(values);
- leftMenuParams.leftMargin = values[0];
- leftMenu.setLayoutParams(leftMenuParams);
- }
- @Override
- protected void onPostExecute(Integer result)
- {
- super.onPostExecute(result);
- leftMenuParams.leftMargin = result;
- leftMenu.setLayoutParams(leftMenuParams);
- }
- }
- /**
- *
- * TODO 控制右側View
- * @author cuiran
- * @version 1.0.0
- */
- class showRightMenuAsyncTask extends AsyncTask<Integer, Integer, Integer>
- {
- @Override
- protected Integer doInBackground(Integer... params)
- {
- int leftMargin =contentParams.leftMargin;
- while (true)
- {
- leftMargin += params[0];
- if (params[0] > 0 && leftMargin >= 0)
- {
- break;
- } else if (params[0] < 0 && leftMargin <= -contentParams.width+menuPadding)
- {
- break;
- }
- publishProgress(leftMargin);
- try
- {
- Thread.sleep(30);
- } catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- return leftMargin;
- }
- @Override
- protected void onProgressUpdate(Integer... values)
- {
- super.onProgressUpdate(values);
- contentParams.leftMargin = values[0];
- content.setLayoutParams(contentParams);
- }
- @Override
- protected void onPostExecute(Integer result)
- {
- Log.i(TAG, "onPostExecute");
- super.onPostExecute(result);
- contentParams.leftMargin = result;
- content.setLayoutParams(contentParams);
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- /* (non-Javadoc)
- * @see android.view.View.OnClickListener#onClick(android.view.View)
- */
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- switch (v.getId())
- {
- case R.id.leftMenu:
- isMenu=false;
- new showMenuAsyncTask().execute(-speed);
- break;
- case R.id.leftBtn:
- if(isMenu){
- isMenu=false;
- new showMenuAsyncTask().execute(-speed);
- }else{
- isMenu=true;
- new showMenuAsyncTask().execute(speed);
- }
- break;
- case R.id.rightBtn:
- Log.i(TAG, "點選rightBtn");
- if(isRightMenu){
- isRightMenu=false;
- new showRightMenuAsyncTask().execute(speed);
- }else{
- isRightMenu=true;
- new showRightMenuAsyncTask().execute(-speed);
- }
- break;
- case R.id.content:
- break;
- }
- }
- }
原始碼下載地址為:https://github.com/cayden/viewdemo
相關文章
- 自定義ViewGroup,實現Android的側滑選單ViewAndroid
- 原生Android 側滑選單實踐(部分)Android
- 自定義View:側滑選單實現View
- Android側滑選單DrawerLayout使用Android
- 自定義View:側滑選單動畫實現View動畫
- css3實現側邊滑動選單CSSS3
- AndroidDrawerLayout+fragment佈局實現左右側滑AndroidFragment
- 利用DrawerLayout實現側滑選單學習總結
- (有圖)仿QQ側滑選單:RecyclerView側滑選單,長按拖拽,滑動刪除View
- Android左右滑動效果的程式碼實現Android
- Android SlidingMenu側滑選單使用介紹Android
- Swift - 仿寫QQ側滑選單Swift
- Flutter 側滑欄及城市選擇UI的實現FlutterUI
- Android導航選單橫向左右滑動並和下方的控制元件實現聯動Android控制元件
- Flutter:手把手教你實現一個仿QQ側滑選單的功能Flutter
- 短視訊平臺開發,依靠DrawerLayout實現側滑選單效果
- Activity側滑返回的實現原理
- 自定義view——仿酷狗的側滑選單View
- Vue實現左右選單聯動實現(更新)Vue
- Android側滑返回分析和實現(不高仿微信)Android
- 實現 UITableViewCell 側滑操作列表UIView
- Flutter | 超簡單仿微信QQ側滑選單元件Flutter元件
- jQuery實現左側分類選單jQuery
- UI介面微信底部(ViewPager實現Tab,左右滑動+底部點選)UIViewpager
- 【只發精品】匠心打造Vue側滑選單元件Vue元件
- 自定義控制元件?試試300行程式碼實現QQ側滑選單控制元件行程
- ItemTouchHelper實現可拖拽和側滑的列表
- 帶有視覺滾動差的選單側滑欄視覺
- 電商左側商品分類選單實現
- TornadoFx實現側邊欄選單效果
- Android 禁止ViewPager左右滑動AndroidViewpager
- Flutter 仿iOS側滑返回案例實現FlutteriOS
- 自定義RecyclerView實現側滑刪除View
- Android最簡單的側劃選單,DrawerLayout的使用Android
- 微信小程式實現卡片左右滑動效果微信小程式
- ViewPager實現左右無限迴圈滑動Viewpager
- (十)如果實現滑動展示選單效果
- 讓web擁有原生手機側滑選單那種順滑外掛Web