android实现左右侧滑菜单效果

 更新时间:2017年10月10日 10:31:02   作者:人走丿茶凉  
这篇文章主要为大家详细介绍了android实现左右侧滑菜单效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

在android开发中,左右侧滑菜单的开发已成为我们现在开发的必备技术之一,再次之前,我没有做过相类似的demo,但是项目的开发有要求有这样的效果,而且大家都知道,虽然网上由开源的代码,但是不仅种类多,看着一个头两个大,而且代码不好分离。因此我们无法简化成自己的demo,为此,还查阅了很多别人的资料,最后做出了自己想要的效果,具体效果如下所示:

图1 左边菜单

这里写图片描述 

图2 右边菜单

这里写图片描述

今天要做的是把两个效果结合在一起,左右侧滑菜单

话不多说,直接上代码:

activity_main.xml:

<LinearLayout 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"
 android:orientation="vertical" >

 <android.support.v4.widget.DrawerLayout
 android:id="@+id/dl"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_above="@+id/tv" >

 <!-- 作为侧拉菜单 主页面显示的效果 要写在布局的最上面 首先进行加载 -->
 <FrameLayout
  android:id="@+id/fl"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >
 </FrameLayout>

 <ListView
  android:id="@+id/lv"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#ff0"
  android:layout_gravity="left" >
 </ListView>

 <LinearLayout
  android:id="@+id/ll"
  android:layout_width="200dp"
  android:layout_height="match_parent"
  android:layout_gravity="right"
  android:background="#0ff"
  android:orientation="vertical" >

  <ImageView
  android:layout_width="100dp"
  android:layout_height="100dp"
  android:src="@drawable/ic_launcher" />

  <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_margin="20dp"
  android:text="呵呵呵" />
 </LinearLayout>
 </android.support.v4.widget.DrawerLayout>

</LinearLayout>


frag_main.xml:

<LinearLayout 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"
 android:gravity="center"
 android:orientation="vertical" >

 <TextView
 android:id="@+id/tv_title"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_margin="20dp"
 android:text="标题" />


 <ImageView
 android:id="@+id/iv"
 android:layout_width="100dp"
 android:layout_height="100dp"
 android:src="@drawable/ic_launcher" />

</LinearLayout>

MainActivity.java:

import java.util.ArrayList;
import java.util.List;

import com.example.day12drawerlayout1.fragment.MainFragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * 1、静态和动态Fragment的使用
 * 静态 直接在布局中使用<fragment />
 * 动态 使用管理器 得到一个事务 然后使用事务调用replace方法 把一个Fragment对象替换到指定id的FramLayout帧布局中
 * @author Administrator
 *
 */
public class MainActivity extends FragmentActivity {

 DrawerLayout dl;
 ListView lv;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 dl = (DrawerLayout) findViewById(R.id.dl);
 // FrameLayout fl = (FrameLayout) findViewById(R.id.fl);
 // fl.setOnClickListener(new OnClickListener() {
 //  
 //  @Override
 //  public void onClick(View v) {
 //  // TODO Auto-generated method stub
 //  dl.openDrawer(Gravity.RIGHT);
 //  }
 // });

 /**
  * set 一般是只有一个 如果再次调用会把前面的覆盖掉
  * 和 
  * add 把数据添加进去 不会覆盖之前的内容
  */
 dl.addDrawerListener(new DrawerListener() {

  //滑动状态发生改变的时候 会调用该方法
  @Override
  public void onDrawerStateChanged(int arg0) {
  // TODO Auto-generated method stub
  Log.i("===============================", "StateChanged" + arg0);
  }

  //监听滑动过程中 边界的位置
  @Override
  public void onDrawerSlide(View arg0, float arg1) {
  // TODO Auto-generated method stub
  Log.i("===============================", "DrawerSlide" + arg1);
  }

  //监听侧拉是否完全展开
  @Override
  public void onDrawerOpened(View arg0) {
  // TODO Auto-generated method stub
  Log.i("===============================", "DrawerOpened");
  }

  //监听侧拉是否被关闭
  @Override
  public void onDrawerClosed(View arg0) {
  // TODO Auto-generated method stub
  Log.i("===============================", "DrawerClosed");
  }
 });

 showMain();
 showLV();
 }

 public DrawerLayout getDL(){
 return dl;
 }

 private void showLV() {
 lv = (ListView) findViewById(R.id.lv);
 final List<String> list = new ArrayList<String>();
 for (int i = 1; i < 30; i++) {
  list.add("条目"+i);
 }
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
 lv.setAdapter(adapter);

 lv.setOnItemClickListener(new OnItemClickListener() {

  @Override
  public void onItemClick(AdapterView<?> parent, View view,
   int position, long id) {
  // TODO Auto-generated method stub
  dl.closeDrawer(Gravity.LEFT);
  //把点击的listview控件中的值 赋值到主Fragment对象中
  MainFragment fragment = (MainFragment) getSupportFragmentManager().findFragmentByTag("main");
  fragment.setData(list.get(position));
  }
 });
 }

 /**
 * 在侧拉效果的页面中 用来显示主页面的效果
 */
 private void showMain() {
 //动态加载Fragment
 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
 //参数1:FramLayout控件的id, 要替换的Fragment对象
 transaction.replace(R.id.fl, new MainFragment(), "main");
 transaction.commit();
 }

}

MainFragment.java:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.day12drawerlayout1.MainActivity;
import com.example.day12drawerlayout1.R;

public class MainFragment extends Fragment{
 TextView tv;
 ImageView iv;

 @Override
 @Nullable
 public View onCreateView(LayoutInflater inflater,
  @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

 View view = View.inflate(getActivity(), R.layout.frag_main, null);

 tv = (TextView) view.findViewById(R.id.tv_title);
 iv = (ImageView) view.findViewById(R.id.iv);

 iv.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {
  // TODO Auto-generated method stub
  MainActivity activity = (MainActivity) getActivity();
  activity.getDL().openDrawer(Gravity.RIGHT);
  }
 });

 return view;
 }

 public void setData(String str){
 tv.setText(str);
 }
}

更多学习内容,可以点击《Android侧滑效果汇总》学习。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android架构发展进化详解

    Android架构发展进化详解

    Android系统架构从上到下分为五层:应用层、应用框架层、系统运行库层、硬件抽象层、Linux内核层,Android架构也经历了多次演进,下面我们来详细了解一下
    2022-08-08
  • Android实现检测实体按键事件并屏蔽

    Android实现检测实体按键事件并屏蔽

    这篇文章主要介绍了Android实现检测实体按键事件并屏蔽 ,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • 一款Android APK的结构构成解析

    一款Android APK的结构构成解析

    本篇文章介绍了我在学习过程中对于Android 程序的理解总结,刨析了apk的组成与产生过程,通读本篇对大家的学习或工作具有一定的价值,需要的朋友可以参考下
    2021-10-10
  • Android EditText长按菜单中分享功能的隐藏方法

    Android EditText长按菜单中分享功能的隐藏方法

    Android EditText控件是经常使用的控件,下面这篇文章主要给大家介绍了关于Android中EditText长按菜单中分享功能的隐藏方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-02-02
  • Python基础教程学习笔记 第二章 列表和元组

    Python基础教程学习笔记 第二章 列表和元组

    这篇文章主要介绍了Python基础教程学习笔记 第二章 列表和元组,需要的朋友可以参考下
    2015-03-03
  • Android Studio 利用Splash制作APP启动界面的方法

    Android Studio 利用Splash制作APP启动界面的方法

    这篇文章主要介绍了Android Studio 利用Splash制作APP启动界面,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-05-05
  • Android多返回栈技术

    Android多返回栈技术

    本文将详情讲解用户通过系统返回按钮导航回去的一组页面,在开发中被称为返回栈 (back stack)。多返回栈即一堆 "返回栈",对多返回栈的支持是在 Navigation 2.4.0-alpha01 和 Fragment 1.4.0-alpha01 中开始的,有兴趣的话一起参与学习
    2021-08-08
  • Android实战教程第七篇之如何在内存中存储用户名和密码

    Android实战教程第七篇之如何在内存中存储用户名和密码

    这篇文章主要为大家详细介绍了Android如何实现在内存中存储用户名和密码的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • android自定义popupwindow仿微信右上角弹出菜单效果

    android自定义popupwindow仿微信右上角弹出菜单效果

    这篇文章主要为大家详细介绍了android自定义popupwindow仿微信右上角弹出菜单效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • Android 手机屏幕适配解决办法

    Android 手机屏幕适配解决办法

    这篇文章主要介绍了Android 手机屏幕适配的相关资料,在开发Android 手机开发的时候经常会有很多手机品牌和手机屏幕尺寸问题,需要的朋友可以参考下
    2016-10-10

最新评论