package com.shawn.myapplication;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.badge.BadgeDrawable;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabLayout;
import com.shawn.myapplication.R;
import com.shawn.myapplication.databinding.ActivityMainBinding;
import com.shawn.myapplication.ui.main.CustomPagerAdapter;
import com.shawn.myapplication.ui.main.SectionsPagerAdapter;
import java.util.concurrent.ExecutionException;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
/**
* 自定义tabLayout布局
*
* @author steve
*/
public class CustomTabLayoutActivity extends AppCompatActivity {
private TabLayout tabs;
private String[] titles = {"one", "two", "three", "four", "five", "six", "seven", "我是中国人", "Top Search"};
private String imageURL = "https://www.wanandroid.com/blogimgs/62c1bd68-b5f3-4a3c-a649-7ca8c7dfabe6.png";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_tablayout);
CustomPagerAdapter sectionsPagerAdapter = new CustomPagerAdapter(this, getSupportFragmentManager(), titles);
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
setupTabIcons();
// tabs.setTabGravity(TabLayout.GRAVITY_FILL);
// 设置图标和文字水平显示 自定义view不能设置,不会显示图标
// tabs.setInlineLabel(true);
// 设置tabLayout的模式:1、固定长度平均分配 2、滚动模式
tabs.setTabMode(TabLayout.MODE_AUTO);
BadgeDrawable badgeDrawable = tabs.getTabAt(0).getOrCreateBadge();
badgeDrawable.setVisible(true);
badgeDrawable.setBadgeGravity(BadgeDrawable.TOP_END);
badgeDrawable.setNumber(1);
badgeDrawable.setBadgeTextColor(getResources().getColor(R.color.red));
badgeDrawable.setBackgroundColor(getResources().getColor(R.color.black));
tabs.addOnTabSelectedListener(onTabSelectedListener);
// 初始化第一个下划线和字体颜色
int selectPosition = tabs.getSelectedTabPosition();
TabLayout.Tab defaultSelectTab = tabs.getTabAt(selectPosition);
if (defaultSelectTab != null) {
Log.e("selectPosition", "当前被选中的 = " + selectPosition);
changeTabLayoutTextColor(defaultSelectTab, true);
}
// showCustomTab();
// TabLayout.Tab tabAt = tabs.getTabAt(0);
// rxjavaObservable(tabAt, R.drawable.icon_gongjiao);
// Drawable drawable = loadCircleCropImage(R.drawable.icon_gongjiao);
// tabAt.setIcon(R.drawable.icon_gongjiao);
// TabLayout.TabView view = tabAt.view;
// TabLayout.Tab tabAt1 = tabs.getTabAt(1);
// rxjavaObservable(tabAt1,R.drawable.icon_xinyongka);
// Drawable drawable1 = loadCircleCropImage(R.drawable.icon_xinyongka);
// tabAt1.setIcon(R.drawable.icon_xinyongka);
// TabLayout.TabView view1 = tabAt1.view;
// TVplay.subscribe(viewer);
// rxjavaObservable();
}
TabLayout.OnTabSelectedListener onTabSelectedListener = new TabLayout.OnTabSelectedListener() {
/**
* 当选项卡进入选定状态时调用
*
* @param tab
*/
@Override
public void onTabSelected(TabLayout.Tab tab) {
TextView textView = tab.getCustomView().findViewById(R.id.txt_title);
String textString = textView.getText().toString();
changeTabLayoutTextColor(tab, true);
Log.e("tabLayout", "onTabSelected = " + textString);
}
/**
* 当选项卡退出所选状态时调用
*
* @param tab
*/
@Override
public void onTabUnselected(TabLayout.Tab tab) {
TextView textView = tab.getCustomView().findViewById(R.id.txt_title);
String textString = textView.getText().toString();
changeTabLayoutTextColor(tab, false);
Log.e("tabLayout", "onTabUnselected = " + textString);
}
/**
* 再次点击已选中的标签
* @param tab
*/
@Override
public void onTabReselected(TabLayout.Tab tab) {
TextView textView = tab.getCustomView().findViewById(R.id.txt_title);
String textString = textView.getText().toString();
Log.e("tabLayout", "onTabReselected = " + textString);
}
};
private void changeTabLayoutTextColor(TabLayout.Tab tab, boolean isSelected) {
boolean tabSelect = tab.isSelected();
Log.e("tabLayout", "当前TabLayout.Tab = " + tabSelect);
TextView textView = tab.getCustomView().findViewById(R.id.txt_title);
boolean selected = tabs.isSelected();
Log.e("tabLayout", "当前TabLayout = " + selected);
if (isSelected) {
textView.setTextColor(getResources().getColor(R.color.purple_200));
tabs.setSelectedTabIndicatorColor(getResources().getColor(R.color.purple_200));
} else {
textView.setTextColor(getResources().getColor(R.color.black66));
}
}
private void showCustomTab() {
for (int i = 0; i < titles.length; i++) {
View itemView = LayoutInflater.from(this).inflate(R.layout.item_tablayout, null);
((TextView) itemView.findViewById(R.id.tv)).setText(titles[i]);
tabs.getTabAt(i).setCustomView(itemView);
}
}
private void setupTabIcons() {
for (int i = 0; i < titles.length; i++) {
tabs.getTabAt(i).setCustomView(getTabView(i));
}
}
public View getTabView(int position) {
View view = LayoutInflater.from(this).inflate(R.layout.item_tablayout_2, null);
TextView txt_title = (TextView) view.findViewById(R.id.txt_title);
txt_title.setText(titles[position]);
ImageView img_title = (ImageView) view.findViewById(R.id.img_title);
rxjavaObservable(img_title, R.drawable.icon_placeholder);
return view;
}
/**
* 加载圆形图标
*
* @param resourceId 图片资源
* @return
*/
private Drawable loadCircleCropImage(Integer resourceId) {
Drawable icon;
try {
icon = Glide.with(this).load(resourceId)
.apply(RequestOptions.bitmapTransform(new CircleCrop())).submit().get();
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
icon = null;
}
return icon;
}
/**
* 加载圆形图标
*
* @param url 图片网络资源地址
* @return
*/
private Drawable loadCircleCropImage(String url) {
Drawable icon;
try {
icon = Glide.with(this).load(url)
.apply(RequestOptions.bitmapTransform(new CircleCrop())).submit().get();
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
icon = null;
}
return icon;
}
/**
* 加载图片在IO线程,结果切换为主线程显示
*
* @param resourceId
*/
private void rxjavaObservable(ImageView img_title, Integer resourceId) {
//更简洁的方法 基于事件流的链式调用
Observable.create(new ObservableOnSubscribe<Drawable>() {
@Override
public void subscribe(@NonNull ObservableEmitter<Drawable> emitter) {
emitter.onNext(loadCircleCropImage(imageURL));
emitter.onComplete();
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Drawable>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
Log.e("RxJava_TAG", "onSubscribe");
}
@Override
public void onNext(@NonNull Drawable drawable) {
Log.e("RxJava_TAG", "onNext");
img_title.setImageDrawable(drawable);
}
@Override
public void onError(@NonNull Throwable e) {
Log.e("RxJava_TAG", "onError");
}
@Override
public void onComplete() {
Log.e("RxJava_TAG", "onComplete");
}
});
}
//创建被观察者Observable(电视剧)
//使用create操作符从头开始创建一个Observable
Observable<String> TVplay = Observable.create(new ObservableOnSubscribe<String>() {//ObservableOnSubscribe<T>其实就像一个队列一样
@Override
public void subscribe(@NonNull ObservableEmitter<String> emitter) {//ObservableEmitter可以理解为事件发射器
//重写subscribe()定义需要发送的事件
//ObservableEmitter有三种发射方法,onNext()、onComplete()、onError()
//onNext()发送事件,可以无限调用,所有的发射信息观察者都可以收得到
emitter.onNext("电视剧第一集");
emitter.onNext("电视剧第二集");
emitter.onNext("电视剧第三集");
//onComplete()、onError()是互斥的,观察者只会收到一个
//onComplete()完成事件发送,可以重复调用,但是观察者只会接收一次
emitter.onComplete();
//onError()异常发送,不可以重复调用
//emitter.onError(new IllegalArgumentException("TVplay Exception!"));
}
});
//创建观察者Observer(观影者)
Observer<String> viewer = new Observer<String>() {
//定义Disposable类变量
//private Disposable mDisposable;
//事件队列开始
@Override
public void onSubscribe(@NonNull Disposable d) {//Disposable可以当作订阅关系
//当订阅时被调用
Log.e("RxJava_TAG", "onSubscribe");
//取消/解除订阅
//d.dispose();
//是否已经解除订阅
//d.isDisposed();
//可以这样操作
//mDisposable = d;
}
//普通事件
@Override
public void onNext(@NonNull String s) {
//发送数据时被多次调用
Log.e("RxJava_TAG", "onNext" + s);
//取消/解除订阅实例
//if ("电视剧第二集".equals(s)) {
// //设置在接收到"电视剧第三集"事件后切断观察者和被观察者的连接
// mDisposable.dispose();
// Log.d("RxJava_TAG", "是否已经切断了连接:" + mDisposable.isDisposed());
//}
}
//事件队列异常事件
@Override
public void onError(@NonNull Throwable e) {
//在事件处理过程中出现异常时,onError()方法会被触发
Log.e("RxJava_TAG", "onError");
}
//事件队列完结事件
@Override
public void onComplete() {
//RxJava把所有事件都当作队列处理
//当被观察者不再发送onNext()普通事件的时候
Log.e("RxJava_TAG", "onComplete");
}
};
}
06-13
5971