1.总体浏览效果及布局分析

1.1.看一下我的帖子预览效果

  Diycode开源项目 MyTopicActivity分析

 

1.2.然后看一下我的收藏预览效果

  Diycode开源项目 MyTopicActivity分析

 

1.3.归纳一下

  所以我的帖子和我的收藏可以共用同一个类。

  布局效果几乎一样,只是用户的选择不同。

  所以今天我研究的就是共用的同一个类MyTopicActivity。

 

1.4.中间这个活动就是MyTopicActivity。布局为:activity_fragment.

  1.4.1.看一下布局的源代码。  

<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ Copyright 2017 GcsSloop
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~    http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  ~
  ~ Last modified 2017-04-09 19:39:57
  ~
  ~ GitHub:  https://github.com/GcsSloop
  ~ Website: http://www.gcssloop.com
  ~ Weibo:   http://weibo.com/GcsSloop
  -->

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_topic"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.gcssloop.diycode.activity.TopicActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

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

</LinearLayout>
View Code

 

  1.4.2.布局对应实体关系。

      Diycode开源项目 MyTopicActivity分析

 

  1.4.3.分析整体布局

      外层是一个LinearLayout线性布局。

      然后是Toolbar标题栏。

     然后是FrameLayout布局。

     深入理解FrameLayout布局点击我!


2.分析MyTopicActivity中的成员变量及部分函数

2.1.预览一下所有的变量。

  Diycode开源项目 MyTopicActivity分析

  这里使用了一个DataCache类,在通用类中的数据缓存工具,之前分析过。

  然后使用了一个枚举类型,enum InfoType{MY_TOPIC,MY_COLLECT}

  代表我的话题,我的收藏。

  建立一个枚举类型的变量,初始化为我的话题。

 

2.2.建立一个新的实例。

  Diycode开源项目 MyTopicActivity分析

  在外部可以直接用MyTopicActivity.newInstance来建立一个实例,来打开这个话题列表。

 

2.3.获取布局资源id。

   Diycode开源项目 MyTopicActivity分析

 

2.4.初始化数据。

  Diycode开源项目 MyTopicActivity分析

  在BaseActivity中执行顺序为:

    getLayoutId()==>initActionBar()==>initDatas()==>initViews()

  首先获取SDK中API的单例,然后建立一个缓存类。

  然后获取intent中的枚举类型,如果为空就默认“我的话题”。 


3.MyTopActivity关键函数initViews分析

3.1.首先看一下源代码。 

 @Override
    protected void initViews(ViewHolder holder, View root) {
        if (!mDiycode.isLogin()) {
            toastShort("用户未登录");
            return;
        }

        // 获取用户名
        if (mDataCache.getMe() == null) {
            try {
                UserDetail me = mDiycode.getMeNow();
                mDataCache.saveMe(me);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String username = mDataCache.getMe().getLogin();

        // 初始化 Fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        if (current_type == InfoType.MY_COLLECT){
            setTitle("我的收藏");
            UserCollectionTopicFragment fragment1 = UserCollectionTopicFragment.newInstance(username);
            transaction.add(R.id.fragment, fragment1);
        } else if (current_type == InfoType.MY_TOPIC){
            setTitle("我的话题");
            UserCreateTopicFragment fragment2 = UserCreateTopicFragment.newInstance(username);
            transaction.add(R.id.fragment, fragment2);
        }
        transaction.commit();
    }
View Code

 

3.2.预览一下这个函数。

  Diycode开源项目 MyTopicActivity分析

 

3.3.第一个if

  判断是否登录==>如果没有,就不存在我的话题和我的收藏了。

 

3.4.第二个if

  判断缓存中有没有我的信息,如果没有,调用API获得并存进缓存,如果有,那继续。

  

3.5.然后初始化碎片。

  采用方式:开启事务模式。

  Diycode开源项目 MyTopicActivity分析

  先从活动中得到FragmentManager,然后开启事务,动态加载碎片。

  然后判断枚举类型

  Diycode开源项目 MyTopicActivity分析

  最后提交事务  

  transaction.commit()。

相关文章:

  • 2021-05-18
  • 2021-08-21
  • 2022-02-09
  • 2021-11-21
  • 2022-01-27
  • 2021-08-08
  • 2021-07-02
  • 2021-05-13
猜你喜欢
  • 2021-06-17
  • 2021-12-03
  • 2021-10-15
  • 2021-08-10
  • 2022-02-10
  • 2021-05-24
  • 2022-02-20
相关资源
相似解决方案