|
| 1 | +/* |
| 2 | + * Copyright 2017 GcsSloop |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * Last modified 2017-04-09 20:47:16 |
| 17 | + * |
| 18 | + * GitHub: https://github.com/GcsSloop |
| 19 | + * Website: http://www.gcssloop.com |
| 20 | + * Weibo: http://weibo.com/GcsSloop |
| 21 | + */ |
| 22 | + |
| 23 | +package com.gcssloop.diycode.activity; |
| 24 | + |
| 25 | +import android.content.Context; |
| 26 | +import android.content.Intent; |
| 27 | +import android.support.v4.app.FragmentManager; |
| 28 | +import android.support.v4.app.FragmentTransaction; |
| 29 | +import android.view.View; |
| 30 | + |
| 31 | +import com.gcssloop.diycode.R; |
| 32 | +import com.gcssloop.diycode.base.app.BaseActivity; |
| 33 | +import com.gcssloop.diycode.base.app.ViewHolder; |
| 34 | +import com.gcssloop.diycode.fragment.UserCollectionTopicFragment; |
| 35 | +import com.gcssloop.diycode.fragment.UserCreateTopicFragment; |
| 36 | +import com.gcssloop.diycode.utils.DataCache; |
| 37 | +import com.gcssloop.diycode_sdk.api.Diycode; |
| 38 | +import com.gcssloop.diycode_sdk.api.user.bean.UserDetail; |
| 39 | + |
| 40 | +import java.io.IOException; |
| 41 | + |
| 42 | +public class MyTopicActivity2 extends BaseActivity { |
| 43 | + private DataCache mDataCache; |
| 44 | + |
| 45 | + // 数据类型 |
| 46 | + enum InfoType { |
| 47 | + MY_TOPIC, MY_COLLECT |
| 48 | + } |
| 49 | + |
| 50 | + private InfoType current_type = InfoType.MY_TOPIC; |
| 51 | + |
| 52 | + public static void newInstance(Context context, InfoType type) { |
| 53 | + Intent intent = new Intent(context, MyTopicActivity2.class); |
| 54 | + intent.putExtra("type", type); |
| 55 | + context.startActivity(intent); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + protected int getLayoutId() { |
| 60 | + return R.layout.activity_topic; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + protected void initDatas() { |
| 65 | + mDiycode = Diycode.getSingleInstance(); |
| 66 | + mDataCache = new DataCache(this); |
| 67 | + Intent intent = getIntent(); |
| 68 | + InfoType type = (InfoType) intent.getSerializableExtra("type"); |
| 69 | + if (type != null) { |
| 70 | + current_type = type; |
| 71 | + } else { |
| 72 | + current_type = InfoType.MY_TOPIC; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + protected void initViews(ViewHolder holder, View root) { |
| 78 | + if (!mDiycode.isLogin()) { |
| 79 | + toastShort("用户未登录"); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + // 获取用户名 |
| 84 | + if (mDataCache.getMe() == null) { |
| 85 | + try { |
| 86 | + UserDetail me = mDiycode.getMeNow(); |
| 87 | + mDataCache.saveMe(me); |
| 88 | + } catch (IOException e) { |
| 89 | + e.printStackTrace(); |
| 90 | + } |
| 91 | + } |
| 92 | + String username = mDataCache.getMe().getLogin(); |
| 93 | + |
| 94 | + // 初始化 Fragment |
| 95 | + FragmentManager fragmentManager = getSupportFragmentManager(); |
| 96 | + FragmentTransaction transaction = fragmentManager.beginTransaction(); |
| 97 | + |
| 98 | + if (current_type == InfoType.MY_COLLECT){ |
| 99 | + setTitle("我的收藏"); |
| 100 | + UserCollectionTopicFragment fragment1 = UserCollectionTopicFragment.newInstance(username); |
| 101 | + transaction.add(R.id.fragment, fragment1); |
| 102 | + } else if (current_type == InfoType.MY_TOPIC){ |
| 103 | + setTitle("我的话题"); |
| 104 | + UserCreateTopicFragment fragment2 = UserCreateTopicFragment.newInstance(username); |
| 105 | + transaction.add(R.id.fragment, fragment2); |
| 106 | + } |
| 107 | + transaction.commit(); |
| 108 | + } |
| 109 | +} |
0 commit comments