Skip to content

chengzhuangjie/MyUtils

 
 

Repository files navigation

自用Android工具类

第版本4
Github地址

适配器

RecyclerView 适配器
BaseAdapter
QuickAdapter

ViewPager 适配器
PagerAdapter
PagerAdapter2

基类

Activity
BaseActivity

Fragment
BaseFragment

对话框
BaseDialog
BaseBottomSheetDialog
BasePopupWindow

工具类

Activity堆栈管理工具类 AppManagerUtils

Assets文件管理工具类 AssetsUtils

Base64工具类 Base64Utils

bitmap图片工具类 BitmapUtils

设备工具信息工具 EquipmentUtils

文件工具类 FileUtils

glide图片加载工具类 GlideUtils

系统http工具类 LoadUrlUtils

log日志工具类 LogsUtils

网络检测工具类 NetUtils

SharedPreferences本地xml储存工具类 SharedPreferencesUtils

时间工具类 TimeUtils

Toast工具类 ToastUtils

Ui相关工具类 UiUtils

使用

导入

  1. 项目build.gradle导入
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
  1. app build.gradle导入
dependencies {
    implementation 'com.github.pikachu0621:MyUtils:0.0.4'
}
  1. 在app build.gradle 开启 viewBinding
android {
    ...
    viewBinding {
        enabled = true
    }
}

QuickAdapter (RecyclerView单布局)

public class TestQuickAdapter extends QuickAdapter<ActivityMainBinding, String> {

    public TestQuickAdapter(List<String> data) {
        super(data);
    }

    @Override
    public void onQuickBindView(ActivityMainBinding binding, String itemData, int position, List<String> data) {

    }
}

BaseAdapter (RecyclerView多布局)

public class Test2QuickAdapter extends BaseAdapter<String> {

    public Test2QuickAdapter(List<String> data) {
        super(data);
    }

    @Override
    public Class<? extends ViewBinding> onCreateView(ViewGroup parent, int viewType) {
        // 根据 viewType 返回布局 Binding
        if (viewType == 1) {
            return ActivityMainBinding.class;
        } else if (viewType == 10) {
            return ActivityMainBinding.class;
        }
        return ActivityMainBinding.class;
    }

    @Override
    public void onBindView(ViewBinding binding, String itemData, int position, int itemViewType, List<String> data) {

    }

    @Override
    public int getItemViewType(int position, String itemData) {
        //根据position 返回 viewType
        return 0;
    }
}

Activity用法

public class MainActivity extends BaseActivity<ActivityMainBinding> {
    @Override
    protected void initActivity(Bundle savedInstanceState) {
        // 业务逻辑
        // binding.getRoot().setVisibility(View.VISIBLE);
    }
}

Fragment用法(支持懒加载)

public class MainFragment extends BaseFragment<FragmentMainBinding> {

    @Override
    protected void onInitView(Bundle savedInstanceState, FragmentMainBinding binding, FragmentActivity activity) {
        // 业务逻辑
        // binding.getRoot().setVisibility(View.VISIBLE);
    }

    @Override
    protected void lazyLoad() {
        // 懒加载 第一次对用户可见时调用
        // binding.getRoot().setVisibility(View.VISIBLE);
    }
}

对话框

public class TestDialog {
    
    private final Context context;
    public TestDialog(Context context) {
        this.context = context;
    }
    
    /**
     * 普通对话框
     * 背景默认透明
     */
    public void testBaseDialog(){
        BaseDialog<ActivityMainBinding> dialog = new BaseDialog<ActivityMainBinding>(context) {
            @Override
            protected void onViewCreate(ActivityMainBinding binding) {
                // 业务逻辑
                // binding.getRoot().setOnClickListener();
            }
        };
        dialog.show();
    }


    /**
     * 底部弹出对话框
     * 背景默认透明
     */
    public void testBaseBottomSheetDialog(){
        BaseBottomSheetDialog<ActivityMainBinding> dialog = new BaseBottomSheetDialog<ActivityMainBinding>(context) {
            @Override
            protected void onViewCreate(ActivityMainBinding binding) {
                // 业务逻辑
                // binding.getRoot().setOnClickListener();
            }
        };
        dialog.show();
    }


    /**
     * 控件悬浮对话框
     * 背景默认透明
     */
    public void testBasePopupWindow(View view){
        BasePopupWindow<ActivityMainBinding> dialog = new BasePopupWindow<ActivityMainBinding>(context) {
            @Override
            public void onViewCreate(ActivityMainBinding binding) {
                // 业务逻辑
                // binding.getRoot().setOnClickListener();
            }
        };

        dialog.showAsTop(view);
    }
}

持续添加中

混淆

-keep class  com.pikachu.databinding.* {*;}

About

自用 - Android utils 工具类

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%