編輯:關於 Android 程式設計
公司項目里有這樣一個需求:從網絡獲取 JSON 數據並解析後,動態添加按鈕,點擊時切換各按鈕下存儲的對應數據。

這里僅為演示動態添加 RadioButton,所以數據直接用 Fragment 替換,效果如下:

MainActivity:
package com.ut.radiobuttondynamicadd;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.ut.radiobuttondynamicadd.factory.UserFragmentFactory;
import butterknife.ButterKnife;
import butterknife.InjectView;
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
@InjectView(R.id.radiogroup_single) RadioGroup mRadioGroup;
@InjectView(R.id.fl_info_framlayout) FrameLayout mFlInfoFramlayout;
//注意:因為是測試,數組個數不能超過自定義 UserFragment 的個數
private String[] mStrContents = {"孫悟空", "豬八戒", "沙和尚", "白龍馬", "唐僧"};
private RadioButton mFirstButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
initRadioButton();
}
//動態添加 RadioButton
private void initRadioButton() {
for (int i = 0; i < mStrContents.length; i++) {
RadioButton tempButton = new RadioButton(this);
tempButton.setBackgroundResource(R.drawable.main_radiobutton_bg_selector);
tempButton.setButtonDrawable(android.R.color.transparent);
tempButton.setPadding(30, 0, 0, 0);
tempButton.setText(mStrContents[i]);
if (i == 0) {
mFirstButton = tempButton;
}
mRadioGroup.addView(tempButton, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}
mRadioGroup.setOnCheckedChangeListener(this);
if (mFirstButton != null) {
mFirstButton.setChecked(true);
}
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = (RadioButton) findViewById(checkedId);
if (rb.isChecked()) {
for (int i = 0; i < mStrContents.length; i++) {
if (rb.getText().equals(mStrContents[i])) {
Log.d("onCheckedChanged", "當前選中界面:" + i);
changeFragment(UserFragmentFactory.createFragment(i));
}
}
}
}
private void changeFragment(Fragment targetFragment) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fl_info_framlayout, targetFragment, "fragment")
.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.commit();
}
}
以上基本就是核心代碼。下面關於 Fragment:
MyBaseFragment:
package com.ut.radiobuttondynamicadd.fragment;
import android.support.v4.app.Fragment;
public class MyBaseFragment extends Fragment { }
UserFragment1(其餘類似):
package com.ut.radiobuttondynamicadd.fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class UserFragment1 extends MyBaseFragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView textView = new TextView(getContext());
textView.setText("第一個界面");
textView.setTextColor(Color.RED);
return textView;
}
}
存儲 Fragment 的工廠類 UserFragmentFactory:
package com.ut.radiobuttondynamicadd.factory;
import com.ut.radiobuttondynamicadd.fragment.MyBaseFragment;
import com.ut.radiobuttondynamicadd.fragment.UserFragment1;
// ... 其他 UserFragment
import java.util.HashMap;
public class UserFragmentFactory {
private static HashMap<Integer, MyBaseFragment> hashMap = new HashMap<>();
public static MyBaseFragment createFragment(int arg0) {
//內存中若已有對應 fragment 則複用,否則創建
MyBaseFragment fragment = hashMap.get(arg0);
if (fragment != null) {
return fragment;
} else {
switch (arg0) {
case 0: fragment = new UserFragment1(); break;
case 1: fragment = new UserFragment2(); break;
// ...
}
hashMap.put(arg0, fragment);
return fragment;
}
}
}
布局文件:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical">
<RadioGroup android:id="@+id/radiogroup_single" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<FrameLayout android:id="@+id/fl_info_framlayout" android:layout_width="200dp" android:layout_height="200dp" android:background="#4400ffff">
</FrameLayout>
</RadioGroup>
</LinearLayout>
這里順帶補充一句:項目中我雖然採用了動態添加 RadioButton,但實際項目里支持左右切換數據的界面並非用 Fragment,而是用 ViewPager 實現的。
Android ProgressBar 進度條使用筆記
進度條能讓使用者知道目前任務的執行進度。特別是執行時間較長的操作,若沒有任何回饋,使用者會以為程式當機而直接關閉。 常見的進度條可以分成三類: 1. 有明確進度資訊的,用
MPAndroidChart 圖表庫(十二):正負堆疊條形圖實作
接續上一篇。這次要畫的圖表與前一篇幾乎同構,只是數值有了正反兩面——一個人數分布圖,男性朝左、女性朝右。先看效果: 因為結構相似,程式碼也大同小異,直接看基本實作。 一
Android Master Key 漏洞解析:成因、風險與修復概念
Android Master Key 漏洞,官方編號 ANDROID-8219321,是早期 Android 簽名驗證機制中的重大安全問題。它讓攻擊者有機會在 APK
Android Notification 進階玩法:實作可展開的「寬視圖」通知欄
Notification 是應用程式在常規介面之外向使用者展示訊息的機制。當 App 呼叫系統發送通知時,訊息會先以圖示的形式出現在通知欄;要看詳細內容,則需要下拉開啟通