package com.example.day12drawerlayout1; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; import android.support.v4.widget.DrawerLayout; import android.view.Gravity; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import com.example.day12drawerlayout1.fragment.MainFragment; import java.util.ArrayList; import java.util.List; import test.lilin.com.tabdraw.R; /** * 1、静态和动态Fragment的使用 * 静态 直接在布局中使用<fragment /> * 动态 使用管理器 得到一个事务 然后使用事务调用replace方法 把一个Fragment对象替换到指定id的FramLayout帧布局中 * @author Administrator * */ public class MainActivity extends FragmentActivity { DrawerLayout dl; ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dl = (DrawerLayout) findViewById(R.id.dl); // FrameLayout fl = (FrameLayout) findViewById(R.id.fl); // fl.setOnClickListener(new OnClickListener() { // // @Override // public void onClick(View v) { // // TODO Auto-generated method stub // dl.openDrawer(Gravity.RIGHT); // } // showMain(); showLV(); } public DrawerLayout getDL(){ return dl; } private void showLV() { lv = (ListView) findViewById(R.id.lv); final List<String> list = new ArrayList<String>(); for (int i = 1; i < 30; i++) { list.add("条目"+i); } ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list); lv.setAdapter(adapter); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub dl.closeDrawer(Gravity.LEFT); //把点击的listview控件中的值 赋值到主Fragment对象中 MainFragment fragment = (MainFragment) getSupportFragmentManager().findFragmentByTag("main"); fragment.setData(list.get(position)); } }); } /** * 在侧拉效果的页面中 用来显示主页面的效果 */ private void showMain() { //动态加载Fragment FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); //参数1:FramLayout控件的id, 要替换的Fragment对象 transaction.replace(R.id.fl, new MainFragment(), "main"); transaction.commit(); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v4.widget.DrawerLayout android:id="@+id/dl" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/tv" > <!-- 作为侧拉菜单 主页面显示的效果 要写在布局的最上面 首先进行加载 --> <FrameLayout android:id="@+id/fl" android:layout_width="match_parent" android:layout_height="match_parent" > </FrameLayout> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff0" android:layout_gravity="left" > </ListView> <LinearLayout android:id="@+id/ll" android:layout_width="200dp" android:layout_height="match_parent" android:layout_gravity="right" android:background="#0ff" android:orientation="vertical" > <ImageView android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp" android:text="呵呵呵" /> </LinearLayout> </android.support.v4.widget.DrawerLayout> </LinearLayout>