ViewBinding的封装使用

在build中添加
android{
...
 viewBinding {
        enabled = true
    }
...
}
BaseActivity的代码如下
public abstract class BaseActivity< T extends ViewBinding> extends AppCompatActivity implements Contract.IView {

    private LoadDialog mLoadingDialog;
    public BasePresenter mPresenter;
    private Unbinder bind;
    private View rootView;
    protected T binding;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        if (setLayoutResource() != 0) {
            setStatusBar();
            mLoadingDialog = new LoadDialog(this);
            mLoadingDialog.setCancelable(false);
            Type type = getClass().getGenericSuperclass();
            if (type instanceof ParameterizedType) {
                try {
                    Class clazz = (Class) ((ParameterizedType) type).getActualTypeArguments()[0];
                    Method method = clazz.getMethod("inflate", LayoutInflater.class);
                    binding = (T) method.invoke(null, getLayoutInflater());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                setContentView(binding.getRoot());
            }

            mPresenter = new PresenterImpl();
            if (mPresenter != null) {
                mPresenter.attach(this);
            }
//            bind = ButterKnife.bind(this);
            initView();
        }
    }

    protected abstract int setLayoutResource();

    protected void setStatusBar() {
        StatusBarUtil.setTransparentForImageView(this, null);
    }


    protected abstract void initView();


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onEnd();
        }
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
            mLoadingDialog = null;
        }
        if (bind != null) {
            bind.unbind();
        }
    }

    @Override
    public void showLoading() {
        if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
        }
    }

    @Override
    public void hideShowing() {
        if (mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
    }

    @Override
    public void finish() {
        super.finish();
    }

}
activity中的使用
public class LjzActivity extends BaseActivity {
//    @BindView(R.id.viewpager)
//    ViewPager viewPager;
//    @BindView(R.id.tablayout)
//    XTabLayout tabLayout;

    private List fragments = new ArrayList<>();
    private List titles = new ArrayList<>();

    @Override
    protected int setLayoutResource() {
        return R.layout.activity_ljz;
    }

    @Override
    protected void initView() {
        titles.add("tab1");
        titles.add("tab2");
        titles.add("tab3");
        titles.add("tab4");
        fragments.add(new LjzLazyFragment1());
        fragments.add(new LjzLazyFragment2());
        fragments.add(new LjzLazyFragment3());
        fragments.add(new LjzLazyFragment4());
        binding.viewpager.setAdapter(new BaseViewPagerAdapter(getSupportFragmentManager(), 
    FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, fragments, titles));
        binding.tablayout.setupWithViewPager(binding.viewpager);
        binding.viewpager.setCurrentItem(0);
//        viewPager.setOffscreenPageLimit(3);
    }

    @Override
    public void onSuccess(Object o) {

    }

    @Override
    public void onError(String error) {

    }


}

BaseFragment的代码如下
  public BasePresenter mPresenter;
    private Unbinder bind;
    private LoadDialog loadDialog;
    private View view;
    private boolean isFirstLoad = true; // 是否第一次加载
    protected T binding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.CONTENTS_FILE_DESCRIPTOR);
        }

//        view = inflater.inflate(Layout(), container, false);
//        bind = ButterKnife.bind(this, view);
        Type type = getClass().getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            try {
                Class clazz = (Class) ((ParameterizedType) type).getActualTypeArguments()[0];
                Method method = clazz.getMethod("inflate", LayoutInflater.class);
                binding = (T) method.invoke(null, getLayoutInflater());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        loadDialog = new LoadDialog(getActivity());
        loadDialog.setCancelable(false);
        initView(savedInstanceState);

        return binding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isFirstLoad){
            initData();
//            isFirstLoad = false;
        }
    }

    protected abstract void initData();



    protected abstract void initView(Bundle savedInstanceState);


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onEnd();
        }
    }



freagment中的使用
public class LjzLazyFragment4 extends BaseLazyFragment {
    @BindView(R.id.text)
    TextView textView;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.smart)
    SmartRefreshLayout smartRefreshLayout;
    private List data = new ArrayList<>();

    @Override
    protected void initData() {
        LogUtils.error("fragment3");
        binding.refreshload.smart.autoRefresh();
    }


//    @Override
//    protected int Layout() {
//        return R.layout.fragment_ljz1;
//    }

    @Override
    protected void initView(Bundle savedInstanceState) {

        for (int i = 0; i < 20; i++) {
            data.add(i + "");
        }
        binding.refreshload.recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));

        MyAdapter myAdapter = new MyAdapter(R.layout.activity_recycler_item, data);
        binding.refreshload.recyclerview.setAdapter(myAdapter);

        //条目点击事件
        myAdapter.setOnItemClickListener((adapter, view, position) -> ToastUtils.showToast("条目点击事件"));
        binding.refreshload.smart.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                binding.refreshload.smart.finishLoadMore();
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                binding.refreshload.smart.finishRefresh();

            }
        });

    }
}

popupwindow中的使用 :布局写了之后,如果找不到PopupItemBinding,需要编译一下项目
  public void showPopup(View view) {
        lightoff();
        //以前使用的
//        View popupView = View.inflate(this, R.layout.popup_item, null);


        //viewbinding使用
        PopupItemBinding popupItemBinding = PopupItemBinding.inflate(LayoutInflater.from(this));
        
        
        PopupWindow popupWindow = new PopupWindow(popupItemBinding.getRoot(), WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setOnDismissListener(() -> lighton());

        popupItemBinding.textview.setText("popupwindow");

        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);

        popupWindow.setOutsideTouchable(true);

        TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_PARENT, 0);
        animation.setInterpolator(new AccelerateInterpolator());
        animation.setDuration(100);

        if (popupWindow.isShowing()) {
            popupWindow.dismiss();
            lighton();
        }
        popupItemBinding.getRoot().startAnimation(animation);
        popupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);


    }

    /**
     * 设置手机屏幕亮度变暗
     */
    public void lightoff() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.3f;
        getWindow().setAttributes(lp);
    }

    /**
     * 设置手机屏幕亮度显示正常
     */
    public void lighton() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 1f;
        getWindow().setAttributes(lp);
    }
include中的使用,需要加入一个id
    
   binding.commontitle.title.setText("列表");

你可能感兴趣的:(ViewBinding的封装使用)