Android开发之抽屉布局实现侧滑菜单
来源:爱站网时间:2020-10-21编辑:网友分享
对于侧滑菜单相信大家并不陌生,一般都是从左边滑出,本文是爱站技术频道小编为大家带来的Android开发之抽屉布局实现侧滑菜单,一起进入下文学习一下吧!
对于侧滑菜单相信大家并不陌生,一般都是从左边滑出,本文是爱站技术频道小编为大家带来的Android开发之抽屉布局实现侧滑菜单,一起进入下文学习一下吧!
1、Drawerlayout的xml布局
2、在Activity中的具体使用方法
package animtest.com.example.e531.drawerlayout_demo; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.RelativeLayout; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ListView listView; private DrawerLayout drawerLayout; private RelativeLayout rel; private Listdatas; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //查找组件 drawerLayout = (DrawerLayout) findViewById(R.id.drlayout); listView = (ListView) findViewById(R.id.lv); rel = (RelativeLayout) findViewById(R.id.rel); //构造数据 datas = new ArrayList (); for(int i=0;i adapter=new ArrayAdapter (this,android.R.layout.simple_list_item_1, datas); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { //关闭侧滑菜单 drawerLayout.closeDrawer(rel); //drawerLayout.openDrawer(rel);//打开侧滑菜单 //设置参数 ContentFragment contentFragment=new ContentFragment(); Bundle bundle=new Bundle(); bundle.putString("value",datas.get(position)); contentFragment.setArguments(bundle); //切换fragment getSupportFragmentManager().beginTransaction().replace(R.id.fcontent,contentFragment).commit(); } }); drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() { //slideOffset:侧滑菜单 宽度的像素值 @Override public void onDrawerSlide(View drawerView, float slideOffset) { } @Override public void onDrawerOpened(View drawerView) { Log.d("zzz","open"); } @Override public void onDrawerClosed(View drawerView) { Log.d("zzz","close"); } @Override public void onDrawerStateChanged(int newState) { Log.d("zzz","状态改变:"+newState); } }); } }
以上是爱站技术频道小编带来的Android开发之抽屉布局实现侧滑菜单,js.aizhan.com作为专业的平台,一定能令大家受益匪浅。