Activity 向几个FramLayout 中添加View来展示
public class DetailActivity extends Activity {
@BindView(R.id.fl_app_info)
FrameLayout flAppInfo;
@BindView(R.id.fl_app_safe)
FrameLayout flAppSafe;
@BindView(R.id.fl_app_pic)
FrameLayout flAppPic;
@BindView(R.id.fl_app_des)
FrameLayout flAppDes;
@BindView(R.id.scrollview)
ScrollView scrollview;
private String packagename;
private LoadingPage loadingPage;
private HomeInfo.AppInfo detailAppInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
packagename = getIntent().getStringExtra("packagename");
loadingPage = new LoadingPage(UIUitls.getContext()) {
@Override
public ResultState onLoad() {
return onSubLoad();
}
@Override
public View onCreateSuccessedView() {
return onSubCreateSuccessedView();
}
};
//因为在loadingPage中包含了4中界面展示情况,所以将loadingPage作为activity页面显示内容
setContentView(loadingPage);
//在show方法中会触发onLoad方法,在onLoad方法中需要请求网络获取结果,结果还必须是(ResultState)限定类型
loadingPage.show();
}
private View onSubCreateSuccessedView() {
View view = UIUitls.inflate(R.layout.activity_detail);
ButterKnife.bind(this,view);
//1 将顶部应用基本信息添加至帧布局中
AppInfoModule appInfoModule = new AppInfoModule(UIUitls.getContext());
appInfoModule.bindData(detailAppInfo);
flAppInfo.addView(appInfoModule.view);
//2 安全相关
AppSafeModule appSafeModule = new AppSafeModule(UIUitls.getContext());
appSafeModule.bindData(detailAppInfo);
flAppSafe.addView(appSafeModule.view);
//3 图片相关
AppPicModule appPicModule = new AppPicModule(UIUitls.getContext());
appPicModule.bindData(detailAppInfo);
flAppPic.addView(appPicModule.view);
//4 详情相关
AppDesModule appDesModule = new AppDesModule(UIUitls.getContext(),scrollview);
appDesModule.bindData(detailAppInfo);
flAppDes.addView(appDesModule.view);
return view;
}
private LoadingPage.ResultState onSubLoad() {
DetailProtocol detailProtocol = new DetailProtocol();
detailAppInfo = detailProtocol.getData(
"detail", 0, "&packageName=" + packagename);
if (detailAppInfo == null) {
return LoadingPage.ResultState.RESULT_STATE_ERROR;
}
return LoadingPage.ResultState.RESULT_STATE_SUCCESSED;
}
}
2 BaseMoudle和4个子Moudlue
public abstract class BaseModule<T>{
private Context mCtx;
public View view;
public BaseModule(Context ctx){
this.mCtx = ctx;
view = initView();
}
//因为在父类中不知道详情4个拆分出来模块的具体布局效果
public abstract View initView();
//因为在父类中不知道详情4个拆分出来模块的具体数据也不知道控件有哪些,所以提供一个给控件填充数据方法
public abstract void bindData(T t);
}
子Moudlue
public class AppPicModule extends BaseModule<HomeInfo.AppInfo> {
@BindView(R.id.ll_pic)
LinearLayout llPic;
private ArrayList<String> screenList;
public AppPicModule(Context ctx) {
super(ctx);
}
@Override
public View initView() {
View view = UIUitls.inflate(R.layout.layout_app_pic);
ButterKnife.bind(this,view);
return view;
}
@Override
public void bindData(HomeInfo.AppInfo appInfo) {
screenList = (ArrayList<String>) appInfo.getScreen();
llPic.removeAllViews();
int width = UIUitls.dip2px(90);
int height = UIUitls.dip2px(150);
int margin = UIUitls.dip2px(6);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
for (int i = 0; i < screenList.size(); i++) {
ImageView imageView = new ImageView(UIUitls.getContext());
Glide.with(UIUitls.getContext())
.load(Constant.IMG+ screenList.get(i)).into(imageView);
params.setMargins(margin,margin,0,margin);
llPic.addView(imageView,params);
final int finalI = i;
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(UIUitls.getContext(), ImageScaleActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("screenList", screenList);
intent.putExtra("index", finalI);
UIUitls.getContext().startActivity(intent);
}
});
}
}
}