lmw
2023-06-13 4b7d8d9a038f6522df46d0f14fa07eb940a1b34d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.kuanzhai.driver.base;
 
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;
 
 
import com.kuanzhai.driver.R;
import com.kuanzhai.driver.ui.DialogUtil;
import com.kuanzhai.driver.utils.EasePopup.EasyPopup;
import com.google.gson.Gson;
 
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
 
import java.util.List;
 
import cn.sinata.xldutils.fragment.BaseFragment;
import pub.devrel.easypermissions.EasyPermissions;
 
 
/**
 * MyBaseFragment
 *
 * @author Administrator
 * @date 2018/5/16
 */
public abstract class MyBaseFragment extends BaseFragment implements EasyPermissions.PermissionCallbacks {
    protected final int PAGE_SIZE = 10;//每页条数
    protected int pageNumber = 1;//当前第几页
    public FragmentActivity mContext;
    public EasyPopup easyPopup;
    public Gson gson = new Gson();
 
    @Override
    protected int getContentViewLayoutID() {
        return 0;
    }
 
    @Override
    protected void onFirstVisibleToUser() {
 
    }
 
    @Override
    protected void onVisibleToUser() {
 
    }
 
    @Override
    protected void onInvisibleToUser() {
 
    }
 
    /***
     * 添加一个fragment 到栈
     * @param fragment 目标类
     * @param container 容器id
     */
    public void addFragment(Fragment fragment, int container) {
        FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
        if (!fragment.isAdded()) {
            fragmentTransaction.add(container, fragment);
        }
        for (Fragment currentFragment : getChildFragmentManager().getFragments()) {
            if (currentFragment.isAdded() && currentFragment != fragment) {
                fragmentTransaction.hide(currentFragment);
            }
        }
        fragmentTransaction.show(fragment);
        fragmentTransaction.commitAllowingStateLoss();
        getChildFragmentManager().executePendingTransactions();
    }
 
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        View view = inflater.inflate(layoutId(),container,false);
        View view = LayoutInflater.from(getActivity()).inflate(layoutId(),container,false);
        mContext = getActivity();
        EventBus.getDefault().register(this);
        return view;
    }
 
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }
 
    @Override
    public void onResume() {
        super.onResume();
    }
 
    public abstract int layoutId();
 
    public abstract void initView();
 
 
    public void startActivity(Class tClass){
        startActivity(new Intent(mContext,tClass));
    }
 
    @Override
    public void showDialog() {
//        super.showDialog();
        if (easyPopup == null){
            easyPopup = DialogUtil.INSTANCE.getPopupwindow(getActivity(), R.layout.progress_dialog_main);
        }
        if (easyPopup.isShowing()){
            return;
        }
        FragmentActivity activity = getActivity();
        if (activity != null && activity.getWindow() != null) {
            try {
                getActivity().getWindow().getDecorView().post(() -> easyPopup.showCenter(getActivity().getWindow().getDecorView()));
            }catch (Exception e){
 
            }
        }
    }
 
    @Override
    public void onPause() {
        super.onPause();
        if (easyPopup != null && easyPopup.isShowing()){
            easyPopup.dismiss();
        }
    }
 
    @Override
    public void dismissDialog() {
//        super.dismissDialog();?
        if (easyPopup == null){
            easyPopup = DialogUtil.INSTANCE.getPopupwindow(getActivity(),R.layout.progress_dialog_main);
        }
        easyPopup.dismiss();
    }
 
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
 
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(BaseEvent event) {
 
    }
 
    //成功打开权限
    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
 
    }
 
    //用户未同意权限
    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
 
    }
 
 
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }
 
    //    获取权限
    public boolean checkPermissions(String... per) {
        if (EasyPermissions.hasPermissions(requireContext(), per)) {
            //已经打开权限
            return true;
        } else {
            return false;
        }
    }
}