BaseActivity
public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener,ViewPager.OnPageChangeListener,RadioGroup.OnCheckedChangeListener{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
findId();
initData();
}
/**
* 获取布局的id
* @return
*/
protected abstract int getLayoutId();
/**
* 找到控件的id
*/
protected abstract void findId();
/**
* 初始化数据的操作
*/
protected abstract void initData();
}
封装完成之后,创建一个activity继承baseActivity,并重写里面的方法
public class SecondActivity extends BaseActivity {
private Button button;
private Button button_02;
private RadioGroup radioGroup;
/**
* 关联当前activity显示的布局的id
* @return
*/
@Override
protected int getLayoutId() {
return R.layout.second_layout;
}
/**
* 找到当前activity上面的控件
*/
@Override
protected void findId() {
button = (Button) findViewById(R.id.button_01);
button_02 = (Button) findViewById(R.id.button_02);
radioGroup = (RadioGroup) findViewById(R.id.radio_group);
}
/**
* 有关数据有关逻辑的操作写在这里面
*/
@Override
protected void initData() {
//设置点击事件
button.setOnClickListener(this);
button_02.setOnClickListener(this);
//radioGroup监听
radioGroup.setOnCheckedChangeListener(this);
}
/**
* radioGroup的事件
* @param radioGroup
* @param i
*/
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i){
}
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button_01:
break;
case R.id.button_02:
break;
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
BaseFragment
public abstract class BaseFragment extends Fragment implements View.OnClickListener{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = initView(inflater,container);
return view;
}
/**
* 关联布局,,,并且找到控件id的操作
* @param inflater
* @param container
* @return
*/
protected abstract View initView(LayoutInflater inflater, ViewGroup container);
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//逻辑的操作
initData();
}
protected abstract void initData();
}
封装完成之后,创建一个fragment继承封装的basefragment,重写里面的方法
public class FragmnetNews extends BaseFragment {
@Override
protected View initView(LayoutInflater inflater, ViewGroup container) {
View view = inflater.inflate(R.layout.second_layout,container,false);
//找到id
return view;
}
@Override
protected void initData() {
//获取数据
//设置适配器
}
@Override
public void onClick(View view) {
}
}
封装json方法
在解析的时候调用
public class JsonUtil {
public static Gson gson = new Gson();
/**
* DataDataBean dataDataBean = gson.fromJson(s,DataDataBean.class);
*
* 这一一种解析数据的格式,,,{}对应,解析出来是一个实体类对象
*/
public static <T> T parseJson(String json,Class<T> tClass){
T t = gson.fromJson(json, tClass);
return t;
}
/**
* List<DataDataBean> listData = gson.fromJson(s,type);
*
* 这是第二种,解析出来是一个list集合
*/
}