DrawerLayout实现左侧右侧滑动栏目(附栏目内容获取判断点击事件的配置)

本文简单使用了DrawerLayout的应用,设置两个view,通过左右滑动来唤出
首先我们将布局文件修改为DrawerLayout,并在里面添加左侧与右侧想要实现的布局,在这里我们用一个listview来实现左侧滑动出的栏目的栏目添加

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_gravity="start"
        android:background="#ffff">

        <ListView
            android:id="@+id/list_view"
            android:layout_width="150dp"
            android:layout_height="match_parent" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="150dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_gravity="end"
        android:background="#D81B60">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="用户数据" />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="退出登录" />
    </LinearLayout>

</androidx.drawerlayout.widget.DrawerLayout>

两个对应的view中layout_gravity设置成“start”与“end”,分别实现从左侧出现与右侧出现
对应地用“left”与“right”也是可以的
通过设置LinearLayout的宽度来改变滑动出的栏目宽度

接下来是MainActivity文件的代码:

package com.game.drawerlayout;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {
ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView)findViewById(R.id.list_view);//对listview进行引用
        //创建适配器字符串数组来生成左侧栏目
        ArrayAdapter<String>adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,new String[]{"菜单1","菜单2","菜单3","菜单4"});
        //通过setAdapter方法来将适配器中的内容与listview连接起来
        listView.setAdapter(adapter);
    }
}

这里使用了适配器与listview进行对应的栏目生成。
如果使用simple_list_item_2会发生报错如下:

ArrayAdapter requires the resource ID to be a TextView

因为simple_list_item_2的根节点是TextView,这里换成simple_list_item_1就不会报错。

下面是如何设置点击事件的内容获取:

  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //getItemAtPosition()中的position参数即为i,这里获取点击到的内容
                String result = adapterView.getItemAtPosition(i).toString();
                switch(result){
                    case "菜单1":
                        Toast.makeText(MainActivity.this, "click 1", Toast.LENGTH_SHORT).show();
                        break;
                        default:
                            break;
                }
            }
        });

在这里特别提一下如何获取点击按钮的内容来进行点击事件的判定
在上面这段代码中我们使用adapterView.getItemAtPosition(i).toString()这个方法来获取内容,i即为position参数(在上文中)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值