简介

这个项目主要有两个功能,一个加载网页/文章,另一个用来显示评论。并应用了MVP模式,Dagger2RxJavaButterKnife等开源框架。效果图如下:

结构

首先来看一下布局文件:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.dean.articlecomment.article.ArticleActivity"> <com.dean.articlecomment.ui.XAppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay" /> </com.dean.articlecomment.ui.XAppBarLayout> <include layout="@layout/content_scrolling" /> <include layout="@layout/article_bottom_view" /> </android.support.design.widget.CoordinatorLayout>

toolbar

在显示网页文章时是仿知乎的操作,向下滑动时隐藏toolbar和屏幕下方发表评论的视图,向上滚动时再显示。

toolbar的显示隐藏是通过设置其scrollFlags属性实现的。

enterAlways:向上滑时toolbar隐藏,向下滑动即展示。

enterAlwaysCollapsed:向上滑时toolbar隐藏,向下滑动直到NestedScrollView的底部时toolbar才展示。

exitUntilCollapsed:当你定义了一个minHeight,这个view将在滚动到达这个最小高度的时候消失。

snap:突然折断的意思,效果同enterAlwaysCollapsed,区别为滚动时手指离开屏幕时

toolbar不会显示一半的状态,显示的部分大于一半时即全漏出来,小于一半时即隐藏掉。

article_bottom_view

article_bottom_view是屏幕下方的评论条,它的隐藏显示与toolbar同步,使用方式是通过AppBarLayout.OnOffsetChangedListener的状态监听与动画实现的。

  @Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (verticalOffset >= 0) {
if (xAppBarListener != null) {
xAppBarListener.onFingerDown();
}
} else {
if (xAppBarListener != null) {
xAppBarListener.onFingerUp();
}
}
}

content_scrolling

content_scrolling布局如下:

<com.dean.articlecomment.ui.XNestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_scrolling"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <!--详细-->
<FrameLayout
android:id="@+id/article_content_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout> <!--评论-->
<FrameLayout
android:id="@+id/comment_content_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
</LinearLayout>
</com.dean.articlecomment.ui.XNestedScrollView>

NestedScrollView中嵌套两个视图article_content_view,comment_content_view。分别是用于显示文章Fragment视图和评论fragment视图。

文章Fragment

文章Fragment中使用Webview来显示网页/文章。

Webview使用了腾讯的X5WebView,并在外层封装一个加载用的进度条。

评论fragment

文章Fragment中使用了RecycleView(根据XRecyclerView改造)来显示添加评论,并且可以进行滑动加载更多。

值得注意的是NestedScrollview中嵌套RecycleView的问题,解决方法是:

  • 使用Android Support Library 23.2.0以上,设置layoutManager.setAutoMeasureEnabled(true);

  • 将recyclerView的高度设置为wrap_content

  • 设置recyclerView.setNestedScrollingEnabled(false)避免和NestedScrolling的滑动冲突。

    由于禁用了recyclerView的滚动,所以在实现底部加载更多的时候需要监听外层的NestedScrollingView

MVP

本Demo使用了MVP模式(关于MVP的文章网上很多,我这里就不过多介绍),主要借鉴了下面3个开源项目。并作了一些改动。

大多数MVP模式里都是View持有Presenter的引用。一个fragment对应一个页面,一个页面对应一个Presenter,因此如果一个功能中页面较多时会导致逻辑复杂以及代码文件的增加。

我这里的处理是反过来使Presenter持有View的引用,即一个Activity持有一个Presenter,每个Fragment是一个View,用一个Presenter持有所有的View引用。

所有的逻辑和业务代码都放在Presenter中处理,Activity和Fragment只负责页面的显示。这样的好处是结构简单,逻辑比较清晰,方便在多个view中交互操作。缺点就是会导致Presenter中代码量过大。

代码如下:

public class ArticlePresenter extends RxPresenter implements ArticleContract.Presenter {

    protected final ArticleContract.ArticleView articleView;

    protected final ArticleContract.CommentView commentView;

    protected final ArticleContract.View bottomView;

    @Inject
public ArticlePresenter(ArticleContract.ArticleView articleView, ArticleContract.CommentView commentView, ArticleContract.View bottomView) {
this.articleView = articleView;
this.commentView = commentView;
this.bottomView = bottomView;
} @Inject
void setupListeners() {
// view中注入presenter
articleView.setPresenter(this);
commentView.setPresenter(this);
bottomView.setPresenter(this);
}
}

Contract代码如下:

public interface ArticleContract {

    interface Presenter extends BasePresenter {
void addComment();
void showBottomView();
void hideBottomView();
void onLoadingArticle();
void onLoadingComment();
void onLoadingMoreComment();
void onLoadingArticleSuccess();
void onLoadingArticleFailed();
} interface CommentView extends BaseView<Presenter> {
void showComments(ArrayList<ArticleComment> comments);
void showLoadMoreComments(ArrayList<ArticleComment> comments);
void addComment(ArticleComment comment);
void onScrollToPageEnd();
} interface ArticleView extends BaseView<Presenter> {
void showArticle(String url);
} interface View extends BaseView<Presenter> {
void showBottomView();
void hideBottomView();
void goToComment();
void goToArticle();
}
}

Rxjava/RxAndroid

ReactiveX/RxJava

ReactiveX/RxAndroid

Rxjava也是最近才知道。。。使用后发现是真的很牛逼。。。

于是也简单的在这个Demo中应用了一下,加载更多评论的代码如下:

  • 首先在IO线程中创建数据,这里延迟2秒模拟网络请求。
  • 然后在UI线程中显示,由于懒没写Error的代码。。。
 @Override
public void onLoadingMoreComment() { Subscription rxSubscription = Observable
.create(new Observable.OnSubscribe<ArrayList<ArticleComment>>() {
@Override
public void call(Subscriber<? super ArrayList<ArticleComment>> subscriber) {
ArrayList<ArticleComment> comments = new ArrayList<ArticleComment>();
for (int i = 0; i < 5; i++) {
ArticleComment newComment = new ArticleComment();
newComment.userName = "游客" + i;
newComment.commentContent = "他很懒什么都没说。";
comments.add(newComment);
}
subscriber.onNext(comments);
}
})
.delay(2, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ArrayList<ArticleComment>>() {
@Override
public void onCompleted() { } @Override
public void onError(Throwable e) { } @Override
public void onNext(ArrayList<ArticleComment> articleComments) {
if (commentView.isActive())
commentView.showLoadMoreComments(articleComments);
}
});
addSubscribe(rxSubscription);
}

Rxjava简单使用很容易,但要达到能适应各种场景就不轻松了,我也在摸索中。下面列出我找到相关文章:

给 Android 开发者的 RxJava 详解

RxJava操作符大全

ReactiveX/RxJava文档中文版

dagger

google/dagger

实话实说,这个依赖注入框架真心不太明白,感觉学习成本和使用成本都有点高,demo里也仅仅做了最简单的应用。

下面列出我觉得不错的文章:

依赖注入神器:Dagger2详解系列

butterknife

JakeWharton/butterknife

视图注入框架,很好用!网上例子很多,使用起来也方便就不介绍了。

最后

还有一些小细节,比如添加/删除评论,双击toolbar回到文章头,点击评论按钮跳转到评论等等。写这个demo的主要目的是为了练习使用MVP以及各种开源框架,如果以后有时间会陆续加入下面列表中的开源框架。

  • Realm
  • Retrofit
  • RxCache
  • RxBinding
  • RxBus

demo地址

https://github.com/a396901990/Article-Comment

Android实现文章+评论(MVP,RxJava,Dagger2,ButterKnife)的更多相关文章

  1. android完整资讯App、Kotlin新闻应用MVP + RxJava + Retrofit + Dagger2、优雅区间选择器等源码

    Android精选源码 Android完整资讯客户端源码 android展示注册进度效果源码 Android Wifi热点数据传输Socket 通信示例源码 Android Dota的辅助信息app源 ...

  2. android流式布局、待办事项应用、贝塞尔曲线、MVP+Rxjava+Retrofit、艺术图片应用等源码

    Android精选源码 android模仿淘宝首页效果源码 一款艺术图片应用,采用T-MVVM打造 Android MVP + RxJava + Retrofit项目 android流式布局实现热门标 ...

  3. MVP+RXJAVA+RecyclerView实现sd卡根目录下的所有文件中的照片加载并显示

    初学Rxjava,目前只能遍历加载指定目录下的所有文件夹中的照片,文件夹中如果还嵌套有文件夹目前还没找到实现方法. 先看mvp目录结构: 很抱歉,没有model. 接下来是view层的接口代码和pre ...

  4. 转:Android开发中的MVP架构(最后链接资源不错)

    Android开发中的MVP架构 最近越来越多的人开始谈论架构.我周围的同事和工程师也是如此.尽管我还不是特别深入理解MVP和DDD,但是我们的新项目还是决定通过MVP来构建. 这篇文章是我通过研究和 ...

  5. Android开发之手把手教你写ButterKnife框架(三)

    欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/52672188 本文出自:[余志强的博客] 一.概述 上一篇博客讲了, ...

  6. Android 架构艺术之MVP

    MVP是Google官方发布的Android开发相关的架构知识.本文要讲解的是一种最基本的MVP的实现方式,它使用手动的依赖注入来提供具有本地和远程数据源的存储库.异步任务处理回调. 基本的MVP的项 ...

  7. Android开发之手把手教你写ButterKnife框架(二)

    欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/52664112 本文出自:[余志强的博客] 上一篇博客Android开 ...

  8. $Django 站点:样式--文章--分类文章--文章详情--文章评论点赞--文章评论点赞统计(数据库优化)

    <h3>个人站点下的</h3> 知识点 url (r'(?P<username>\w+)/p/(?P<id>\d+)', xiangxi,name='x ...

  9. BBS - 文章评论

    一.文章评论 <div class="comment_region"> <div class="row"> <div class= ...

随机推荐

  1. NOSQL场景梳理

    Redis 场景:缓存,Session,消息发布订阅,产品属性分析,订单购买等强事务,计数等   Memcached 场景:读密集,写一般的缓存,Session   MongoDB 场景:数据显示,查 ...

  2. (转)我看PhD by 王珢

    我看PhD by 王垠 前段时间看了一下这些关于 PhD 的负面信息: 一个专门反对读 PhD 的 BLOG 叫“100 Reasons NOT to Go to Graduate School”(下 ...

  3. android studio增量更新

    一.概述 1.1 概念 增量更新即是通过比较 本机安装版本 和 想要安装版本 间的差异,产生一个差异安装包,不需要从官网下载并安装全量安装包,更不需要将本机已安装的版本下载,而仅仅只是安装此差异安装包 ...

  4. CI,从数据库读取数据

    1.准备数据库,(用户,密码,数据库服务的地址) 2.CI链接数据库,配置database.php(配置文件)       //application/config/database.php 3.准备 ...

  5. BSBuDeJie_02

    一 左边的类别数据 1 模型 和 字典中的数据对应 /* id */ @property (nonatomic, assign) NSInteger *id; /* 总数 */ @property ( ...

  6. mysql优化limit分页

  7. C# String 前面不足位数补零的方法

    int i=10;方法1:Console.WriteLine(i.ToString("D5"));方法2:Console.WriteLine(i.ToString().PadLef ...

  8. netfiler源代码分析之框架介绍

    netfiler框架是在内核协议栈实现的基础上完成的,在报文从网口接收,路由等方法实现基础上使用NF_HOOK调用相应的钩子来进入netfiler框架的处理,如 ip_rcv之后会调用NF_HOOK( ...

  9. GDB调试32位汇编堆栈分析

    GDB调试32位汇编堆栈分析 测试源代码 #include <stdio.h> int g(int x){ return x+5; } int f(int x){ return g(x)+ ...

  10. css 文字溢出隐藏 带省略号

    .demo{ width:100px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } 注意宽度要设置