<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mydrawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!--注意:抽屉布局中 定义的第一个组件最好是 主内容布局 第二个是 菜单内容布局
否则:如果菜单中ListView列表的话,会造成没有组件没焦点的问题-->
<!--主内容区域-->
<RelativeLayout
android:id="@+id/fr_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
<!--菜单-->
<LinearLayout
android:id="@+id/lin_menu"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#f00"
android:orientation="vertical"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher_round" />
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
package zhanghaijiao.bawei.com.drawerlayout_demo2;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.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<String> datas;
private DrawerLayout drawerLayout;
private LinearLayout linMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView=findViewById(R.id.lv);
//抽屉布局
drawerLayout = findViewById(R.id.mydrawer);
//菜单
linMenu = findViewById(R.id.lin_menu);
datas = new ArrayList<>();
for(int i=0;i<10;i++){
datas.add("菜单"+i);
}
//设置适配器
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, datas);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//获取当前点击的条目
String s = datas.get(i);
// Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
//关闭菜单
drawerLayout.closeDrawer(linMenu);
Bundle bundle=new Bundle();
bundle.putString("data",s);
ContentFragment contentFragment = new ContentFragment();
contentFragment.setArguments(bundle);
//动态添加fragment进来
getSupportFragmentManager().beginTransaction().replace(R.id.fr_content,contentFragment).commit();
}
});
}
}
package zhanghaijiao.bawei.com.drawerlayout_demo2;
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 jane on 2018/1/4.
*/
public class ContentFragment extends Fragment {
private TextView textView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.content,container,false);
textView = v.findViewById(R.id.tv);
//接收参数
Bundle arguments = getArguments();
if(arguments!=null){
String data = arguments.getString("data");
textView.setText(data);
}
return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}