ANDROID drawerlayout+fragment侧滑

<android.support.v4.widget.DrawerLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/id_drawerlayout"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent" >  
    <FrameLayout
        android:id="@+id/id_framelayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fffaf0" >
    </FrameLayout>

    <LinearLayout
        android:id="@+id/id_linearlayout"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#f5f5f5"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/id_listView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:divider="#deb887"
            android:dividerHeight="1dp" >
        </ListView>
    </LinearLayout>
</android.support.v4.widget.DrawerLayout  >

这里直接放一个drawerlayout,里面上面那个是主内容

下面的是侧滑菜单,这里放的一个ListView

下面放java文件

package com.example.sliding;



import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class MainActivity extends ActionBarActivity implements OnItemClickListener {
	private DrawerLayout mDrawLayout;
	private ListView mListView;
	private FragmentManager fragmentManager;
	private Fragment fragment1,fragment2,fragment3;
	private String[] str;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intiView();
    }
    private void intiView() {
    	mDrawLayout=(DrawerLayout) findViewById(R.id.id_drawerlayout);
    	mListView=(ListView) findViewById(R.id.id_listView);
    	str = new String[] { "item1", "item2", "item3"};
    	ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.
    			R.layout.simple_list_item_1, str);
		mListView.setAdapter(adapter);
		mListView.setOnItemClickListener(this);
		fragmentManager = getSupportFragmentManager();
		fragment1= new Fragment1();
		fragment2 = new Fragment2();
		fragment3 = new Fragment3();
		
		fragmentManager.beginTransaction().replace(R.id.id_framelayout, fragment1).commit();
		setTitle("fragment 1");
		mDrawLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
		});
	}
	

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		switch (position) {
		case 0:
			fragmentManager.beginTransaction().
			 	replace(R.id.id_framelayout, fragment1).commit();
			setTitle("fragment1");
			break;
		case 1:
			fragmentManager.beginTransaction().
		 	replace(R.id.id_framelayout, fragment2).commit();
			setTitle("fragment2");
			break;
		case 2:
			fragmentManager.beginTransaction().
		 	replace(R.id.id_framelayout, fragment3).commit();
			setTitle("fragment3");
			break;
		}
		mDrawLayout.closeDrawers();
	}



	}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值