1.json 2.HotFragment ...
1.json
2.HotFragment
/** * 排行 * * @author Kevin * */ public class HotFragment extends BaseFragment { private ArrayList<String> mList; @Override public View onCreateSuccessView() { int padding = UIUtils.dip2px(10); // 為了使佈局可以上下滑動,需要用ScrollView包裝起來 ScrollView scrollView = new ScrollView(UIUtils.getContext()); // 設置ScrollView邊距 scrollView.setPadding(padding, padding, padding, padding); // 初始化自定義控制項 FlowLayout flow = new FlowLayout(UIUtils.getContext()); // 水平間距 flow.setHorizontalSpacing(UIUtils.dip2px(6)); // 豎直間距 flow.setVerticalSpacing(UIUtils.dip2px(8)); // 根據介面返回的數據個數,動態添加TextView for (final String str : mList) { TextView view = new TextView(UIUtils.getContext()); view.setText(str); view.setTextColor(Color.WHITE); view.setGravity(Gravity.CENTER); view.setPadding(padding, padding, padding, padding); view.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); // 設置隨機文字顏色 Random random = new Random(); int r = 30 + random.nextInt(210); int g = 30 + random.nextInt(210); int b = 30 + random.nextInt(210); int color = 0xffcecece;// 按下後偏白的背景色 // 根據預設顏色和按下顏色, 生成圓角矩形的狀態選擇器 Drawable selector = DrawableUtils.getStateListDrawable( Color.rgb(r, g, b), color, UIUtils.dip2px(6)); // 給TextView設置背景 view.setBackgroundDrawable(selector); // 必須設置點擊事件, TextView按下後顏色才會變化 view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(UIUtils.getContext(), str, Toast.LENGTH_SHORT).show(); } }); // 給自定義控制項添加view對象 flow.addView(view); } scrollView.addView(flow); return scrollView; } @Override public ResultState onLoad() { HotProtocol protocol = new HotProtocol(); mList = protocol.getData(0); return check(mList); } }