项目的继承结构
AppCompatActivity
BaseActivity(Activity的基类, 主要管理RxJava的订阅和取消)
ToolbarActivity(封装了Toolbar的Activity)
PictureActivity(展示图片)
WebActivity (展示网页)
GankActivity(干货页面)
SwipeRefreshBaseActivity
MainActivity(程序主页面)
项目中使用的库
// 数据库
lite-orm
compile project(":libraries:headsupcompat")
compile project(":libraries:smooth-app-bar-layout")
compile 'com.daimajia.numberprogressbar:library:1.2@aar'
// 通过各种触摸手势实现支持缩放功能的ImageView
compile 'com.github.chrisbanes.photoview:library:1.2.3'
// 事件总线
compile 'com.squareup:otto:1.3.8'
一些值得注意的代码
获取服务数据的代码,使用了RxJava + Retrofit获取
private void loadData(boolean clean) {
mLastVideoIndex = 0;
Subscription s = Observable.zip(sGankIO.getMeizhiData(mPage),
sGankIO.getVideoData(mPage),
this::createMeizhiDataWithVideoDesc)
.map(meizhiData -> meizhiData.results)
.flatMap(Observable::from)
// 排序
.toSortedList((meizhi1, meizhi2) -> meizhi2.publishedAt.compareTo(meizhi1.publishedAt))
//
.doOnNext(this::saveMeizhiData)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(meizhis -> {
if (clean)
mMeizhiList.clear();
mMeizhiList.addAll(meizhis);
mMeizhiListAdapter.notifyDataSetChanged();
setRefresh(false);
}, throwable -> loadError(throwable));
addSubscription(s);
}
判断瀑布流页面是否滑动到了底部
RecyclerView.OnScrollListener getOnBottomListener(StaggeredGridLayoutManager layoutManager) {
return new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView rv, int dx, int dy) {
boolean isBottom = layoutManager.findLastCompletelyVisibleItemPositions(new int[2])
// 这里的[1]表示返回值的[1]
[1] >= mMeizhiListAdapter.getItemCount() - PRELOAD_SIZE;
if (!mSwipeRefreshLayout.isRefreshing() && isBottom) {
if (!mIsFirstTimeTouchBottom) {
mSwipeRefreshLayout.setRefreshing(true);
mPage += 1;
loadData();
} else {
mIsFirstTimeTouchBottom = false;
}
}
}
};
}
- 使用
SpannableStringBuilder
来设置字符串的内容和样式,一步搞定字符串的内容和样式
SpannableStringBuilder builder = new SpannableStringBuilder(gank.desc).append(StringStyles.format(holder.gank
.getContext(), " (via. " + gank.who + ")", R.style.ViaTextAppearance));
最终实现的效果
- 使用
TextSwitcher
来实现字符串的滚动效果
TextSwitcher放到Toolbar中,实现Toolbar中标题的滚动效果,布局如下:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<TextSwitcher
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v7.widget.Toolbar>
实现的效果是这样的:
TextSwitcher具体的用法在WebActivity中,代码如下:
mTextSwitcher.setFactory(() -> {
TextView textView = new TextView(this);
textView.setTextAppearance(this, R.style.WebTitle);
textView.setSingleLine(true);
textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
textView.postDelayed(() -> textView.setSelected(true), 1738);
return textView;
});
mTextSwitcher.setInAnimation(this, android.R.anim.fade_in);
mTextSwitcher.setOutAnimation(this, android.R.anim.fade_out);
if (mTitle != null)
setTitle(mTitle);
如何查看当前正在显示的Activity
linux:
adb shell dumpsys activity | grep "mFocusedActivity"
windows:
adb shell dumpsys activity | findstr "mFocusedActivity"