MdRr捶惜,一個(gè)結(jié)合Material Design+RxJava+Retrofit的小demo

本文出自 “阿敏其人” 簡書博客批钠,轉(zhuǎn)載或引用請注明出處。

一個(gè)簡單的小綜合demo怠苔,分別演示:

  • Material Design
  • RxJava
  • Retrofit
  • RxJava+Retrofit
Paste_Image.png

一同廉、簡單的小架子

Paste_Image.png

布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mDrawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fitsSystemWindows="true"
    tools:context="com.am.mdrr.MainActivity">

    <!--抽屜分兩部分 一部分內(nèi)容, 一部分導(dǎo)航-->

    <!--抽屜的 內(nèi)容部分-->
    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/mCoordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/mAppbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
            <android.support.v7.widget.Toolbar
                android:id="@+id/mToolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
        </android.support.design.widget.AppBarLayout>

        <FrameLayout
            android:id="@+id/mFrameLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/appbar"
            android:scrollbars="none"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </android.support.design.widget.CoordinatorLayout>

    <!--抽屜  導(dǎo)航部分
    分為 導(dǎo)航頭部和導(dǎo)航主菜單
    可以通過 headerLayout 設(shè)置抽出方向-->
    <android.support.design.widget.NavigationView
        android:id="@+id/mNavigation"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_head"
        app:menu="@menu/nav_main_part"
        />
</android.support.v4.widget.DrawerLayout>

設(shè)置抽屜導(dǎo)航的 頭部 和 菜單
.
.
MainActivity

public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mActionBarDrawerToggle;
    private Toolbar mToolbar;
    private NavigationView mNavigationView;
    private FragmentManager supportFragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mToolbar = (Toolbar) findViewById(R.id.mToolbar);
        setSupportActionBar(mToolbar);

        supportFragmentManager = getSupportFragmentManager();

        // 抽屜整體的一些設(shè)置柑司,toggle同步迫肖,添加toggle同步  沒卵用但是一定要加
        mDrawerLayout = (DrawerLayout) findViewById(R.id.mDrawerLayout);
        mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.open,
                R.string.close);
        mActionBarDrawerToggle.syncState();
        mDrawerLayout.addDrawerListener(mActionBarDrawerToggle);

        // 菜單的頭部
        mNavigationView = (NavigationView) findViewById(R.id.mNavigation);
        View headerView = mNavigationView.getHeaderView(0); // 得到菜單 的頭部
        if(headerView!=null){
            headerView.findViewById(R.id.mIvPic).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switchToRx();
                    mDrawerLayout.closeDrawers();
                    mNavigationView.getMenu().getItem(1).setChecked(true);
                }
            });
        }

        // 菜單的正式部分
        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.nav_item_rx:
                        switchToRx();
                        break;
                    case R.id.nav_item_re:
                        switchToRe();
                        break;
                    case R.id.nav_item_rxre:
                        switchToRxRe();
                        break;
                    case R.id.nav_item_md:
                        switchToMd();
                        break;
                    case R.id.nav_item_about:
                        switchToAbout();
                        break;
                }
                menuItem.setChecked(true); // 把當(dāng)前菜單的item置為選中狀態(tài)
                mDrawerLayout.closeDrawers(); // 抽屜合起來,隱藏菜單

                // 默認(rèn)返回flase攒驰,自己實(shí)現(xiàn)邏輯需要返回true
                return true;
            }
        });

    }

    private void switchToRx() {
        supportFragmentManager.beginTransaction().replace(R.id.mFrameLayout, new RxFragment()).commit();
        mToolbar.setTitle("RxJava");
    }

    private void switchToRe() {
        supportFragmentManager.beginTransaction().replace(R.id.mFrameLayout, new ReFragment()).commit();
        mToolbar.setTitle("Retrofit");
    }

    private void switchToRxRe() {
        supportFragmentManager.beginTransaction().replace(R.id.mFrameLayout, new RxReFragment()).commit();
        mToolbar.setTitle("RxJava Retrofit");
    }

    private void switchToMd() {
        supportFragmentManager.beginTransaction().replace(R.id.mFrameLayout, new MdFragment()).commit();
        mToolbar.setTitle("Material Design");
    }

    private void switchToAbout() {
        supportFragmentManager.beginTransaction().replace(R.id.mFrameLayout, new AboutFragment()).commit();
        mToolbar.setTitle("About");
    }
}

gif

小架子.gif

二蟆湖、 Md Widget

*SnackBar

  • CardView
  • RecyclerView
  • TextInputLayput
  • FloatingActionButton
  • ToolBar
  • CoordinatorLaypout
  • CollasoingToolvarLaypout
  • NestedScrollView
  • TabLayout

簡圖如下

MdShow.gif

注:
關(guān)于RecyclerView沒有單獨(dú)開一個(gè)activity,但是其實(shí)列表本事就是RecyclerView了玻粪,想學(xué)參考可以參考這里:RecyclerView良好參考

關(guān)于md控件隅津,可以參考這里:
MD系列2、ToolBar+DrawerLayout + NavigationView

Md系列3劲室、CoordinatorLayout 里 Toobar和TabLayout等發(fā)生的一系列故事

三伦仍、RxJava

RxJava和Retrofit往往是一起使用的。

所以 build.gradle添加如下引入

    compile 'io.reactivex:rxjava:1.1.7'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
    compile 'com.google.code.gson:gson:2.7'

分點(diǎn):

  • RxJava初認(rèn)識(shí)
  • Observer+Observable
  • Subscriber+Observable
  • 快捷創(chuàng)建事件很洋,just
  • 快捷創(chuàng)建事件充蓝,from
  • 不完整回調(diào)ActionX
  • Scheduler調(diào)度器
  • Scheduler切換線程
  • 變換入門
  • 變換flatMap
  • 應(yīng)用場景

gif

Rx.gif

RxJava可以參考這里:旁邊那門轉(zhuǎn)左看到一個(gè)RxJava

四、Retrofit

當(dāng)前只做了一個(gè)示例蹲缠,就是RxJava和Retrofit結(jié)合的獲取天氣的demo頁面棺克。

Retro.gif

關(guān)鍵代碼

        String baseUrl = "http://apistore.baidu.com/";
        // 創(chuàng)建一個(gè)  retrofit ,baseUrl必須有
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create()) // 用到了 com.squareup.retrofit2:adapter-rxjava:2.1.0'
                .build();
        // 利用 retrofit 創(chuàng)建一個(gè)接口
        WeatherApi apiService = retrofit.create(WeatherApi.class);

        // 利用接口創(chuàng)建一個(gè)Call對象
        Call<WeatherBean> weatherBeanCall = apiService.getWeather("beijing");
        // 請求入隊(duì)线定,回調(diào)請求成功或者失敗
        weatherBeanCall.enqueue(new Callback<WeatherBean>() {
            @Override
            public void onResponse(Call<WeatherBean> call, Response<WeatherBean> response) {
                System.out.println("====請求成功:"+response.body().retData.weather);
                mTvData.setText(response.body().retData.weather);
            }
            @Override
            public void onFailure(Call<WeatherBean> call, Throwable t) {
                System.out.println("====請求失敗");
                mTvData.setText(t.getMessage());
            }
        });

Retrofit可以參考這里:
Retrofit應(yīng)用小記

五娜谊、RxJava + Retrofit

簡單的demo,獲取文本笑話

    compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
    compile 'com.squareup.okhttp3:okhttp:3.3.1'
    compile 'com.squareup.okhttp3:okhttp:3.3.1'
    compile 'io.reactivex:rxandroid:1.2.1'
Re加Rx.gif

項(xiàng)目地址

先這樣斤讥,沒事再完善纱皆。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市芭商,隨后出現(xiàn)的幾起案子派草,更是在濱河造成了極大的恐慌,老刑警劉巖铛楣,帶你破解...
    沈念sama閱讀 218,122評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件近迁,死亡現(xiàn)場離奇詭異,居然都是意外死亡簸州,警方通過查閱死者的電腦和手機(jī)鉴竭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門歧譬,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人搏存,你說我怎么就攤上這事瑰步。” “怎么了璧眠?”我有些...
    開封第一講書人閱讀 164,491評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵缩焦,是天一觀的道長。 經(jīng)常有香客問我责静,道長袁滥,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,636評(píng)論 1 293
  • 正文 為了忘掉前任灾螃,我火速辦了婚禮呻拌,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘睦焕。我一直安慰自己,他們只是感情好靴拱,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,676評(píng)論 6 392
  • 文/花漫 我一把揭開白布垃喊。 她就那樣靜靜地躺著,像睡著了一般袜炕。 火紅的嫁衣襯著肌膚如雪本谜。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,541評(píng)論 1 305
  • 那天偎窘,我揣著相機(jī)與錄音乌助,去河邊找鬼。 笑死陌知,一個(gè)胖子當(dāng)著我的面吹牛他托,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播仆葡,決...
    沈念sama閱讀 40,292評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼赏参,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了沿盅?” 一聲冷哼從身側(cè)響起把篓,我...
    開封第一講書人閱讀 39,211評(píng)論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎腰涧,沒想到半個(gè)月后韧掩,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,655評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡窖铡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,846評(píng)論 3 336
  • 正文 我和宋清朗相戀三年疗锐,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了坊谁。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,965評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡窒悔,死狀恐怖呜袁,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情简珠,我是刑警寧澤阶界,帶...
    沈念sama閱讀 35,684評(píng)論 5 347
  • 正文 年R本政府宣布,位于F島的核電站聋庵,受9級(jí)特大地震影響膘融,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜祭玉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,295評(píng)論 3 329
  • 文/蒙蒙 一氧映、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧脱货,春花似錦岛都、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至扣孟,卻和暖如春烫堤,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背凤价。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評(píng)論 1 269
  • 我被黑心中介騙來泰國打工鸽斟, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人利诺。 一個(gè)月前我還...
    沈念sama閱讀 48,126評(píng)論 3 370
  • 正文 我出身青樓富蓄,卻偏偏與公主長得像,于是被迫代替她去往敵國和親慢逾。 傳聞我的和親對象是個(gè)殘疾皇子格粪,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,914評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容