Drawlayout

直接使用  不需要插件        重要的-------listview设置  左滑?右滑??

<?xml version="1.0" encoding="utf-8"?>
<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/drawlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bwei.drawerlayout.MainActivity">

    <FrameLayout

        android:id="@+id/fl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

    <ListView
        android:background="#ff00ff"
        android:id="@+id/lv"
        android:layout_gravity="start"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        />

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

代码

package com.bwei.drawerlayout;

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.bwei.drawerlayout.com.bwei.adapter.MyAdapter;
import com.bwei.drawerlayout.com.bwei.fragment.ContentFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<String> list;
    private DrawerLayout drawlayout;
    private ListView lv;

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

        initData();

        lv = (ListView) findViewById(R.id.lv);

        drawlayout = (DrawerLayout) findViewById(R.id.drawlayout);

        MyAdapter adapter = new MyAdapter(list,this);
        //给listView设置adapter
        lv.setAdapter(adapter);

        /**
         * listView的item点击事件
         */
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                //创建出fragment
                ContentFragment fragment = new ContentFragment();

                Bundle b = new Bundle();
                b.putString("name",list.get(position));

                //fragment进行传值
                fragment.setArguments(b);

                getSupportFragmentManager().beginTransaction().replace(R.id.fl,fragment).commit();

                //关闭侧滑
                drawlayout.closeDrawer(lv);

                //drawlayout.ope
            }
        });

    }

    private void initData() {

        list = new ArrayList<>();
        list.add("娱乐");
        list.add("热点");
        list.add("视频");
    }


}
适配器

package com.bwei.drawerlayout.com.bwei.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bwei.drawerlayout.MainActivity;

import java.util.List;

/**
 * Created by Administrator on 2017/12/7.
 */

public class MyAdapter extends BaseAdapter {
;
    List<String> list;
    Context context;
    public MyAdapter(List<String> list, Context context) {

        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

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

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        TextView tv = new TextView(context);
        tv.setTextSize(35);
        tv.setText(list.get(i));
        return tv;
    }
}

fragment传值

package com.bwei.drawerlayout.com.bwei.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by Administrator on 2017/12/7.
 */

public class ContentFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        //取值
        Bundle bundle = getArguments();
        String name = bundle.getString("name");

        TextView tv = new TextView(getActivity());

        tv.setText(name);

        return tv;
    }
}
OK

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值