1.效果预览以及布局分析

1.1.实际效果预览

  

  左侧话题列表的布局是通过TopicProvider来实现的,所以当初分析话题列表就没有看到布局。

  这里的话题内容不是一个ListView,故要自己布局。

1.2.整体布局对应关系

  

  简单易懂的布局,首先用最外层的RelativeLayout,包裹了一个Toolbar和一个NestedScrollView

  NestedScrollView包裹了一个LinearLayout

  LinearLayout包裹了很多视图

1.3.注意点,这里图标是自定义圆角边框==>CircleImageView

  

  下面看看如何自定义圆角视图:

  1. /*
  2. * Copyright 2017 GcsSloop
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. * Last modified 2017-03-08 01:01:18
  17. *
  18. * GitHub: https://github.com/GcsSloop
  19. * Website: http://www.gcssloop.com
  20. * Weibo: http://weibo.com/GcsSloop
  21. */
  22.  
  23. package com.gcssloop.diycode.widget;
  24.  
  25. import android.content.Context;
  26. import android.content.res.TypedArray;
  27. import android.graphics.Bitmap;
  28. import android.graphics.BitmapShader;
  29. import android.graphics.Canvas;
  30. import android.graphics.Color;
  31. import android.graphics.Matrix;
  32. import android.graphics.Paint;
  33. import android.graphics.RectF;
  34. import android.graphics.Shader;
  35. import android.graphics.drawable.BitmapDrawable;
  36. import android.graphics.drawable.ColorDrawable;
  37. import android.graphics.drawable.Drawable;
  38. import android.util.AttributeSet;
  39. import android.widget.ImageView;
  40.  
  41. import com.gcssloop.diycode.R;
  42.  
  43. /**
  44. * 圆形图片
  45. */
  46. public class CircleImageView extends ImageView {
  47.  
  48. private static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP;
  49.  
  50. private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;
  51. private static final int COLORDRAWABLE_DIMENSION = 1;
  52.  
  53. private static final int DEFAULT_BORDER_WIDTH = 0;
  54. private static final int DEFAULT_BORDER_COLOR = Color.BLACK;
  55.  
  56. private final RectF mDrawableRect = new RectF();
  57. private final RectF mBorderRect = new RectF();
  58.  
  59. private final Matrix mShaderMatrix = new Matrix();
  60. private final Paint mBitmapPaint = new Paint();
  61. private final Paint mBorderPaint = new Paint();
  62.  
  63. private int mBorderColor = DEFAULT_BORDER_COLOR;
  64. private int mBorderWidth = DEFAULT_BORDER_WIDTH;
  65.  
  66. private Bitmap mBitmap;
  67. private BitmapShader mBitmapShader;
  68. private int mBitmapWidth;
  69. private int mBitmapHeight;
  70.  
  71. private float mDrawableRadius;
  72. private float mBorderRadius;
  73.  
  74. private boolean mReady;
  75. private boolean mSetupPending;
  76.  
  77. public CircleImageView(Context context) {
  78. super(context);
  79. }
  80.  
  81. public CircleImageView(Context context, AttributeSet attrs) {
  82. this(context, attrs, 0);
  83. }
  84.  
  85. public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
  86. super(context, attrs, defStyle);
  87. super.setScaleType(SCALE_TYPE);
  88.  
  89. TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView, defStyle, 0);
  90.  
  91. mBorderWidth = a.getDimensionPixelSize(R.styleable.CircleImageView_border_width, DEFAULT_BORDER_WIDTH);
  92. mBorderColor = a.getColor(R.styleable.CircleImageView_border_color, DEFAULT_BORDER_COLOR);
  93.  
  94. a.recycle();
  95.  
  96. mReady = true;
  97.  
  98. if (mSetupPending) {
  99. setup();
  100. mSetupPending = false;
  101. }
  102. }
  103.  
  104. @Override
  105. public ScaleType getScaleType() {
  106. return SCALE_TYPE;
  107. }
  108.  
  109. @Override
  110. public void setScaleType(ScaleType scaleType) {
  111. if (scaleType != SCALE_TYPE) {
  112. throw new IllegalArgumentException(String.format("ScaleType %s not supported.", scaleType));
  113. }
  114. }
  115.  
  116. @Override
  117. protected void onDraw(Canvas canvas) {
  118. if (getDrawable() == null) {
  119. return;
  120. }
  121.  
  122. canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius, mBitmapPaint);
  123. canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBorderRadius, mBorderPaint);
  124. }
  125.  
  126. @Override
  127. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  128. super.onSizeChanged(w, h, oldw, oldh);
  129. setup();
  130. }
  131.  
  132. public int getBorderColor() {
  133. return mBorderColor;
  134. }
  135.  
  136. public void setBorderColor(int borderColor) {
  137. if (borderColor == mBorderColor) {
  138. return;
  139. }
  140.  
  141. mBorderColor = borderColor;
  142. mBorderPaint.setColor(mBorderColor);
  143. invalidate();
  144. }
  145.  
  146. public int getBorderWidth() {
  147. return mBorderWidth;
  148. }
  149.  
  150. public void setBorderWidth(int borderWidth) {
  151. if (borderWidth == mBorderWidth) {
  152. return;
  153. }
  154.  
  155. mBorderWidth = borderWidth;
  156. setup();
  157. }
  158.  
  159. @Override
  160. public void setImageBitmap(Bitmap bm) {
  161. super.setImageBitmap(bm);
  162. mBitmap = bm;
  163. setup();
  164. }
  165.  
  166. @Override
  167. public void setImageDrawable(Drawable drawable) {
  168. super.setImageDrawable(drawable);
  169. mBitmap = getBitmapFromDrawable(drawable);
  170. setup();
  171. }
  172.  
  173. @Override
  174. public void setImageResource(int resId) {
  175. super.setImageResource(resId);
  176. mBitmap = getBitmapFromDrawable(getDrawable());
  177. setup();
  178. }
  179.  
  180. private Bitmap getBitmapFromDrawable(Drawable drawable) {
  181. if (drawable == null) {
  182. return null;
  183. }
  184.  
  185. if (drawable instanceof BitmapDrawable) {
  186. return ((BitmapDrawable) drawable).getBitmap();
  187. }
  188.  
  189. try {
  190. Bitmap bitmap;
  191.  
  192. if (drawable instanceof ColorDrawable) {
  193. bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);
  194. } else {
  195. bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), BITMAP_CONFIG);
  196. }
  197.  
  198. Canvas canvas = new Canvas(bitmap);
  199. drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
  200. drawable.draw(canvas);
  201. return bitmap;
  202. } catch (OutOfMemoryError e) {
  203. return null;
  204. }
  205. }
  206.  
  207. private void setup() {
  208. if (!mReady) {
  209. mSetupPending = true;
  210. return;
  211. }
  212.  
  213. if (mBitmap == null) {
  214. return;
  215. }
  216.  
  217. mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
  218.  
  219. mBitmapPaint.setAntiAlias(true);
  220. mBitmapPaint.setShader(mBitmapShader);
  221.  
  222. mBorderPaint.setStyle(Paint.Style.STROKE);
  223. mBorderPaint.setAntiAlias(true);
  224. mBorderPaint.setColor(mBorderColor);
  225. mBorderPaint.setStrokeWidth(mBorderWidth);
  226.  
  227. mBitmapHeight = mBitmap.getHeight();
  228. mBitmapWidth = mBitmap.getWidth();
  229.  
  230. mBorderRect.set(0, 0, getWidth(), getHeight());
  231. mBorderRadius = Math.min((mBorderRect.height() - mBorderWidth) / 2, (mBorderRect.width() - mBorderWidth) / 2);
  232.  
  233. mDrawableRect.set(mBorderWidth, mBorderWidth, mBorderRect.width() - mBorderWidth, mBorderRect.height() - mBorderWidth);
  234. mDrawableRadius = Math.min(mDrawableRect.height() / 2, mDrawableRect.width() / 2);
  235.  
  236. updateShaderMatrix();
  237. invalidate();
  238. }
  239.  
  240. private void updateShaderMatrix() {
  241. float scale;
  242. float dx = 0;
  243. float dy = 0;
  244.  
  245. mShaderMatrix.set(null);
  246.  
  247. if (mBitmapWidth * mDrawableRect.height() > mDrawableRect.width() * mBitmapHeight) {
  248. scale = mDrawableRect.height() / mBitmapHeight;
  249. dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f;
  250. } else {
  251. scale = mDrawableRect.width() / mBitmapWidth;
  252. dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f;
  253. }
  254.  
  255. mShaderMatrix.setScale(scale, scale);
  256. mShaderMatrix.postTranslate((int) (dx + 0.5f) + mBorderWidth, (int) (dy + 0.5f) + mBorderWidth);
  257.  
  258. mBitmapShader.setLocalMatrix(mShaderMatrix);
  259. }
  260.  
  261. }

  这里复写了3个构造函数,还有实现scaleType()+onDraw()+onSizeChanged+setImageBitmap。

  需要自定义一个样式文件

  

2.分析TopicContentActivity部分函数

2.1.首先预览一下成员变量

  

  首先是几个临时字符串。

  然后声明一个话题的唯一id,这个可以确定是哪个话题。

  然后声明一个话题的类,这个类也是可以确定是哪个话题的。

  然后是一个数据缓存类,用来处理记录回复以及话题详情的。

  然后定义了一个话题回复的适配器,这个适配器用来显示整个回复列表的。

  然后这里自定义了一个webView叫做MarkdownView,可能是为了方便点击图片跳转的吧。

  然后这里自定义了一个webViewClient叫做GcsMarkdownViewClient,可能也是为了注入js函数监听的。 

2.2.然后详细看一下TopicReplyAdapter这个适配器。

  2.2.1.首先是源代码。 

  1. /*
  2. * Copyright 2017 GcsSloop
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. * Last modified 2017-03-26 05:35:12
  17. *
  18. * GitHub: https://github.com/GcsSloop
  19. * Website: http://www.gcssloop.com
  20. * Weibo: http://weibo.com/GcsSloop
  21. */
  22.  
  23. package com.gcssloop.diycode.adapter;
  24.  
  25. import android.content.Context;
  26. import android.content.Intent;
  27. import android.support.annotation.NonNull;
  28. import android.text.Html;
  29. import android.view.View;
  30. import android.widget.ImageView;
  31. import android.widget.TextView;
  32.  
  33. import com.gcssloop.diycode.R;
  34. import com.gcssloop.diycode.activity.UserActivity;
  35. import com.gcssloop.diycode.base.glide.GlideImageGetter;
  36. import com.gcssloop.diycode.utils.HtmlUtil;
  37. import com.gcssloop.diycode.utils.ImageUtils;
  38. import com.gcssloop.diycode_sdk.api.topic.bean.TopicReply;
  39. import com.gcssloop.diycode_sdk.api.user.bean.User;
  40. import com.gcssloop.diycode.utils.TimeUtil;
  41. import com.gcssloop.recyclerview.adapter.base.RecyclerViewHolder;
  42. import com.gcssloop.recyclerview.adapter.singletype.SingleTypeAdapter;
  43.  
  44. public class TopicReplyAdapter extends SingleTypeAdapter<TopicReply> {
  45. private Context mContext;
  46.  
  47. public TopicReplyAdapter(@NonNull Context context) {
  48. super(context, R.layout.item_topic_reply);
  49. mContext = context;
  50. }
  51.  
  52. /**
  53. * 在此处处理数据
  54. *
  55. * @param position 位置
  56. * @param holder view holder
  57. * @param bean 数据
  58. */
  59. @Override public void convert(int position, RecyclerViewHolder holder, TopicReply bean) {
  60. final User user = bean.getUser();
  61. holder.setText(R.id.username, user.getLogin());
  62. holder.setText(R.id.time, TimeUtil.computePastTime(bean.getUpdated_at()));
  63.  
  64. ImageView avatar = holder.get(R.id.avatar);
  65. ImageUtils.loadImage(mContext, user.getAvatar_url(), avatar);
  66. TextView content = holder.get(R.id.content);
  67. // TODO 评论区代码问题
  68. content.setText(Html.fromHtml(HtmlUtil.removeP(bean.getBody_html()), new GlideImageGetter(mContext, content), null));
  69.  
  70. holder.setOnClickListener(new View.OnClickListener() {
  71. @Override
  72. public void onClick(View v) {
  73. Intent intent = new Intent(mContext, UserActivity.class);
  74. intent.putExtra(UserActivity.USER, user);
  75. mContext.startActivity(intent);
  76. }
  77. }, R.id.avatar, R.id.username);
  78. }
  79. }

  2.2.2.然后分析一下继承类和构造函数。

    

    首先继承了一个SingleTypeAdapter<TopicReply>

    这个类又是干什么的呢?

    2.2.2.1.首先看一下源代码。 

  1. /*
  2. * Copyright 2017 GcsSloop
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. * Last modified 2017-04-08 16:14:10
  17. *
  18. * GitHub: https://github.com/GcsSloop
  19. * WeiBo: http://weibo.com/GcsSloop
  20. * WebSite: http://www.gcssloop.com
  21. */
  22.  
  23. package com.gcssloop.recyclerview.adapter.singletype;
  24.  
  25. import android.content.Context;
  26. import android.support.annotation.LayoutRes;
  27. import android.support.annotation.NonNull;
  28. import android.support.v7.widget.RecyclerView;
  29. import android.view.LayoutInflater;
  30. import android.view.View;
  31. import android.view.ViewGroup;
  32.  
  33. import com.gcssloop.recyclerview.adapter.base.RecyclerViewHolder;
  34.  
  35. import java.util.ArrayList;
  36. import java.util.List;
  37.  
  38. public abstract class SingleTypeAdapter<T> extends RecyclerView.Adapter<RecyclerViewHolder> {
  39.  
  40. protected Context mContext;
  41. private LayoutInflater mInflater;
  42. private List<T> mDatas = new ArrayList<>();
  43. private int mLayoutId;
  44.  
  45. public SingleTypeAdapter(@NonNull Context context, @LayoutRes int layoutId) {
  46. mInflater = LayoutInflater.from(context);
  47. mContext = context;
  48. mLayoutId = layoutId;
  49. }
  50.  
  51. @Override
  52. public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  53. View rootView = null;
  54. try {
  55. rootView = mInflater.inflate(mLayoutId, parent, false);
  56. } catch (Exception e) {
  57. e.printStackTrace();
  58. }
  59. return new RecyclerViewHolder(rootView);
  60. }
  61.  
  62. @Override
  63. public void onBindViewHolder(RecyclerViewHolder holder, int position) {
  64. convert(position, holder, mDatas.get(position));
  65. }
  66.  
  67. /**
  68. * 在此处处理数据
  69. *
  70. * @param position 位置
  71. * @param holder view holder
  72. * @param bean 数据
  73. */
  74. public abstract void convert(int position, RecyclerViewHolder holder, T bean);
  75.  
  76. @Override
  77. public int getItemCount() {
  78. return mDatas.size();
  79. }
  80.  
  81. public void addDatas(List<T> datas) {
  82. this.mDatas.addAll(datas);
  83. notifyDataSetChanged();
  84. }
  85.  
  86. public List<T> getDatas() {
  87. return mDatas;
  88. }
  89.  
  90. public void clearDatas() {
  91. this.mDatas.clear();
  92. notifyDataSetChanged();
  93. }
  94. }

    

    2.2.2.2.然后分析成员变量。

       

       首先声明一个Context

       LayoutInflater用来动态加载界面

         mDatas用来保存数据

         mLayoutId是用来存放布局资源id

    2.2.2.3.然后是构造函数

       

        这里首先从context获取可以加载界面的LayoutInflater

          然后获得context

       然后从外部获取资源id 

    2.2.2.4.然后是Override一个函数onCreateViewHolder

       

        动态加载从构造函数中获取的资源id,返回一个自定义的布局持有者

    2.2.2.5.SingleTypeAdapter中处理数据 

        

        首先这是一个适配器,然后在Override的函数onBindViewHolder中处理数据

        这里交给了一个抽象函数convert中处理,也就是在子类中具体实现抽象函数来处理数据

    2.2.2.6.适配器中其他必要的函数

       

        这里首先是适配器必须实现的getItemCount,返回数据条数。

        然后是添加数据,然后是获取数据,然后是清理数据。

    2.2.2.7.这个构造函数中加载了一个item_topic_reply布局。

        这个布局就是回复中每一个回复的详细布局。 

        

  2.2.3.然后就是实现在SingleTypeAdapter定义的抽象函数了convert。

     

      意义:就是将布局中的控件,附上了实际的数据。

            可以看到,数据全部从一个TopicPeply的一个实例中获取,然后,通过holder的设置函数类设置

          textView的文字,ImageView的图片。

          图片加载方式是调用了一个通用类ImageUtils来loadImage实现。

      评论区的文字是通过获取网页版的html,然后调用Html.fromHtml(HtmlUtil.removeP(...)这样

            来设置的。注意点这里又一个GlideImageGetter类,自定义Html图片获取类,用Glide加载图片,

        并且在textView中显示。

   

      然后就是设置几个控件的监听器,用户点击了头像,用户名之后会跳转到用户Activity中。

2.3.回到TopicContentActivity中,接下来是创建实例的两个函数

  

  这里又两种方式可以跳转到话题详情这个活动

  可以通过话题的类,也可以通过话题的唯一表示id

2.4.实现3个必须实现的抽象类。

  

  首先是getLayoutId==>activity_topic_content.xml,这是整个话题详情页面,包括标题栏toolbar。

  然后是initDatas==>从intent获取话题唯一标识id,和话题类

        如果topic不为空且话题唯一id不小于0,则将话题类的id的数据赋给topic_id。即统一化。

  然后是initViews==>这里要新建一个数据缓存类

  然后初始化RecyclerView,用来显示话题回复列表的显示

  然后初始化MarkdownView,用来显示webView的,且点击了WebView中的图片会跳转到ImageActivity。

  最后是loadData==>初始化topic内从面板的数据。

  

  下面会详细分析这三个函数的具体作用。

3.分析TopicContentActivity剩下的函数

3.1.首先是initRecyclerView(ViewHolder holder)

  

  这个R.id.reply_list指的是这个东西

  

  然后这里涉及到了一个RecyclerViewUtil

  

  意义:当RecyclerView外围嵌套ScrollView时,将滚动事件交给上层处理。

3.2.然后是initMarkdownView

  

  这里的R.id.webview_container指的是这个东西

  

  意义:用来显示这个话题详情的,其实是FrameLayout布局,然后要用到自定义的webView。

        然后这里new了一个自定义WebView。然后动态添加到FrameLayout中。

  然后这里涉及到一个自定义监听器WebImageListener,点击其中的图片,跳转到ImageActivity中。

  

3.3.然后处理登录和非登录状态的显示

  

  这里调用API查看用户是否登录,再决定显示什么,再设置监听器。

3.4.初始化topic内容面板的数据==>loadData()

  

  一开始不理解这个注解

  

  估计这里的意思也是相近的,这里应该是告知编译器,此处要添加js方法。

  首先是showPreview(topic),这个函数怎么定义的?

  

  显示基础数据,包括用户名,时间,标题,回复的数量。

  注意点:时间这里用了一个通用类TimeUtil类来完成相应的转换。

  然后判断是否重新加载topic详情。

  

  注意点:采用了一个NetUtil通用类判断有无网络。

  然后如果没有缓存就需要加载

  注意topic是存在intent中的数据,而如果别人已经更新了,那么这里也是需要重新加载的。

  然后是loadCache==>加载缓存。

  

  从缓存中得到数据放入webView中。

  如果缓存中数据为null,则去请求API调用。

  若是缓存有内容则读取缓存

  

3.5.显示基础数据==>参数有两种,一种Topic,一种TopicContent

    

  

3.6.显示全部数据

  

  全部数据包括话题内容和回复数据。

3.7.请求话题详情的回调

  

  请求成功后,显示所有数据,然后存入缓存。

3.8.请求话题回复的回调

  

  请求成功后,先清空所有数据,然后添加到适配器,然后存入缓存。

3.9.创建回复的回调

  

  清空回复的编辑框,然后调用API,增长评论条数。

3.10.防止webView引起的内存泄漏

  

  清空webView的资源,设置空的链接。

3.11.复写返回按钮

  

  点击返回按钮,即退出这个活动。

3.12.活动的声明周期

  

  onStart中==>注册EventBus,初始化评论。

  onRestart中==>初始化评论。

  onStop中==>反注册EventBus。

  onDestroy中==>清空webView资源,防止内存泄漏。

3.13.处理点击事件

  

  点击了头像、用户名==>跳转到用户活动页面

  点击了登录==>跳转到登录界面

  点击了评论==>先获取,再调用API来实现这个过程

 

3.14.创建菜单,分享点击事件

  

  

  

  这里设置了intent的类型为:text/plain

  Android利用intent分享信息点击这里

  这里利用了intent可以直接分享这个链接。

4.总结一下

4.1.话题内容这个类是我在目前看过最长的一个类,如果之前要是先分析这个类,估计是不懂得。现在来看这个类,就

  相对于简单一些了。定义的变量有Topic的一个实例,一个暂存数据的作用。然后有一个DataCache,缓存数据。

  然后一个TopicReplyAdapter,话题回复列表的适配器,自定义的,继承SingleTypeAdapter<TopicReply>,也

  是自定义的,继承RecylerView.Adapter<RecylerViewHolder>,当然RecyleViewHolder也是自定义的,继承

  RecyclerView.ViewHolder的,它是一个布局持有者,这就是最基本的。

4.2.这里的一个FrameLayout其实是一个用来加载一个webView的,首先在总布局中定义有这个东西,然后将一个动态

  的webView,而且是自定义的webView==>MarkdownView。然后有一个自定义的webViewClient。两者结合

  主要处理webView中的图片点击问题,可以获得所有图片,然后点击其中一个图片可以进入图片浏览页面。

4.3.然后有两个newInstance函数,一开始我以为是看错了,结果它就是有两个,可能就是为了方便外部调用吧,如果

  可以得到某个话题的唯一id,那便可以确定这个话题,如果这个类都知道,那么岂不是更加快得到数据了嘛。

4.4.话题内容的的主体布局分为四大块,一个是标题栏,一个是内容用自定义的webView显示,一个是评论列表,一个

  是评论内容,或者点击登录。所以采用一个NestedScrollView+LinearLayout即可完成。

4.5.初始化数据其实就是从intent中获取数据,外部函数调用newInstance会传入一些数据,比如话题的id什么的,然后

  就可以在initDatas()中获取,所以这里我忽然明白为什么要在BaseActivity中定义这个函数了。因为实现活动跳转的

  时候总是会有携带额外的一些数据的,所以这里不管有没有都在BaseActivity中定义即可。

4.6.初始化视图的时候,这里注意将标题栏的标题设置好,基本上每个页面都会有一个标题吧。然后注意缓存,新建一个

  DataCache,每个项目基本都会涉及到吧。然后注意要加载评论列表和自定义WebView。

4.7.这里话题回复用到了一个TopicReplyAdapter,继承一个SingleTypeAdapter<TopicReply>,然后继承了一个

  RecyclerView.Adapter<RecyclerViewHolder>类,作用就是定义话题回复列表,布局在TopicReplyAdapter中

  定义为R.layout.item_topic_reply,所以RecyclerView.Adapter的作用就是定义最简单的适配器+抽象函数。

4.8.然后讨论一下初始化webView。这个函数的作用就是将new的自定义webView动态加载到FrameLayout中,然后

  设置webView监听事件,即添加了一个自定义的Client和一个js接口。

4.9.然后是初始化topic内容面板中的数据,这里先判断从intent中获得的topic是否为null,如果不为null,就再次判断

  是否需要重新加载,如果不需要就进入loadCache环节。loadCache作用就是从缓存中取出数据,然后将webView

  中的数据显示出来。以及话题回复的内容添加到适配器中。

4.10.然后是显示基础数据,话题内容中要显示的东西有点多,它将webView和其他控件分开了,webView部分先不显

  示,先将其他信息,比如用户名,用户头像显示出来。然后设置一些监听。

4.11.然后这里有3个EventBus的回调,一个是请求话题详细的回调==>请求完之后,展示所有。一个是话题列表的回

  调==>请求完之后,适配器重新添加数据且存进缓存。第三个是创建一个回复==>请求之后,将编辑框清空,然

  后在去请求服务器将评论的条数+1。

4.12.这里学会了如何防止webView引起的内存泄露,原来写webView不知道什么原因有时候就崩溃了,原来是

  webView可能会导致内存泄露的问题,所以今后的webView注意都要在destroy中clear一下。这个函数就很

  关键了。

Diycode开源项目 TopicContentActivity分析的更多相关文章

  1. Diycode开源项目 BaseApplication分析+LeakCanary第三方+CrashHandler自定义异常处理

    1.BaseApplication整个应用的开始 1.1.看一下代码 /* * Copyright 2017 GcsSloop * * Licensed under the Apache Licens ...

  2. DiyCode开源项目 BaseActivity 分析

    1.首先将这个项目的BaseActivity源码拷贝过来. /* * Copyright 2017 GcsSloop * * Licensed under the Apache License, Ve ...

  3. Diycode开源项目 ImageActivity分析

    1.首先看一下效果 1.1做成了一个GIF 1.2.我用格式工厂有点问题,大小无法调到手机这样的大小,目前还没有解决方案. 1.3.网上有免费的MP4->GIF,参考一下这个网站吧. 1.4.讲 ...

  4. Diycode开源项目 UserActivity分析

    1.效果预览 1.1.实际界面预览 1.2. 这是MainActivity中的代码 这里执行了跳转到自己的用户界面的功能. 1.3.点击头像或者用户名跳转到别人的页面 UserActivity的结构由 ...

  5. Diycode开源项目 LoginActivity分析

    1.首先看一下效果 1.1.预览一下真实页面 1.2.分析一下: 要求输入Email或者用户名,点击编辑框,弹出键盘,默认先进入输入Email或用户名编辑框. 点击密码后,密码字样网上浮动一段距离,E ...

  6. Diycode开源项目 MainActivity分析

    1.分析MainActivity整体结构 1.1.首先看一下这个界面的整体效果. 1.2.活动源代码如下 /* * Copyright 2017 GcsSloop * * Licensed under ...

  7. DiyCode开源项目 AboutActivity分析

    1.首先看一下效果 这是手机上显示的效果: 1.1首先是一个标题栏,左侧一个左箭头,然后一个图标. 1.2然后下方是一个可以滑动的页面. 1.3分成了7个部分. 1.4DiyCode的图标. 1.5然 ...

  8. DiyCode开源项目 TopicActivity 分析

    1.首先看看TopActivity效果.    2.TopicActivity是一个继承BaseActivity的.前面分析过BaseActivity了.主要有一个标题栏,有返回的图标. 3.贴一下T ...

  9. Diycode开源项目 SitesListFragment分析

    1.效果预览 1.1.网站列表实际界面 1.2.注意这个界面没有继承SimpleRefreshRecycleFragment 前面的话题和新闻继承了SimpleRefreshRecyclerFragm ...

随机推荐

  1. 数学建模大赛-NO.1

    数学建模大赛-NO.1   论文精析 近期,在网上各种的收罗,张开了各式各样的捕抓.哈哈… .终于,在一个不经意之间发现了一个巨大无比的宝藏式的网站,此网站网址为:http://cjsxjm.gzsi ...

  2. zblog删除网站后台顶部菜单中的“官方网站”链接

    文件\zb_system\function\c_system_admin.php 注释或删除代码 $topmenus[] = MakeTopMenu("misc", $zbp-&g ...

  3. C# 只运行一个实例 ShowWindowAsync 窗体隐藏时失效 解决方案

    如果窗体已经隐藏,那么利用instance.MainWindowHandle得到的句柄为空,继而ShowWindowAsync 操作失败 不过我们可以使用FindWindow来查找到指定窗体的句柄 只 ...

  4. C# Dictionary的遍历

    foreach (KeyValuePair<string, string> kvp in dic) { Console.WriteLine("key:{0},value:{1}& ...

  5. 报错:Program "sh" not found in PATH

    参考原文:http://vin-mail.blog.163.com/blog/static/37895280201211932919513/ 如果你按照我的方法 先配置了cygwin的环境变量,在打开 ...

  6. Hadoop 分片、分组与排序

    首先需要明确的是,hadoop里的key一定要是可排序的,要么key自身实现了WritableComparator接口,要么有一个排序类可以对key进行排序.如果key本身不实现WritableCom ...

  7. gitlab用户添加ssh免密钥认证后clone还是要求输入密码

    今天在centos 7公网服务器上安装gitlab在配置ssh免密钥时遇到一个奇怪的事,正确添加了本机的公钥到gitlab账户上,进行clone时死活都要你输入密码gitlab使用yum安装的,之前在 ...

  8. HTML、CSS、JS、JQ速查笔记

      一.HTML  1.编写html文件 a.格式 <!DOCTYPE html> <html> <head> <title>标题</title& ...

  9. javascript:理解try...catch...finally

    以前,我一直喜欢用console.log(do some thing)去执行输出的类型和值,想马上看到弹出的信息,就会直接在浏览器alert()一下,这些是基础知识. 稍微复杂一点点,就要用到判断语句 ...

  10. selenium显示等待解决浏览器未加载完成查找控件的问题

    问题描述:wap版支付成功后,跳转到支付成功页,查找的元素已出现,如图的:元素1,元素2,但是提示查找的元素超时,失败,并且每到这个页面都会报页面超时,不能查找到页面元素 原始代码: try{ op. ...