編輯:關於Android編程
DrawerLayout 在supportV4 Lib中,類似開源slidemenu一樣,DrawerLayout父類為ViewGroup,自定義組件基本都是擴展這個類。
android.support.v4.widget.DrawerLayout
下面是個簡單的用法演示。點左上角的按鈕 打開抽屜菜單。
點擊對應的ITEM 切換對應的內容,內容顯示使用Fragment,這裡沒用到ActionBar來做切換<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:java;">
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class DrawerLayoutActivity extends FragmentActivity {
private DrawerLayout mDrawerLayout;
private ListView drawerContent;//抽屜內容
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_layout);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerContent = (ListView) findViewById(R.id.drawer_content);
mDrawerLayout.setDrawerListener(new DemoDrawerListener());
addHeader(drawerContent);//ListView Header頭
drawerContent.setAdapter(new MyAdapter());
drawerContent.setOnItemClickListener(new DrawerItemClickListener());
findViewById(R.id.slide).setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
mDrawerLayout.openDrawer(drawerContent);//打開抽屜內容
}
});
//顯示默認的HOME
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
}
private void addHeader(ListView mDrawer2) {
View view = LayoutInflater.from(this).inflate(R.layout.list_header,null);
mDrawer2.addHeaderView(view);
}
static String titles[] = { "主頁", "公司", "附近", "設置" };
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return titles.length;
}
@Override
public Object getItem(int p) {
return titles[p];
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int p, View contentView, ViewGroup arg2) {
String title = titles[p];
View v = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_list, null);
TextView textView = (TextView) v.findViewById(R.id.text);
textView.setText(title);
return v;
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position,long id) {
showFragmentById(position);
mDrawerLayout.closeDrawer(drawerContent);
}
}
//切換到對應的Fragment
public void showFragmentById(int position) {
Fragment f = null;
switch (position) {
case 1:
f = new HomeFragment();
break;
case 2:
f = new CompanyFragment();
break;
case 3:
f = new NearFragment();
break;
case 4:
f = new SettingFragment();
break;
default:
break;
}
if (f == null) {
return;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, f).commit();
}
//回調函數,打開或關閉時可以做一些事情
private class DemoDrawerListener implements DrawerLayout.DrawerListener {
@Override
public void onDrawerOpened(View drawerView) { //打開時調用
}
@Override
public void onDrawerClosed(View drawerView) { //關閉時調用
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {//滑動過程中
}
@Override
public void onDrawerStateChanged(int newState) {
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
}
這裡只寫HomeFragment代碼,其它幾個內容都是一樣的。
public class HomeFragment extends Fragment {
public HomeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.home_fg, container,false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView textView = (TextView) view.findViewById(R.id.text);
textView.setText("Home");
}
}
home_fg.xml
Android系統源碼閱讀(11):Android的InputManagerService的工作過程 請對照AOSP版本:6.0.1_r50。1. 創建InputMana
由於公司項目的需要,要實現在項目中使用第三方授權登錄以及分享文字和圖片等這樣的效果,幾經波折,查閱了一番資料,做了一個Demo。實現起來的效果還是不錯的,不敢獨享,決定寫
本文實例講述了Android上下文菜單用法。分享給大家供大家參考。具體如下:上下文菜單不同於選項菜單,選項菜單服務於Activity,而上下文菜單則是注冊到某個View對
准備寫幾篇博客講解Android的動畫,首先介紹Android動畫的整體輪廓。 Android動畫主要分為三大類 View Animation Drawable An