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
    评论
A ViewPager is a layout manager that allows the user to swipe left and right to move between pages of data. In the case of a Fragment-based ViewPager, each page is represented by a Fragment. To use a ViewPager with Fragments, you need to do the following: 1. Create a layout file that contains a ViewPager element. 2. Create a Fragment class that will represent a single page in the ViewPager. This class should inflate a layout that contains any UI elements you want to display on the page. 3. Create an adapter class that extends FragmentPagerAdapter or FragmentStatePagerAdapter. This adapter will be responsible for creating and managing the Fragments that are displayed in the ViewPager. 4. Set the adapter on the ViewPager. Here's an example of how to create a Fragment-based ViewPager: 1. Create a layout file that contains a ViewPager element. For example: ``` <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 2. Create a Fragment class that will represent a single page in the ViewPager. For example: ``` public class MyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_my, container, false); // TODO: Set up UI elements in the layout return view; } } ``` 3. Create an adapter class that extends FragmentPagerAdapter or FragmentStatePagerAdapter. For example: ``` public class MyPagerAdapter extends FragmentPagerAdapter { public MyPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { // TODO: Return a new instance of MyFragment for the given position return null; } @Override public int getCount() { // TODO: Return the total number of pages return 0; } } ``` 4. Set the adapter on the ViewPager in your Activity or Fragment. For example: ``` ViewPager viewPager = findViewById(R.id.view_pager); MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(adapter); ``` That's it! You should now have a functioning Fragment-based ViewPager. Of course, you'll need to fill in the TODOs in the code snippets above to actually display content on the pages.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值