直接使用 不需要插件 重要的-------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