Skip to content

Commit

Permalink
增加ViewPager2使用案例
Browse files Browse the repository at this point in the history
  • Loading branch information
xWX916614 committed May 29, 2020
1 parent 513361f commit 71128d2
Show file tree
Hide file tree
Showing 8 changed files with 422 additions and 17 deletions.
9 changes: 6 additions & 3 deletions app/src/main/java/com/xuexiang/xuidemo/MyApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.xuexiang.xormlite.annotation.DataBase;
import com.xuexiang.xormlite.enums.DataBaseType;
import com.xuexiang.xui.XUI;
import com.xuexiang.xuidemo.utils.SettingSPUtils;
import com.xuexiang.xuidemo.utils.sdkinit.ANRWatchDogInit;
import com.xuexiang.xuidemo.utils.sdkinit.AutoCameraStrategy;
import com.xuexiang.xuidemo.utils.sdkinit.BuglyInit;
Expand Down Expand Up @@ -40,9 +41,9 @@ protected void attachBaseContext(Context base) {
@Override
public void onCreate() {
super.onCreate();
initUI();
//初始化基础库
XBasicLibInit.init(this);
initUI();
//三方SDK初始化
XUpdateInit.init(this);
TbsInit.init(this);
Expand All @@ -61,8 +62,10 @@ public void onCreate() {
private void initUI() {
XUI.init(this);
XUI.debug(MyApp.isDebug());
// //设置默认字体为华文行楷
XUI.getInstance().initFontStyle("fonts/hwxk.ttf");
if (SettingSPUtils.getInstance().isUseCustomFont()) {
//设置默认字体为华文行楷
XUI.getInstance().initFontStyle("fonts/hwxk.ttf");
}
PictureFileUtils.setAppName("xui");

//字体图标库
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@

package com.xuexiang.xuidemo.fragment;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.widget.CompoundButton;

import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xui.widget.dialog.DialogLoader;
import com.xuexiang.xui.widget.textview.supertextview.SuperTextView;
import com.xuexiang.xuidemo.R;
import com.xuexiang.xuidemo.activity.MainActivity;
import com.xuexiang.xuidemo.base.BaseFragment;
import com.xuexiang.xuidemo.utils.SettingSPUtils;
import com.xuexiang.xutil.XUtil;
import com.xuexiang.xutil.app.AppUtils;
import com.xuexiang.xutil.app.IntentUtils;

import butterknife.BindView;

Expand All @@ -34,10 +40,11 @@
* @since 2019-09-17 17:51
*/
@Page(name = "设置")
public class SettingFragment extends BaseFragment implements CompoundButton.OnCheckedChangeListener {
public class SettingFragment extends BaseFragment {
@BindView(R.id.stv_switch_custom_theme)
SuperTextView stvSwitchCustomTheme;

@BindView(R.id.stv_switch_custom_font)
SuperTextView stvSwitchCustomFont;
/**
* 布局的资源id
*
Expand All @@ -55,17 +62,34 @@ protected int getLayoutId() {
protected void initViews() {
stvSwitchCustomTheme.setSwitchIsChecked(SettingSPUtils.getInstance().isUseCustomTheme());
stvSwitchCustomTheme.setOnSuperTextViewClickListener(superTextView -> stvSwitchCustomTheme.setSwitchIsChecked(!stvSwitchCustomTheme.getSwitchIsChecked(), false));
stvSwitchCustomTheme.setSwitchCheckedChangeListener(this);
}
stvSwitchCustomTheme.setSwitchCheckedChangeListener((buttonView, isChecked) -> {
SettingSPUtils.getInstance().setIsUseCustomTheme(isChecked);
popToBack();
//重启主页面
Intent intent = new Intent(getContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
});

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SettingSPUtils.getInstance().setIsUseCustomTheme(isChecked);
popToBack();
stvSwitchCustomFont.setSwitchIsChecked(SettingSPUtils.getInstance().isUseCustomFont());
stvSwitchCustomFont.setOnSuperTextViewClickListener(superTextView -> stvSwitchCustomFont.setSwitchIsChecked(!stvSwitchCustomFont.getSwitchIsChecked(), false));
stvSwitchCustomFont.setSwitchCheckedChangeListener((buttonView, isChecked) -> {
DialogLoader.getInstance().showTipDialog(getContext(), -1, "切换字体", "切换字体需重启App后生效, 点击“重启”应用将自动重启!", "重启", (dialog, which) -> {
SettingSPUtils.getInstance().setIsUseCustomFont(isChecked);
//重启app
rebootApp();
});
});
}

//重启主页面
Intent intent = new Intent(getContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
private void rebootApp() {
Intent intent = IntentUtils.getLaunchAppIntent(XUtil.getContext().getPackageName());
PendingIntent restartIntent = PendingIntent.getActivity(XUtil.getContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager mgr = (AlarmManager) XUtil.getContext().getSystemService(Context.ALARM_SERVICE);
if (mgr != null) {
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 500, restartIntent);
}
//退出程序
AppUtils.exitApp();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.xuexiang.xuidemo.base.ComponentContainerFragment;
import com.xuexiang.xuidemo.fragment.components.tabbar.tablayout.TabLayoutCacheFragment;
import com.xuexiang.xuidemo.fragment.components.tabbar.tablayout.TabLayoutSimpleFragment;
import com.xuexiang.xuidemo.fragment.components.tabbar.tablayout.TabLayoutViewPager2Fragment;

/**
* @author xuexiang
Expand All @@ -32,7 +33,8 @@ public class TabLayoutFragment extends ComponentContainerFragment {
protected Class[] getPagesClasses() {
return new Class[]{
TabLayoutSimpleFragment.class,
TabLayoutCacheFragment.class
TabLayoutCacheFragment.class,
TabLayoutViewPager2Fragment.class
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright (C) 2020 xuexiangjys([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.xuexiang.xuidemo.fragment.components.tabbar.tablayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

/**
* @author xuexiang
* @since 2020/5/21 1:27 AM
*/
public class FragmentStateViewPager2Adapter extends FragmentStateAdapter {

private List<Fragment> mFragmentList = new ArrayList<>();

private List<String> mTitleList = new ArrayList<>();

private List<Long> mIds = new ArrayList<>();

private AtomicLong mAtomicLong = new AtomicLong(0);

public FragmentStateViewPager2Adapter(@NonNull Fragment fragment) {
super(fragment);
}

@NonNull
@Override
public Fragment createFragment(int position) {
return mFragmentList.get(position);
}

public FragmentStateViewPager2Adapter addFragment(Fragment fragment, String title) {
if (fragment != null) {
mFragmentList.add(fragment);
mTitleList.add(title);
mIds.add(getAtomicGeneratedId());
}
return this;
}

public FragmentStateViewPager2Adapter addFragment(int index, Fragment fragment, String title) {
if (fragment != null && index >= 0 && index <= mFragmentList.size()) {
mFragmentList.add(index, fragment);
mTitleList.add(index, title);
mIds.add(index, getAtomicGeneratedId());
}
return this;
}

public FragmentStateViewPager2Adapter removeFragment(int index) {
if (index >= 0 && index < mFragmentList.size()) {
mFragmentList.remove(index);
mTitleList.remove(index);
mIds.remove(index);
}
return this;
}

private long getAtomicGeneratedId() {
return mAtomicLong.incrementAndGet();
}

@Override
public int getItemCount() {
return mFragmentList.size();
}

public void clear() {
mFragmentList.clear();
mTitleList.clear();
mIds.clear();
notifyDataSetChanged();
}

public CharSequence getPageTitle(int position) {
return mTitleList.get(position);
}

@Override
public long getItemId(int position) {
return mIds.get(position);
}

@Override
public boolean containsItem(long itemId) {
return mIds.contains(itemId);
}
}
Loading

0 comments on commit 71128d2

Please sign in to comment.