androidDrawerLayout侧边栏实现

更新时间:2023-06-26 22:04:27 阅读: 评论:0

androidDrawerLayout侧边栏实现
现在实现侧边栏⽐较简单了,官⽅提供的DrawerLayout可以很⽅便实现。
主要实现⽅法是:⽤DrawerLayout 作为界⾯根控件。在DrawerLayout⾥⾯第⼀个View为当前界⾯主内容;第⼆个和第三个View为抽屉菜单内容。如果当前界⾯只需要⼀个抽屉菜单,则第三个View可以省略。
第⼀个View的宽⾼应当设置为match_parent
第⼆、三个View需要设置android:layout_gravity=”left”,和android:layout_gravity=”right”且⼀搬⾼度设置为match_parent,宽度为固定值,即侧滑菜单的宽度,宽度应该不超过320dp,这样⽤户可以在菜单打开的时候看到部分内容界⾯。如果需要监听菜单打开关闭事件,则需要调⽤ DrawerLayout类的tDrawerListener()
该接⼝提供了菜单打开关闭等事件的回调函数,例如
onDrawerOpened()
onDrawerClod()
效果图:
描述⼀下:右侧滑出侧边栏,包括⼀个标题,⼀个车辆列表,点击列表,右侧滑出⼀个与侧边栏等宽的popupwindow,也包括标题与列表,点击popupwindow消失。主要有两点,第⼀是侧边栏,第⼆是popupwindow以及滑动动画
上代码:
主布局activity_l
<android.support.v4.widget.DrawerLayout xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:id="@+id/id_drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f2f2f2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
&boerchargingpile.UI.CustomTitles.TitleLayout
android:id="@+id/id_titleLayot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@mipmap/top">
</boerchargingpile.UI.CustomTitles.TitleLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:orientation="vertical">
<ImageView
android:id="@+id/id_imageMyCar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="88dp"
android:layout_marginBottom="88dp"
android:src="@mipmap/mycar_2" />
</LinearLayout>
<LinearLayout
android:id="@+id/id_linearCarKind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/rect_white"
android:padding="10dp"
android:gravity="center">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_weight="1"
android:text="@string/mycar_kind"
android:drawableLeft="@mipmap/mycar"
android:drawablePadding="10dp"
android:textColor="#3a3a3a"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/next_arrow"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/id_right_menu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@mipmap/top"
android:orientation="horizontal">
<ImageView
android:id="@+id/id_imageUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/back"
android:layout_margin="12dp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/mycar_favorite"
android:textColor="#ffffff"
android:textSize="17sp" />
</LinearLayout>
<ListView
android:id="@+id/id_listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:cacheColorHint="#00000000"
android:divider="#f2f2f2"
android:dividerHeight="1dp"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
主界⾯代码
//我的爱车
public class FavoriteCarActivity extends BaActivity implements TitleLayout.titleLayoutClick {    private TitleLayout titleLayout;
private ImageView imageViewCar;
private LinearLayout linearLayoutCarKind;
private LinearLayout linearLayoutRight;//右边栏
private DrawerLayout mDrawerLayout;
private ListView listViewCar;//车名称
private ArrayList<String> carNameList = new ArrayList<>();//车名称
private ArrayList<String> carModelList = new ArrayList<>();//车类型
private PopupWindow popupWindow = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
tContentView(R.layout.activity_favorite_car);
titleLayout = (TitleLayout) findViewById(R.id.id_titleLayot);
titleLayout.tTitle("我的爱车");
imageViewCar = (ImageView) findViewById(R.id.id_imageMyCar);
ImageView imageViewUp = (ImageView) findViewById(R.id.id_imageUp);
linearLayoutCarKind = (LinearLayout) findViewById(R.id.id_linearCarKind);
linearLayoutRight = (LinearLayout) findViewById(R.id.id_right_menu);绝望的笑容
linearLayoutCarKind.tOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OpenRightMenu();
}
});
mDrawerLayout = (DrawerLayout) findViewById(R.id.id_drawerLayout);
mDrawerLayout.tDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,
Gravity.RIGHT);
listViewCar = (ListView) findViewById(R.id.id_listView);
for (int i = 0; i < 10; i++) {
carNameList.add("保时捷");
carModelList.add("Panamera");
}
CarAdapter carAdapter = new CarAdapter();
listViewCar.tAdapter(carAdapter);
listViewCar.tOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
showCarModelPopupWindow();
}
});
imageViewUp.tOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDrawerLayout.cloDrawers();
}
});
}
public void OpenRightMenu() {
mDrawerLayout.openDrawer(Gravity.RIGHT);
mDrawerLayout.tDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED,
Gravity.RIGHT);
}
private class CarAdapter extends BaAdapter {
public CarAdapter() {
}
@Override
public int getCount() {
return carNameList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(FavoriteCarActivity.this).inflate(R.layout.car_name_item, null);            }
TextView textViewCarName = (convertView, R.id.id_textViewCar);
textViewCarName.(position));
return convertView;
}
}
@Override
public void leftViewClick() {
finish();
期中考试复习计划}
@Override
public void rightViewClick() {
}
private void showCarModelPopupWindow() {
View view = View.inflate(FavoriteCarActivity.this, R.layout.car_model_list, null);
ImageView imageViewUp = (ImageView) view.findViewById(R.id.id_imageUp);
TextView textViewTitle = (TextView) view.findViewById(R.id.id_textViewCarName);
ListView listViewCarModel = (ListView) view.findViewById(R.id.id_listViewCarModel);
popupWindow = new PopupWindow(view, Width(),
LinearLayout.LayoutParams.MATCH_PARENT, true);
popupWindow.tAnimationStyle(R.style.MenuAnimationLeftRight);
popupWindow.tBackgroundDrawable(new BitmapDrawable());//需要设置背景,⽤物理键返回的时候        popupWindow.tFocusable(true);
popupWindow.tOutsideTouchable(true);  //设置点击屏幕其它地⽅弹出框消失
CarModelAdapter carModelAdapter = new CarModelAdapter();
listViewCarModel.tAdapter(carModelAdapter);
listViewCarModel.tOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
popupWindow.dismiss();
}
});
popupWindow.showAtLocation(titleLayout, Gravity.RIGHT, 0, 0);
imageViewUp.tOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
}
private class CarModelAdapter extends BaAdapter {
public CarModelAdapter() {高层建筑施工
青菜苔}
@Override
public int getCount() {
return carModelList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(FavoriteCarActivity.this).inflate(R.layout.car_model_item, null);            }
TextView textViewCarName = (convertView, R.id.id_textViewCarModel);
textViewCarName.(position));
return convertView;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
树的特点if (keyCode == KeyEvent.KEYCODE_BACK) {
if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
mDrawerLayout.cloDrawers();
} el {
KeyDown(keyCode, event);
}
return true;
}
KeyDown(keyCode, event);
}
}
标题类TitleLayout.java
/**
* Created by dell on 2015/11/10.
* 布局标题
*/
江心坡
public class TitleLayout extends RelativeLayout {
TextView textViewContent;
LinearLayout linearLeft;
ImageView imageViewLeft;
LinearLayout linearRight;
ImageView imageViewRight;
TextView textViewRight;
private titleLayoutClick mListener;
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title, this);
textViewContent = (TextView) findViewById(R.id.id_textviewContent);
linearLeft = (LinearLayout) findViewById(R.id.id_linearLeft);
imageViewLeft = (ImageView)findViewById(R.id.id_left);
linearRight = (LinearLayout) findViewById(R.id.id_linearRight);
imageViewRight = (ImageView)findViewById(R.id.id_right);
textViewRight = (TextView) findViewById(R.id.id_rightText);
try{
mListener = (titleLayoutClick) context;
}catch (Exception e){
e.printStackTrace();
}
linearLeft.tOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.leftViewClick();小组名字
}
}
});
linearRight.tOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.rightViewClick();
}
}
});
}
public void tTitle(String title) {
textViewContent.tText(title);
}
//设置右边图⽚
public void tLinearRightImage(int drawableID) {
imageViewRight.tBackgroundResource(drawableID);
}
//设置左边图⽚
public void tLinearLeftImage(int drawableID) {
imageViewLeft.tBackgroundResource(drawableID);
}
//设置右边⽂本
public void tLinearRightText(String rightTextString) {
textViewRight.tText(rightTextString);
}
/
/设置右边图⽚为可见,根据viewID判断⽂本与图⽚的显⽰,0显⽰图⽚,1显⽰⽂本    public void tLinearRightVisibility(int viewID) {
linearRight.tVisibility(View.VISIBLE);
if(viewID==0){
imageViewRight.tVisibility(View.VISIBLE);
textViewRight.tVisibility(View.INVISIBLE);
}el if(viewID==1){
textViewRight.tVisibility(View.VISIBLE);
imageViewRight.tVisibility(View.INVISIBLE);
}
}
public interface titleLayoutClick {
public void leftViewClick();//左图⽚点击
public void rightViewClick();//右边图⽚点击
革命气节歌
}
}

本文发布于:2023-06-26 22:04:27,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1046726.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:菜单   列表   打开   需要   关闭   点击   事件
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图