谷歌電子市場-ActionBarDrawerToggle、DrawerLayout、ActionBar

1.側邊欄是一個抽屜效果
2viewpager是一個滑動的
3.頂部的類似viewpagerindecater 升級
4.自動輪播圖
5.listview展示
6.點擊進去詳情數據展示 打開關閉
7.下載退出,兩個狀態是同步的 ,暫停,點擊下載可以繼續下載,點擊安裝,


始終有問題,即使運行別人寫好的代碼也不能替換actionBar的圖標,更不能實現動態效果….從來沒有實現過,不知道問題出在哪?
但是在查資料的時候看到很多
ActionBar自定義佈局
不同fragment下actionBar不同狀態
L bar
最新的 Toolbar
更改ActionBar的up圖標,但是就沒有Toggle的動態效果了
主界面的佈局見 DrawLayout

public class MainActivity extends AppCompatActivity {

    private ActionBar mActionBar;
    private DrawerLayout mDrawerLayout;    // 抽屜
    private ActionBarDrawerToggle mDrawerToggle;    // 抽屜開關

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

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);

        // 初始化actionbar
        initActionBar();


    }

    private void initActionBar() {
        mActionBar = getSupportActionBar();

        // 設置title
        mActionBar.setTitle("GooglePlay");
        // 設置圖標
        mActionBar.setIcon(R.mipmap.ic_launcher);
        mActionBar.setDisplayShowHomeEnabled(true);
        // 顯示up按鈕
        mActionBar.setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);//設置返回鍵可用
        mActionBar.setHomeAsUpIndicator(R.mipmap.ic_drawer_am);
        // 創建開關
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.mipmap.ic_drawer_am,
                R.string.openDrawerContentDescRes,
                R.string.closeDrawerContentDescRes);
        // 後面兩個String其實沒什麼意思,但是一定要寫

        // 設置drawerlayout的監聽
        //實現動畫
        //ActionBarDrawerToggle implements DrawerLayout.DrawerListener
        mDrawerLayout.setDrawerListener(mDrawerToggle);

    }

    /**
     *使用時覆蓋這個方法,圖標就會伸縮了
     * When using the ActionBarDrawerToggle, you must call it during
     * onPostCreate() and onConfigurationChanged()...
     */
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // 打開或關閉抽屜
                boolean selected = mDrawerToggle.onOptionsItemSelected(item);
                /*
                 * This method should be called by your <code>Activity</code>'s
                 * {@link Activity#onOptionsItemSelected(android.view.MenuItem) onOptionsItemSelected} method.
                 * If it returns true, your <code>onOptionsItemSelected</code> method should return true and
                 * skip further processing.
                 *
                 * @param item the MenuItem instance representing the selected menu item
                 * @return true if the event was handled and further processing should not occur
*/
                if (selected) {
                    return true;//防止影響下面的
                }
                break;

            default:
                break;
        }

        return super.onOptionsItemSelected(item);
    }
}
發佈了37 篇原創文章 · 獲贊 46 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章