DrawerLayout1


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

import com.example.day12drawerlayout1.fragment.MainFragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.util.Log;
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;

/**
 * 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);
        //            }
        //        });

        /**
         * set  一般是只有一个   如果再次调用会把前面的覆盖掉
         * 和  
         * add  把数据添加进去  不会覆盖之前的内容
         */
        dl.addDrawerListener(new DrawerListener() {

            //滑动状态发生改变的时候  会调用该方法
            @Override
            public void onDrawerStateChanged(int arg0) {
                // TODO Auto-generated method stub
                Log.i("===============================", "StateChanged" + arg0);
            }

            //监听滑动过程中   边界的位置
            @Override
            public void onDrawerSlide(View arg0, float arg1) {
                // TODO Auto-generated method stub
                Log.i("===============================", "DrawerSlide" + arg1);
            }

            //监听侧拉是否完全展开
            @Override
            public void onDrawerOpened(View arg0) {
                // TODO Auto-generated method stub
                Log.i("===============================", "DrawerOpened");
            }

            //监听侧拉是否被关闭
            @Override
            public void onDrawerClosed(View arg0) {
                // TODO Auto-generated method stub
                Log.i("===============================", "DrawerClosed");
            }
        });

        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();
    }

}

=================================================================


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

import com.example.day12drawerlayout1.MainActivity;
import com.example.day12drawerlayout1.R;

public class MainFragment extends Fragment{
    TextView tv;
    ImageView iv;

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

        View view = View.inflate(getActivity(), R.layout.frag_main, null);

        tv = (TextView) view.findViewById(R.id.tv_title);
        iv = (ImageView) view.findViewById(R.id.iv);

        iv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                MainActivity activity = (MainActivity) getActivity();
                activity.getDL().openDrawer(Gravity.RIGHT);
            }
        });

        return view;
    }

    public void setData(String str){
        tv.setText(str);
    }
}

==============================xml==========================


<?xml version="1.0"?>
-<LinearLayout android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"> -<android.support.v4.widget.DrawerLayout android:layout_height="match_parent" android:layout_width="match_parent" android:layout_above="@+id/tv" android:id="@+id/dl">
<!-- 作为侧拉菜单 主页面显示的效果 要写在布局的最上面 首先进行加载 -->
 <FrameLayout android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/fl"> </FrameLayout> <ListView android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/lv" android:background="#ff0" android:layout_gravity="left"> </ListView> -<LinearLayout android:orientation="vertical" android:layout_height="match_parent" android:layout_width="200dp" android:id="@+id/ll" android:background="#0ff" android:layout_gravity="right"> <ImageView android:layout_height="100dp" android:layout_width="100dp" android:src="@drawable/ic_launcher"/> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="呵呵呵" android:layout_margin="20dp"/> </LinearLayout> </android.support.v4.widget.DrawerLayout> </LinearLayout>

========================================================


<?xml version="1.0"?>
-<LinearLayout android:orientation="vertical" android:gravity="center" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="标题" android:layout_margin="20dp" android:id="@+id/tv_title"/> <ImageView android:layout_height="100dp" android:layout_width="100dp" android:id="@+id/iv" android:src="@drawable/ic_launcher"/> </LinearLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值