左侧拉动

public class MainActivity extends AppCompatActivity implements DrawerLayout.DrawerListener{
    private static final String TAG = "MainActivity---";
    private String[] names = new String[]{"baby", "尼古拉斯-赵四", "赵丽颖", "张三"};
    private ListView listView;
    private DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<String> strings = Arrays.asList(names);

        listView = findViewById(R.id.main_listview);

        listView.setAdapter(new MyAdapter());

        final TextView content = findViewById(R.id.content);

        drawerLayout = findViewById(R.id.drawer_layout);
        drawerLayout.setDrawerListener(this);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                content.setText(names[position]);
                //让侧滑菜单收回去
                drawerLayout.closeDrawers();
            }
        });

    }
    //当侧滑菜单滑动的时候调用
    @Override
    public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {

    }
    //当侧滑菜单打开的时候
    @Override
    public void onDrawerOpened(@NonNull View drawerView) {
        Toast.makeText(MainActivity.this,"打开",Toast.LENGTH_SHORT).show();
    }
    //当侧滑菜单关闭的时候
    @Override
    public void onDrawerClosed(@NonNull View drawerView) {

        BitmapFactory.Options o=new BitmapFactory.Options();
        o.inSampleSize=2;
        Toast.makeText(MainActivity.this,"关闭",Toast.LENGTH_SHORT).show();
    }
    //当侧滑菜单状态改变的时候
    @Override
    public void onDrawerStateChanged(int newState) {
        Log.d(TAG, "onDrawerStateChanged: "+newState);
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return names.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView = new TextView(MainActivity.this);
            textView.setTextSize(25);
            textView.setText(names[position]);
            return textView;
        }
    }

}



//布局

<android.support.v4.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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView

            android:id="@+id/maint_title"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#F00"
            android:gravity="center"
            android:text="标题" />

        <TextView
            android:gravity="center"
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="内容" />
    </LinearLayout>

    <ListView
        android:id="@+id/main_listview"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#0f0"></ListView>


</android.support.v4.widget.DrawerLayout>


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭