Skip to content

Instantly share code, notes, and snippets.

@tendoasan
Last active April 27, 2019 03:32
Show Gist options
  • Save tendoasan/4d0c9bf47005cd18c26d803653e29fed to your computer and use it in GitHub Desktop.
Save tendoasan/4d0c9bf47005cd18c26d803653e29fed to your computer and use it in GitHub Desktop.
[Android-Fragment]#Android
public class InflateViewsFragment extends Fragment {
@BindView(R.id.ll_roots)
LinearLayout mLlRoots; // 容器
Unbinder mUnBinder;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_simple, container, false);
mUnBinder = ButterKnife.bind(this, view);
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
mUnBinder.unbind();
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
addView(mLlRoots);
}
private void addView(LinearLayout ll_roots){
View view = LayoutInflater.from(getActivity()).inflate(R.layout.inflate_root, null);
LinearLayout ll_root = (LinearLayout) view.findViewById(R.id.ll_root);
TextView tv_operator = (TextView) view.findViewById(R.id.tv_operator);
tv_operator.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_del_red, 0, 0, 0); // 设置 drawableLeft
ll_roots.addView(view);
}
}
public abstract class SimpleFragment extends Fragment {
Unbinder mUnBinder;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initEventAndData();
}
@Override
public void onResume() {
super.onResume();
queryData();
}
protected abstract void initEventAndData();
protected void queryData(){}
@LayoutRes
protected abstract int getLayoutId();
/*@LayoutRes
protected int getLayoutId(){
return R.layout.fragment_simple;
}*/
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(getLayoutId(), container, false);
mUnBinder = ButterKnife.bind(this, view);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onDestroyView() {
super.onDestroyView();
mUnBinder.unbind();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment