前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >android实现左右侧滑菜单效果

android实现左右侧滑菜单效果

作者头像
砸漏
发布2020-10-26 16:34:05
1.4K0
发布2020-10-26 16:34:05
举报
文章被收录于专栏:恩蓝脚本

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

这里写图片描述
这里写图片描述

这里写图片描述
这里写图片描述

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

话不多说,直接上代码:

activity_main.xml:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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侧滑效果汇总》学习。

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档