1.BaseActivity文件
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.app.SkinAppCompatDelegateImpl;
import com.example.zixunapp.MainActivity;
/**
*基类 公众的方法
*
* abstract 抽象类
**/
public abstract class BaseActivity extends AppCompatActivity {
private Context mcontext;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mcontext=this;
setContentView(initLayout());
initView();
initData();
}
protected abstract int initLayout();
protected abstract void initView();
protected abstract void initData();
public void ShowToast(String msg){
Toast.makeText(mcontext,msg,Toast.LENGTH_SHORT).show();
}
public void ShowToastSync(String msg){
Looper.prepare();//消息队列自学
Toast.makeText(mcontext,msg,Toast.LENGTH_SHORT).show();
Looper.loop();
}
public void navigateTo(Class cls){
Intent i=new Intent(this, cls);
startActivity(i);
}
protected void saveStringToSp(String key,String val){
SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE);
SharedPreferences.Editor editor=sp.edit();
editor.putString(key,val);
editor.commit();
}
@NonNull
@Override
public AppCompatDelegate getDelegate() {
return SkinAppCompatDelegateImpl.get(this, this);
}
}
2.
BaseFragment文件
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.zixunapp.adapter.listener.OnItemChildClickListener;
import xyz.doikki.videoplayer.player.VideoViewManager;
public abstract class BaseFragment extends Fragment implements OnItemChildClickListener {
public View mRootView;
//onCreateView是创建该fragment对应的视图,必须在这里创建自己的视图并返回给调用者。
//onViewCreated在onCreateView执行完后立即执行.onCreateView返回的就是fragment要显示的view.
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//如果mRootView为空 创建mRootView,不为空 直接返回 复用mRootView
if(mRootView==null){
mRootView=inflater.inflate(initLayout(),container,false);
initView();//初始化view
}
return mRootView;
}
//onViewCreated在onCreateView执行完后立即执抄行.onCreateView返回的就袭是fragment要显示的view.
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initData();
}
protected abstract int initLayout();
protected abstract void initView();
protected abstract void initData();
public void ShowToast(String msg){
Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();
}
public void ShowToastSync(String msg){
Looper.prepare();//消息队列自学
Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();
Looper.loop();
}
public void navigateTo(Class cls){
Intent i=new Intent(getActivity(), cls);
startActivity(i);
}
public void navigateToWithBundle(Class cls, Bundle bundle) {
Intent in = new Intent(getActivity(), cls);
in.putExtras(bundle);
startActivity(in);
}
public void navigateToWithFlag(Class cls, int flags) {
Intent in = new Intent(getActivity(), cls);
in.setFlags(flags);
startActivity(in);
}
// 插入键
protected void saveStringToSp(String key,String val){
// 文件名 userInfo
SharedPreferences sp=getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sp.edit();
editor.putString(key,val);
editor.commit();
}
// 查询键
protected String getStringFromSp(String key){
// 文件名 userInfo
SharedPreferences sharedPreferences=getActivity().getSharedPreferences("userInfo",Context.MODE_PRIVATE);
String value=sharedPreferences.getString(key,"-1");
return value;
}
// 删除键
protected void removeStringToSp(String key){
// 文件名 userInfo
SharedPreferences sp=getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sp.edit();
editor.remove(key);
editor.commit();
}
//视频列表用
/**
* 子类可通过此方法直接拿到VideoViewManager
*/
protected VideoViewManager getVideoViewManager() {
return VideoViewManager.instance();
}
}