Drawlayout

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>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章