侧滑1

package com.example.drawerlayout;


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


import ContentFragment.ContentFragment;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;


public class MainActivity extends Activity implements OnItemClickListener{


private DrawerLayout drawerlayout;
private FrameLayout flContent;
private ListView lv;
private ActionBarDrawerToggle drawerToggle;




private  List datas;
private ArrayAdapter adpter;
private  String title;
private String[] cictys = {"上海","北京","广州","杭州","天津"};
private ArrayAdapter adapter;


@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


title  = (String) getTitle();
drawerlayout  = (DrawerLayout) findViewById(R.id.drawerlayout);
flContent  = (FrameLayout) findViewById(R.id.flContent);
lv  = (ListView) findViewById(R.id.iv);


datas = new ArrayList();
datas.addAll(Arrays.asList(cictys));


//绑定适配器
adapter  = new ArrayAdapter(this, android.R.layout.simple_list_item_1,datas );
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);


//创建菜单控制开关
drawerToggle = new ActionBarDrawerToggle
(this, drawerlayout, R.drawable.ic_launcher, 
R.string.drawer_open, R.string.drawer_close
){


@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActionBar().setTitle("请选择城市");
invalidateOptionsMenu();
}


@Override
public void onDrawerOpened(View drawerView) {


super.onDrawerOpened(drawerView);
getActionBar().setTitle(title);
invalidateOptionsMenu();
}


};
drawerlayout.setDrawerListener(drawerToggle);
//开启ACTIONBAR  显示
getActionBar().setDisplayHomeAsUpEnabled(true);
//控制菜单开关点击事件能够响应
getActionBar().setHomeButtonEnabled(true);
}


@Override
public void onItemClick(AdapterView parent, View view, int position,
long id) {
// ContentFragment fragment = new ContentFragment();
// Bundle args = new Bundle();
// args.putString("text", datas.get(position));
// fragment.setArguments(args);
// getFragmentManager().beginTransaction().replace
// (R.id.flContent, fragment).commit();

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean isDrawOpen = drawerlayout.isDrawerOpen(lv);
menu.findItem(R.id.action_websearch).setVisible(!isDrawOpen);
return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
//屏蔽drawerToggle的点击事件
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
if (item.getItemId() == R.id.action_websearch) {
Intent intent = new Intent();
intent.setAction("android.intent.action.VTEW");
Uri uri = Uri.parse("http//www.baidu.com");
intent.setData(uri);
startActivity(intent);


}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
//将drawerToggle与DrawerLayout同步
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {


super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}

}


activity.xml

    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >


   android:id="@+id/flContent" 
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   
   >


   android:id="@+id/iv"
   android:layout_width="280dp"
   android:layout_height="match_parent"
   android:layout_gravity="start"
   android:background="#F0F8FF"
   android:divider="@android:color/transparent"
   android:dividerHeight="0dp"
   
   >


fragment_content.xml

    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >


   android:id="@+id/tv"
   android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:textStyle="bold"
android:gravity="center"
android:text="abc"
   
   />

你可能感兴趣的:(侧滑1)