侧滑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 <String>datas;
private ArrayAdapter <String> adpter;
private  String title;
private String[] cictys = {"上海","北京","广州","杭州","天津"};
private ArrayAdapter<String> 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<String>();
datas.addAll(Arrays.asList(cictys));


//绑定适配器
adapter  = new ArrayAdapter<String>(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

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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}" >


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


<ListView 
   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"
   
   ></ListView>

</android.support.v4.widget.DrawerLayout>


fragment_content.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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}" >


<TextView 
   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"
   
   />
</LinearLayout>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值