public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
initData();
setOnClickListener();
}
//代替Findviewbtid的方法
protected <T extends View> T getid(int id) {
return (T) findViewById(id);
}
//查找子布局id的方法
protected <T extends View> T getchildid(View view,int id) {
return (T) view.findViewById(id);
}
public abstract void initView();
public abstract void initData();
public abstract void setOnClickListener();
}
public class MainActivity extends BaseActivity {
private List<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void initView() {
setContentView(R.layout.activity_main);
//调用父类的获取id的方法
ListView tv = getid(R.id.tv);
View inflate = View.inflate(this, R.layout.mainactivity_child, null);
//调用父类获取子id的方法
ImageView getchildid = getchildid(inflate, R.id.maactivity_iv);
tv.addHeaderView(inflate);
}