Android 博客园客户端 (一) 基本界面


Android 博客园客户端 (一) 基本界面

Android 博客园客户端 (一) 基本界面

 ===============================================================

菜单main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.arlen.cnblogs.MainActivity" >

    <item
        android:id="@+id/action_share"
        android:icon="@drawable/ic_action_share"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:title="@string/action_share">
        <menu>
            <item
                android:id="@+id/action_share_email"
                android:icon="@drawable/ic_action_share_email"
                android:orderInCategory="101"
                android:showAsAction="always"
                android:title="@string/action_share_email"/>
            <item
                android:id="@+id/action_share_message"
                android:icon="@drawable/ic_action_share_message"
                android:orderInCategory="101"
                android:showAsAction="always"
                android:title="@string/action_share_message"/>
            <item
                android:id="@+id/action_share_bluetooth"
                android:icon="@drawable/ic_action_share_bluetooth"
                android:orderInCategory="101"
                android:showAsAction="always"
                android:title="@string/action_share_bluetooth"/>
            <item
                android:id="@+id/action_share_system"
                android:icon="@drawable/ic_action_share_system"
                android:orderInCategory="101"
                android:showAsAction="always"
                android:title="@string/action_share_system"/>
        </menu>
    </item>
    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        android:orderInCategory="100"
        android:showAsAction="always|collapseActionView"
        android:title="@string/action_search">
        <menu>
            <item
                android:id="@+id/action_search_user"
                android:icon="@drawable/ic_action_search_user"
                android:orderInCategory="102"
                android:showAsAction="always"
                android:title="@string/action_search_user"/>
            <item
                android:id="@+id/action_search_blog"
                android:icon="@drawable/ic_action_search_blog"
                android:orderInCategory="102"
                android:showAsAction="always"
                android:title="@string/action_search_blog"/>
        </menu>
    </item>
    <item
        android:id="@+id/action_refresh"
        android:icon="@drawable/ic_action_refresh"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:title="@string/action_refresh"/>

</menu>

主界面布局layout_main.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

ActivityMain.java

  1 package com.arlen.cnblogs;
  2 
  3 import com.arlen.cnblogs.fragment.AboutFragment;
  4 import com.arlen.cnblogs.fragment.BlogFragment;
  5 import com.arlen.cnblogs.fragment.HomeFragment;
  6 import com.arlen.cnblogs.fragment.NewsFragment;
  7 import com.arlen.cnblogs.fragment.SettingFragment;
  8 
  9 import android.app.Activity;
 10 import android.app.Fragment;
 11 import android.app.FragmentManager;
 12 import android.content.res.Configuration;
 13 import android.os.Bundle;
 14 import android.support.v4.app.ActionBarDrawerToggle;
 15 import android.support.v4.view.GravityCompat;
 16 import android.support.v4.widget.DrawerLayout;
 17 import android.view.Menu;
 18 import android.view.MenuInflater;
 19 import android.view.MenuItem;
 20 import android.view.View;
 21 import android.widget.AdapterView;
 22 import android.widget.ArrayAdapter;
 23 import android.widget.ListView;
 24 import android.widget.Toast;
 25 
 26 public class MainActivity extends Activity {
 27     private DrawerLayout drawerLayout;
 28     private ListView drawerList;
 29     private ActionBarDrawerToggle drawerToggle;
 30 
 31     private CharSequence drawerTitle;
 32     private CharSequence mTitle;
 33     private String[] drawerListTitles;
 34 
 35     @Override
 36     protected void onCreate(Bundle savedInstanceState) {
 37         super.onCreate(savedInstanceState);
 38         setContentView(R.layout.activity_main);
 39 
 40         mTitle = drawerTitle = getTitle();
 41         drawerListTitles = getResources().getStringArray(R.array.navigation_list);
 42         drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
 43         drawerList = (ListView) findViewById(R.id.left_drawer);
 44 
 45         // set a custom shadow that overlays the main content when the drawer opens
 46         drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
 47         // set up the drawer‘s list view with items and click listener
 48         drawerList.setAdapter(new ArrayAdapter<String>(this,
 49                 R.layout.drawer_list_item, drawerListTitles));
 50         drawerList.setOnItemClickListener(new DrawerItemClickListener());
 51 
 52         // enable ActionBar app icon to behave as action to toggle nav drawer
 53         getActionBar().setDisplayHomeAsUpEnabled(true);
 54         getActionBar().setHomeButtonEnabled(true);
 55 
 56         // ActionBarDrawerToggle ties together the the proper interactions
 57         // between the sliding drawer and the action bar app icon
 58         drawerToggle = new ActionBarDrawerToggle(
 59                 this,                  /* host Activity */
 60                 drawerLayout,         /* DrawerLayout object */
 61                 R.drawable.ic_drawer,  /* nav drawer image to replace ‘Up‘ caret */
 62                 R.string.drawer_open,  /* "open drawer" description for accessibility */
 63                 R.string.drawer_close  /* "close drawer" description for accessibility */
 64                 ) {
 65             public void onDrawerClosed(View view) {
 66                 getActionBar().setTitle(mTitle);
 67                 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
 68             }
 69 
 70             public void onDrawerOpened(View drawerView) {
 71                 getActionBar().setTitle(drawerTitle);
 72                 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
 73             }
 74         };
 75         drawerLayout.setDrawerListener(drawerToggle);
 76 
 77         if (savedInstanceState == null) {
 78             selectItem(0);
 79         }
 80     }
 81 
 82     @Override
 83     public boolean onCreateOptionsMenu(Menu menu) {
 84         MenuInflater inflater = getMenuInflater();
 85         inflater.inflate(R.menu.main, menu);
 86         return super.onCreateOptionsMenu(menu);
 87     }
 88 
 89     /* Called whenever we call invalidateOptionsMenu() */
 90     @Override
 91     public boolean onPrepareOptionsMenu(Menu menu) {
 92         // If the nav drawer is open, hide action items related to the content view
 93         boolean drawerOpen = drawerLayout.isDrawerOpen(drawerList);
 94         menu.findItem(R.id.action_refresh).setVisible(!drawerOpen);
 95         return super.onPrepareOptionsMenu(menu);
 96     }
 97 
 98     @Override
 99     public boolean onOptionsItemSelected(MenuItem item) {
100         
101         if (drawerToggle.onOptionsItemSelected(item)) {
102             return true;
103         }
104         
105         int id = item.getItemId();
106 
107         switch (id) {
108         case R.id.action_share:
109             Toast.makeText(this, "分享", Toast.LENGTH_SHORT).show();
110             return true;
111         case R.id.action_share_email:
112             Toast.makeText(this, "邮件分享", Toast.LENGTH_SHORT).show();
113             return true;
114         case R.id.action_share_message:
115             Toast.makeText(this, "短信分享", Toast.LENGTH_SHORT).show();
116             return true;
117         case R.id.action_share_bluetooth:
118             Toast.makeText(this, "蓝牙分享", Toast.LENGTH_SHORT).show();
119             return true;
120         case R.id.action_share_system:
121             Toast.makeText(this, "系统分享", Toast.LENGTH_SHORT).show();
122             return true;
123         case R.id.action_search:
124             Toast.makeText(this, "搜索", Toast.LENGTH_SHORT).show();
125             return true;
126         case R.id.action_search_user:
127             Toast.makeText(this, "搜索博主", Toast.LENGTH_SHORT).show();
128             return true;
129         case R.id.action_search_blog:
130             Toast.makeText(this, "搜索博文", Toast.LENGTH_SHORT).show();
131             return true;
132         case R.id.action_refresh:
133             Toast.makeText(this, "刷新", Toast.LENGTH_SHORT).show();
134             return true;
135         default:
136             return super.onOptionsItemSelected(item);
137         }
138     }
139 
140     /* The click listner for ListView in the navigation drawer */
141     private class DrawerItemClickListener implements ListView.OnItemClickListener {
142         @Override
143         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
144             selectItem(position);
145         }
146     }
147 
148     private void selectItem(int position) {
149         // update the main content by replacing fragments
150         Fragment fragment = null;
151         switch (position) {
152         case 0:
153             fragment = new HomeFragment();
154             break;
155         case 1:
156             fragment = new NewsFragment();
157             break;
158         case 2:
159             fragment = new BlogFragment();
160             break;
161         case 3:
162             fragment = new SettingFragment();
163             break;
164         case 4:
165             fragment = new AboutFragment();
166             break;
167         default:
168             fragment = new HomeFragment();
169             break;
170         }
171 
172         FragmentManager fragmentManager = getFragmentManager();
173         fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
174 
175         // update selected item and title, then close the drawer
176         drawerList.setItemChecked(position, true);
177         setTitle(drawerListTitles[position]);
178         drawerLayout.closeDrawer(drawerList);
179     }
180 
181     @Override
182     public void setTitle(CharSequence title) {
183         mTitle = title;
184         getActionBar().setTitle(mTitle);
185     }
186 
187     /**
188      * When using the ActionBarDrawerToggle, you must call it during
189      * onPostCreate() and onConfigurationChanged()...
190      */
191 
192     @Override
193     protected void onPostCreate(Bundle savedInstanceState) {
194         super.onPostCreate(savedInstanceState);
195         // Sync the toggle state after onRestoreInstanceState has occurred.
196         drawerToggle.syncState();
197     }
198 
199     @Override
200     public void onConfigurationChanged(Configuration newConfig) {
201         super.onConfigurationChanged(newConfig);
202         // Pass any configuration change to the drawer toggls
203         drawerToggle.onConfigurationChanged(newConfig);
204     }
205 }

源码:git@github.com:ZhangTingkuo/Cnblogs.git

Android 博客园客户端 (一) 基本界面,布布扣,bubuko.com

Android 博客园客户端 (一) 基本界面

上一篇:H - 放苹果


下一篇:Android -- Drawable与Bitmap