【干货】Android-侧拉框实现【2019最新版】

2021-09-24 11:58:48 浏览数 (1)

欢迎转载,转载请注明出处

这里我就不啰里巴嗦了,这个写法是我昨天看到的,感觉非常有用,这里给大家分享下:


直接上源码:

首先是主布局:

代码语言:javascript复制

这里我们可以看到,我引用了:"@layout/include_list_viewpager" 和 "@menu/drawer_view" 两个文件,第一个是主活动布局,第二个是侧拉框栏目,现在给出他们的实现,首先是主活动(我这里给空的,你们根据自己需要添加控件):

代码语言:javascript复制

然后是侧拉框的栏目:

代码语言:javascript复制

 ok,那现在来看主活动,用 Runnable 绑定侧拉框各项,再用 Handle 实时监听,关于 Constants 是一个由于放字符串(可理解为 id )的静态类:

代码语言:javascript复制
public class MainActivity extends AppCompatActivity {

    private NavigationView navigationView;
    private Map navigationMap = new HashMap();
    private Handler navDrawerRunnable = new Handler();
    private Runnable runnable;
    private DrawerLayout mDrawerLayout;

    private Runnable navigateLibrary = new Runnable() {
        public void run() {
            Toast.makeText(MainActivity.this,"Library",Toast.LENGTH_SHORT).show();
        }
    };

    private Runnable navigatePlaylist = new Runnable() {
        public void run() {
            Toast.makeText(MainActivity.this,"Playlist",Toast.LENGTH_SHORT).show();
        }
    };

    private Runnable navigateFolder = new Runnable() {
        public void run() {
            Toast.makeText(MainActivity.this,"Folder",Toast.LENGTH_SHORT).show();
        }
    };

    private Runnable navigateQueue = new Runnable() {
        public void run() {
            Toast.makeText(MainActivity.this,"Queue",Toast.LENGTH_SHORT).show();
        }
    };

    private Runnable navigateAlbum = new Runnable() {
        public void run() {
            Toast.makeText(MainActivity.this,"Album",Toast.LENGTH_SHORT).show();
        }
    };

    private Runnable navigateArtist = new Runnable() {
        public void run() {
            Toast.makeText(MainActivity.this,"Artist",Toast.LENGTH_SHORT).show();
        }
    };

    private Runnable navigateLyrics = new Runnable() {
        public void run() {
            Toast.makeText(MainActivity.this,"Lyrics",Toast.LENGTH_SHORT).show();
        }
    };

    private Runnable navigateNowplaying = new Runnable() {
        public void run() {
            Toast.makeText(MainActivity.this,"Nowplaying",Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        navigationMap.put(Constants.NAVIGATE_LIBRARY, navigateLibrary);
        navigationMap.put(Constants.NAVIGATE_PLAYLIST, navigatePlaylist);
        navigationMap.put(Constants.NAVIGATE_QUEUE, navigateQueue);
        navigationMap.put(Constants.NAVIGATE_NOWPLAYING, navigateNowplaying);
        navigationMap.put(Constants.NAVIGATE_ALBUM, navigateAlbum);
        navigationMap.put(Constants.NAVIGATE_ARTIST, navigateArtist);
        navigationMap.put(Constants.NAVIGATE_LYRICS, navigateLyrics);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        navigationView = (NavigationView) findViewById(R.id.nav_view);
        View header = navigationView.inflateHeaderView(R.layout.nav_header);

        navDrawerRunnable.postDelayed(new Runnable() {
            @Override
            public void run() {
                setupDrawerContent(navigationView);
            }
        }, 700);

    }
    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(final MenuItem menuItem) {
                        updatePosition(menuItem);
                        return true;
                    }
                });
    }



    private void updatePosition(final MenuItem menuItem) {
        runnable = null;

        switch (menuItem.getItemId()) {
            case R.id.nav_library:
                runnable = navigateLibrary;

                break;
            case R.id.nav_playlists:
                runnable = navigatePlaylist;

                break;
            case R.id.nav_folders:
                runnable = navigateFolder;

                break;
            case R.id.nav_nowplaying:

                break;
            case R.id.nav_queue:
                runnable = navigateQueue;

                break;
            case R.id.nav_settings:
                runnable = navigateAlbum;
                break;
            case R.id.nav_about:
                runnable = navigateArtist;

                break;
            case R.id.nav_donate:
                runnable = navigateLyrics;

                break;
        }

        if (runnable != null) {
            menuItem.setChecked(true);
            mDrawerLayout.closeDrawers();
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    runnable.run();
                }
            }, 350);
        }
    }
}

ok,顺便来一波 Constants 类:

代码语言:javascript复制
public class Constants {

    public static final String NAVIGATE_LIBRARY = "navigate_library";
    public static final String NAVIGATE_PLAYLIST = "navigate_playlist";
    public static final String NAVIGATE_QUEUE = "navigate_queue";
    public static final String NAVIGATE_ALBUM = "navigate_album";
    public static final String NAVIGATE_ARTIST = "navigate_artist";
    public static final String NAVIGATE_NOWPLAYING = "navigate_nowplaying";
    public static final String NAVIGATE_LYRICS = "navigate_lyrics";
}

这里可能需要一些依赖比如:NavigationView 需要添加:

代码语言:javascript复制
implementation 'com.android.support:design:28.0.0'

其他依赖我救不一一给出了,大家根据需要自行百度 ~

【如果本文对您有帮助,欢迎关注我获得跟多资讯】

0 人点赞