【0147】【项目实战】-【百思不得姐】-【4】基于MVP项目架构实现精华
1. 精华Tab的布局分析
【说明】前面的Tab使用的空间;

【说明】精华布局要实现的实例样式如下:


2.精华布局实现
2.1 圆角图片的处理
【说明】此处使用了自定义的控件
【源码】

1 package com.tz.dream.budejie.pro.essence.view.views; 2 3 import android.content.Context; 4 import android.content.res.TypedArray; 5 import android.graphics.Bitmap; 6 import android.graphics.BitmapShader; 7 import android.graphics.Canvas; 8 import android.graphics.Color; 9 import android.graphics.ColorFilter; 10 import android.graphics.Matrix; 11 import android.graphics.Paint; 12 import android.graphics.RectF; 13 import android.graphics.Shader; 14 import android.graphics.drawable.BitmapDrawable; 15 import android.graphics.drawable.ColorDrawable; 16 import android.graphics.drawable.Drawable; 17 import android.net.Uri; 18 import android.support.annotation.ColorInt; 19 import android.support.annotation.ColorRes; 20 import android.support.annotation.DrawableRes; 21 import android.util.AttributeSet; 22 23 import com.android.volley.toolbox.NetworkImageView; 24 import com.tz.dream.budejie.R; 25 26 27 /** 28 * 处理圆角图片 29 */ 30 public class CircleNetworkImageImage extends NetworkImageView { 31 32 private static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP; 33 34 private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888; 35 private static final int COLORDRAWABLE_DIMENSION = 2; 36 37 private static final int DEFAULT_BORDER_WIDTH = 0; 38 private static final int DEFAULT_BORDER_COLOR = Color.BLACK; 39 private static final int DEFAULT_FILL_COLOR = Color.TRANSPARENT; 40 private static final boolean DEFAULT_BORDER_OVERLAY = false; 41 42 private final RectF mDrawableRect = new RectF(); 43 private final RectF mBorderRect = new RectF(); 44 45 private final Matrix mShaderMatrix = new Matrix(); 46 private final Paint mBitmapPaint = new Paint(); 47 private final Paint mBorderPaint = new Paint(); 48 private final Paint mFillPaint = new Paint(); 49 50 private int mBorderColor = DEFAULT_BORDER_COLOR; 51 private int mBorderWidth = DEFAULT_BORDER_WIDTH; 52 private int mFillColor = DEFAULT_FILL_COLOR; 53 54 private Bitmap mBitmap; 55 private BitmapShader mBitmapShader; 56 private int mBitmapWidth; 57 private int mBitmapHeight; 58 59 private float mDrawableRadius; 60 private float mBorderRadius; 61 62 private ColorFilter mColorFilter; 63 64 private boolean mReady; 65 private boolean mSetupPending; 66 private boolean mBorderOverlay; 67 68 public CircleNetworkImageImage(Context context) { 69 super(context); 70 71 init(); 72 } 73 74 public CircleNetworkImageImage(Context context, AttributeSet attrs) { 75 this(context, attrs, 0); 76 } 77 78 public CircleNetworkImageImage(Context context, AttributeSet attrs, int defStyle) { 79 super(context, attrs, defStyle); 80 81 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView, defStyle, 0); 82 83 mBorderWidth = a.getDimensionPixelSize(R.styleable.CircleImageView_civ_border_width, DEFAULT_BORDER_WIDTH); 84 mBorderColor = a.getColor(R.styleable.CircleImageView_civ_border_color, DEFAULT_BORDER_COLOR); 85 mBorderOverlay = a.getBoolean(R.styleable.CircleImageView_civ_border_overlay, DEFAULT_BORDER_OVERLAY); 86 mFillColor = a.getColor(R.styleable.CircleImageView_civ_fill_color, DEFAULT_FILL_COLOR); 87 88 a.recycle(); 89 90 init(); 91 } 92 93 private void init() { 94 super.setScaleType(SCALE_TYPE); 95 mReady = true; 96 97 if (mSetupPending) { 98 setup(); 99 mSetupPending = false; 100 } 101 } 102 103 @Override 104 public ScaleType getScaleType() { 105 return SCALE_TYPE; 106 } 107 108 @Override 109 public void setScaleType(ScaleType scaleType) { 110 if (scaleType != SCALE_TYPE) { 111 throw new IllegalArgumentException(String.format("ScaleType %s not supported.", scaleType)); 112 } 113 } 114 115 @Override 116 public void setAdjustViewBounds(boolean adjustViewBounds) { 117 if (adjustViewBounds) { 118 throw new IllegalArgumentException("adjustViewBounds not supported."); 119 } 120 } 121 122 @Override 123 protected void onDraw(Canvas canvas) { 124 if (mBitmap == null) { 125 return; 126 } 127 128 if (mFillColor != Color.TRANSPARENT) { 129 canvas.drawCircle(getWidth() / 2.0f, getHeight() / 2.0f, mDrawableRadius, mFillPaint); 130 } 131 canvas.drawCircle(getWidth() / 2.0f, getHeight() / 2.0f, mDrawableRadius, mBitmapPaint); 132 if (mBorderWidth != 0) { 133 canvas.drawCircle(getWidth() / 2.0f, getHeight() / 2.0f, mBorderRadius, mBorderPaint); 134 } 135 } 136 137 @Override 138 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 139 super.onSizeChanged(w, h, oldw, oldh); 140 setup(); 141 } 142 143 public int getBorderColor() { 144 return mBorderColor; 145 } 146 147 public void setBorderColor(@ColorInt int borderColor) { 148 if (borderColor == mBorderColor) { 149 return; 150 } 151 152 mBorderColor = borderColor; 153 mBorderPaint.setColor(mBorderColor); 154 invalidate(); 155 } 156 157 public void setBorderColorResource(@ColorRes int borderColorRes) { 158 setBorderColor(getContext().getResources().getColor(borderColorRes)); 159 } 160 161 public int getFillColor() { 162 return mFillColor; 163 } 164 165 public void setFillColor(@ColorInt int fillColor) { 166 if (fillColor == mFillColor) { 167 return; 168 } 169 170 mFillColor = fillColor; 171 mFillPaint.setColor(fillColor); 172 invalidate(); 173 } 174 175 public void setFillColorResource(@ColorRes int fillColorRes) { 176 setFillColor(getContext().getResources().getColor(fillColorRes)); 177 } 178 179 public int getBorderWidth() { 180 return mBorderWidth; 181 } 182 183 public void setBorderWidth(int borderWidth) { 184 if (borderWidth == mBorderWidth) { 185 return; 186 } 187 188 mBorderWidth = borderWidth; 189 setup(); 190 } 191 192 public boolean isBorderOverlay() { 193 return mBorderOverlay; 194 } 195 196 public void setBorderOverlay(boolean borderOverlay) { 197 if (borderOverlay == mBorderOverlay) { 198 return; 199 } 200 201 mBorderOverlay = borderOverlay; 202 setup(); 203 } 204 205 @Override 206 public void setImageBitmap(Bitmap bm) { 207 super.setImageBitmap(bm); 208 mBitmap = bm; 209 setup(); 210 } 211 212 @Override 213 public void setImageDrawable(Drawable drawable) { 214 super.setImageDrawable(drawable); 215 mBitmap = getBitmapFromDrawable(drawable); 216 setup(); 217 } 218 219 @Override 220 public void setImageResource(@DrawableRes int resId) { 221 super.setImageResource(resId); 222 mBitmap = getBitmapFromDrawable(getDrawable()); 223 setup(); 224 } 225 226 @Override 227 public void setImageURI(Uri uri) { 228 super.setImageURI(uri); 229 mBitmap = uri != null ? getBitmapFromDrawable(getDrawable()) : null; 230 setup(); 231 } 232 233 @Override 234 public void setColorFilter(ColorFilter cf) { 235 if (cf == mColorFilter) { 236 return; 237 } 238 239 mColorFilter = cf; 240 mBitmapPaint.setColorFilter(mColorFilter); 241 invalidate(); 242 } 243 244 private Bitmap getBitmapFromDrawable(Drawable drawable) { 245 if (drawable == null) { 246 return null; 247 } 248 249 if (drawable instanceof BitmapDrawable) { 250 return ((BitmapDrawable) drawable).getBitmap(); 251 } 252 253 try { 254 Bitmap bitmap; 255 256 if (drawable instanceof ColorDrawable) { 257 bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG); 258 } else { 259 bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), BITMAP_CONFIG); 260 } 261 262 Canvas canvas = new Canvas(bitmap); 263 drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); 264 drawable.draw(canvas); 265 return bitmap; 266 } catch (Exception e) { 267 e.printStackTrace(); 268 return null; 269 } 270 } 271 272 private void setup() { 273 if (!mReady) { 274 mSetupPending = true; 275 return; 276 } 277 278 if (getWidth() == 0 && getHeight() == 0) { 279 return; 280 } 281 282 if (mBitmap == null) { 283 invalidate(); 284 return; 285 } 286 287 mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 288 289 mBitmapPaint.setAntiAlias(true); 290 mBitmapPaint.setShader(mBitmapShader); 291 292 mBorderPaint.setStyle(Paint.Style.STROKE); 293 mBorderPaint.setAntiAlias(true); 294 mBorderPaint.setColor(mBorderColor); 295 mBorderPaint.setStrokeWidth(mBorderWidth); 296 297 mFillPaint.setStyle(Paint.Style.FILL); 298 mFillPaint.setAntiAlias(true); 299 mFillPaint.setColor(mFillColor); 300 301 mBitmapHeight = mBitmap.getHeight(); 302 mBitmapWidth = mBitmap.getWidth(); 303 304 mBorderRect.set(0, 0, getWidth(), getHeight()); 305 mBorderRadius = Math.min((mBorderRect.height() - mBorderWidth) / 2.0f, (mBorderRect.width() - mBorderWidth) / 2.0f); 306 307 mDrawableRect.set(mBorderRect); 308 if (!mBorderOverlay) { 309 mDrawableRect.inset(mBorderWidth, mBorderWidth); 310 } 311 mDrawableRadius = Math.min(mDrawableRect.height() / 2.0f, mDrawableRect.width() / 2.0f); 312 313 updateShaderMatrix(); 314 invalidate(); 315 } 316 317 private void updateShaderMatrix() { 318 float scale; 319 float dx = 0; 320 float dy = 0; 321 322 mShaderMatrix.set(null); 323 324 if (mBitmapWidth * mDrawableRect.height() > mDrawableRect.width() * mBitmapHeight) { 325 scale = mDrawableRect.height() / (float) mBitmapHeight; 326 dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f; 327 } else { 328 scale = mDrawableRect.width() / (float) mBitmapWidth; 329 dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f; 330 } 331 332 mShaderMatrix.setScale(scale, scale); 333 mShaderMatrix.postTranslate((int) (dx + 0.5f) + mDrawableRect.left, (int) (dy + 0.5f) + mDrawableRect.top); 334 335 mBitmapShader.setLocalMatrix(mShaderMatrix); 336 } 337 }
【布局】

【源码】
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:circle="http://schemas.android.com/apk/res/com.tz.dream.budejie" 4 android:orientation="vertical" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent"> 7 8 <LinearLayout 9 android:layout_width="match_parent" 10 android:layout_height="60dp" 11 android:orientation="horizontal" 12 android:gravity="center_vertical"> 13 14 <com.tz.dream.budejie.pro.essence.view.views.CircleNetworkImageImage 15 android:id="@+id/iv_header" 16 android:layout_width="40dp" 17 android:layout_height="40dp" 18 android:layout_marginLeft="@dimen/margin_left_12_dp" 19 circle:civ_border_width="1dp" 20 circle:civ_border_color="@android:color/transparent" 21 circle:civ_border_overlay="true" 22 circle:civ_fill_color="@android:color/transparent" 23 android:src="@drawable/item_essence_header_default"/> 24 25 <LinearLayout 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" 28 android:orientation="vertical" 29 android:layout_marginLeft="@dimen/margin_left_5_dp"> 30 31 <TextView 32 android:id="@+id/tv_name" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:textSize="@dimen/item_essence_name_size" 36 android:textColor="@color/main_bottom_text_select" 37 android:text="热点追踪" /> 38 39 <TextView 40 android:id="@+id/tv_time" 41 android:layout_width="wrap_content" 42 android:layout_height="wrap_content" 43 android:textSize="@dimen/item_essence_name_size" 44 android:textColor="@color/main_bottom_text_select" 45 android:text="05-26 11:48:58" 46 android:layout_marginTop="2dp"/> 47 48 </LinearLayout> 49 50 </LinearLayout> 51 52 <TextView 53 android:id="@+id/tv_content" 54 android:layout_width="wrap_content" 55 android:layout_height="wrap_content" 56 android:textSize="@dimen/item_essence_name_size" 57 android:textColor="@color/main_bottom_text_select" 58 android:text="付出不亚于任何人努力" 59 android:layout_marginTop="2dp" 60 android:layout_marginLeft="@dimen/margin_left_6_dp" 61 android:layout_marginRight="@dimen/margin_left_6_dp"/> 62 63 <ImageView 64 android:id="@+id/iv_video" 65 android:layout_width="match_parent" 66 android:layout_height="wrap_content" 67 android:layout_marginLeft="@dimen/margin_left_6_dp" 68 android:layout_marginRight="@dimen/margin_left_6_dp" 69 android:layout_marginTop="@dimen/margin_left_5_dp" 70 android:src="@drawable/item_essence_video_bg" 71 android:scaleType="centerCrop"/> 72 73 <LinearLayout 74 android:layout_width="match_parent" 75 android:layout_height="50dp" 76 android:orientation="horizontal"> 77 <LinearLayout 78 android:id="@+id/ll_like" 79 android:layout_width="0dp" 80 android:layout_height="match_parent" 81 android:layout_weight="1" 82 android:orientation="horizontal" 83 android:gravity="center"> 84 85 <ImageView 86 android:id="@+id/iv_like" 87 android:layout_width="26dp" 88 android:layout_height="26dp" 89 android:src="@drawable/essence_ding_selector" 90 android:scaleType="fitXY"/> 91 92 <TextView 93 android:id="@+id/tv_like" 94 android:layout_width="wrap_content" 95 android:layout_height="wrap_content" 96 android:text="709" 97 android:layout_marginLeft="@dimen/margin_left_2_dp"/> 98 99 </LinearLayout> 100 101 <LinearLayout 102 android:id="@+id/ll_dislike" 103 android:layout_width="0dp" 104 android:layout_height="match_parent" 105 android:layout_weight="1" 106 android:orientation="horizontal" 107 android:gravity="center"> 108 109 <ImageView 110 android:id="@+id/iv_dislike" 111 android:layout_width="26dp" 112 android:layout_height="26dp" 113 android:src="@drawable/essence_cai_selector" 114 android:scaleType="fitXY"/> 115 116 <TextView 117 android:id="@+id/tv_dislike" 118 android:layout_width="wrap_content" 119 android:layout_height="wrap_content" 120 android:text="709" 121 android:layout_marginLeft="@dimen/margin_left_2_dp"/> 122 123 </LinearLayout> 124 125 <LinearLayout 126 android:id="@+id/ll_forword" 127 android:layout_width="0dp" 128 android:layout_height="match_parent" 129 android:layout_weight="1" 130 android:orientation="horizontal" 131 android:gravity="center"> 132 133 <ImageView 134 android:id="@+id/iv_forword" 135 android:layout_width="26dp" 136 android:layout_height="26dp" 137 android:src="@drawable/essence_forword_selector" 138 android:scaleType="fitXY"/> 139 140 <TextView 141 android:id="@+id/tv_forword" 142 android:layout_width="wrap_content" 143 android:layout_height="wrap_content" 144 android:text="709" 145 android:layout_marginLeft="@dimen/margin_left_2_dp"/> 146 147 </LinearLayout> 148 149 <LinearLayout 150 android:id="@+id/ll_comment" 151 android:layout_width="0dp" 152 android:layout_height="match_parent" 153 android:layout_weight="1" 154 android:orientation="horizontal" 155 android:gravity="center"> 156 157 <ImageView 158 android:id="@+id/iv_comment" 159 android:layout_width="26dp" 160 android:layout_height="26dp" 161 android:src="@drawable/essence_commend_selector" 162 android:scaleType="fitXY"/> 163 164 <TextView 165 android:id="@+id/tv_comment" 166 android:layout_width="wrap_content" 167 android:layout_height="wrap_content" 168 android:text="709" 169 android:layout_marginLeft="@dimen/margin_left_2_dp"/> 170 171 </LinearLayout> 172 </LinearLayout> 173 174 </LinearLayout>
3.配置网络请求框架
【说明】系统自带的网络请求已经android放弃,使用okHttp;

【替换网络框架】只需要实现此接口就可以;


4.配置网络请求框架












【分页数据的加载】

5.接口测试
【添加下拉刷新的控件】

【测试】打断点




【取回的json数据进行格式化】



6.UI实现

【绑定代理】



7.下拉刷新组件集成
【布局的添加】

【适配器的代码】

【源码】com.tz.dream.budejie.pro.essence.view.adapter.EssenceAdapter
1 package com.tz.dream.budejie.pro.essence.view.adapter; 2 3 import android.support.v4.app.Fragment; 4 import android.support.v4.app.FragmentManager; 5 import android.support.v4.app.FragmentStatePagerAdapter; 6 7 import com.tz.dream.budejie.pro.essence.view.EssenceVideoFragment; 8 9 import java.util.List; 10 11 12 public class EssenceAdapter extends FragmentStatePagerAdapter { 13 14 public static final String TAB_TAG = "@dream@"; 15 16 private List<String> mTitles; 17 18 public EssenceAdapter(FragmentManager fm, List<String> titles) { 19 super(fm); 20 mTitles = titles; 21 } 22 23 @Override 24 public Fragment getItem(int position) { 25 EssenceVideoFragment fragment = new EssenceVideoFragment(); 26 String[] title = mTitles.get(position).split(TAB_TAG); 27 fragment.setType(Integer.parseInt(title[1])); 28 fragment.setTitle(title[0]); 29 return fragment; 30 } 31 32 @Override 33 public int getCount() { 34 return mTitles.size(); 35 } 36 37 @Override 38 public CharSequence getPageTitle(int position) { 39 return mTitles.get(position).split(TAB_TAG)[0]; 40 } 41 }
【源码】com.tz.dream.budejie.pro.essence.view.adapter.EssenceVideoAdapter
1 package com.tz.dream.budejie.pro.essence.view.adapter; 2 3 import android.content.Context; 4 import android.graphics.Bitmap; 5 import android.support.v7.widget.RecyclerView; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.ImageView; 10 import android.widget.LinearLayout; 11 import android.widget.TextView; 12 13 import com.android.volley.RequestQueue; 14 import com.android.volley.toolbox.ImageLoader; 15 import com.android.volley.toolbox.NetworkImageView; 16 import com.android.volley.toolbox.Volley; 17 import com.andview.refreshview.recyclerview.BaseRecyclerAdapter; 18 import com.tz.dream.budejie.R; 19 import com.tz.dream.budejie.bean.PostsListBean; 20 import com.tz.dream.budejie.pro.essence.view.views.CircleNetworkImageImage; 21 import com.tz.dream.budejie.utils.DateUtils; 22 23 import java.util.List; 24 25 26 /** 27 * 显示和缓存数据 28 */ 29 public class EssenceVideoAdapter extends 30 BaseRecyclerAdapter<EssenceVideoAdapter.VideoAdapterViewHolder> { 31 32 private Context context; 33 private List<PostsListBean.PostList> list; 34 35 public EssenceVideoAdapter(List<PostsListBean.PostList> list, Context context) { 36 this.context = context; 37 this.list = list; 38 } 39 40 41 /** 42 * 配置ViewHoder 43 * @param view 44 * @return 45 */ 46 @Override 47 public VideoAdapterViewHolder getViewHolder(View view) { 48 return new VideoAdapterViewHolder(view, false); 49 } 50 51 //创建布局 52 @Override 53 public VideoAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) { 54 View v = LayoutInflater.from(context).inflate( 55 R.layout.item_essence_video_layout, parent, false); 56 VideoAdapterViewHolder holder = new VideoAdapterViewHolder(v, true); 57 return holder; 58 } 59 60 //给我们的视图绑定数据 61 @Override 62 public void onBindViewHolder(VideoAdapterViewHolder holder, int position, boolean isItem) { 63 PostsListBean.PostList postList = this.list.get(position); 64 loadImage(holder.iv_header,postList.getProfile_image()); 65 holder.tv_name.setText(postList.getName()); 66 holder.tv_time.setText(DateUtils.parseDate(postList.getCreate_time())); 67 holder.tv_content.setText(postList.getText()); 68 holder.tv_like.setText(postList.getDing()); 69 holder.tv_dislike.setText(postList.getCai()); 70 holder.tv_forword.setText(postList.getRepost()); 71 holder.tv_comment.setText(postList.getComment()); 72 } 73 74 @Override 75 public int getAdapterItemCount() { 76 return this.list.size(); 77 } 78 79 //Volley框架 80 private void loadImage(NetworkImageView imageView, String url){ 81 RequestQueue queue = Volley.newRequestQueue(context); 82 ImageLoader imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() { 83 84 @Override 85 public void putBitmap(String url, Bitmap bitmap) { 86 87 } 88 89 @Override 90 public Bitmap getBitmap(String url) { 91 return null; 92 } 93 }); 94 imageView.setImageUrl(url,imageLoader); 95 } 96 97 public class VideoAdapterViewHolder extends RecyclerView.ViewHolder { 98 99 public CircleNetworkImageImage iv_header; 100 public TextView tv_name; 101 public TextView tv_time; 102 public TextView tv_content; 103 public ImageView iv_video; 104 105 public LinearLayout ll_like; 106 public TextView tv_like; 107 108 public LinearLayout ll_dislike; 109 public TextView tv_dislike; 110 111 public LinearLayout ll_forword; 112 public TextView tv_forword; 113 114 public LinearLayout ll_comment; 115 public TextView tv_comment; 116 117 public int position; 118 119 public VideoAdapterViewHolder(View itemView, boolean isItem) { 120 super(itemView); 121 if (isItem) { 122 iv_header = (CircleNetworkImageImage) itemView 123 .findViewById(R.id.iv_header); 124 tv_name = (TextView) itemView 125 .findViewById(R.id.tv_name); 126 tv_time = (TextView) itemView 127 .findViewById(R.id.tv_time); 128 tv_content = (TextView) itemView 129 .findViewById(R.id.tv_content); 130 iv_video = (ImageView) itemView 131 .findViewById(R.id.iv_video); 132 133 ll_like = (LinearLayout) itemView 134 .findViewById(R.id.ll_like); 135 tv_like = (TextView) itemView 136 .findViewById(R.id.tv_like); 137 138 ll_dislike = (LinearLayout) itemView 139 .findViewById(R.id.ll_dislike); 140 tv_dislike = (TextView) itemView 141 .findViewById(R.id.tv_dislike); 142 143 ll_forword = (LinearLayout) itemView 144 .findViewById(R.id.ll_forword); 145 tv_forword = (TextView) itemView 146 .findViewById(R.id.tv_forword); 147 148 ll_comment = (LinearLayout) itemView 149 .findViewById(R.id.ll_comment); 150 tv_comment = (TextView) itemView 151 .findViewById(R.id.tv_comment); 152 } 153 154 } 155 } 156 }
【数据的初始化】


【源码】
1 package com.tz.dream.budejie.pro.essence.view; 2 3 import android.support.design.widget.TabLayout; 4 import android.support.v4.view.ViewPager; 5 import android.view.View; 6 import android.view.ViewGroup; 7 8 import com.tz.dream.budejie.R; 9 import com.tz.dream.budejie.pro.base.view.BaseFragment; 10 import com.tz.dream.budejie.pro.essence.view.adapter.EssenceAdapter; 11 import com.tz.dream.budejie.pro.essence.view.navigation.EssenceNavigationBuilder; 12 import com.tz.dream.budejie.utils.ToastUtil; 13 14 import java.util.Arrays; 15 16 /** 17 * 18 * 精华 19 */ 20 public class EssenceFragment extends BaseFragment{ 21 22 private TabLayout tab_essence; 23 private ViewPager vp_essence; 24 25 @Override 26 public int getContentView() { 27 return R.layout.fragment_essence; 28 } 29 30 @Override 31 public void initContentView(View viewContent) { 32 initToolBar(viewContent); 33 this.tab_essence = (TabLayout) viewContent.findViewById(R.id.tab_essence); 34 this.vp_essence = (ViewPager) viewContent.findViewById(R.id.vp_essence); 35 } 36 37 private void initToolBar(View viewContent){ 38 EssenceNavigationBuilder builder = new EssenceNavigationBuilder(getContext()); 39 builder.setTitleIcon(R.drawable.main_essence_title) 40 .setLeftIcon(R.drawable.main_essence_btn_selector) 41 .setRightIcon(R.drawable.main_essence_btn_more_selector) 42 .setLeftIconOnClickListener(new View.OnClickListener() { 43 @Override 44 public void onClick(View v) { 45 ToastUtil.showToast(getContext(),"点击了"); 46 } 47 }) 48 .setRightIconOnClickListener(new View.OnClickListener() { 49 @Override 50 public void onClick(View v) { 51 ToastUtil.showToast(getContext(),"点击了"); 52 } 53 }); 54 builder.createAndBind((ViewGroup) viewContent); 55 } 56 57 @Override 58 public void initData() { 59 String[] titles = getResources().getStringArray(R.array.essence_video_tab); 60 EssenceAdapter adapter = 61 new EssenceAdapter(getFragmentManager(), Arrays.asList(titles)); 62 this.vp_essence.setAdapter(adapter); 63 this.tab_essence.setupWithViewPager(this.vp_essence); 64 } 65 }
【源码】
1 package com.tz.dream.budejie.pro.essence.view; 2 3 import android.support.v7.widget.LinearLayoutManager; 4 import android.support.v7.widget.RecyclerView; 5 import android.view.View; 6 7 import com.andview.refreshview.XRefreshView; 8 import com.andview.refreshview.XRefreshViewFooter; 9 import com.tz.dream.budejie.R; 10 import com.tz.dream.budejie.bean.PostsListBean; 11 import com.tz.dream.budejie.mvp.presenter.impl.MvpBasePresenter; 12 import com.tz.dream.budejie.pro.base.presenter.BasePresenter; 13 import com.tz.dream.budejie.pro.base.view.BaseFragment; 14 import com.tz.dream.budejie.pro.essence.presenter.EssenceVideoPresenter; 15 import com.tz.dream.budejie.pro.essence.view.adapter.EssenceVideoAdapter; 16 import com.tz.dream.budejie.utils.ToastUtil; 17 18 import java.util.ArrayList; 19 import java.util.List; 20 21 22 /** 23 * Created by Dream on 16/5/27. 24 */ 25 public class EssenceVideoFragment extends BaseFragment{ 26 27 private int mType = 0; 28 private String mTitle; 29 30 private EssenceVideoPresenter presenter; 31 32 private RecyclerView recyclerView; 33 private XRefreshView xRefreshView; 34 private EssenceVideoAdapter videoAdapter; 35 36 private List<PostsListBean.PostList> postList = new ArrayList<>(); 37 38 public void setType(int mType) { 39 this.mType = mType; 40 } 41 public void setTitle(String title){ 42 this.mTitle = title; 43 } 44 45 @Override 46 public MvpBasePresenter bindPresenter() { 47 presenter = new EssenceVideoPresenter(getContext()); 48 return presenter; 49 } 50 51 @Override 52 public int getContentView() { 53 return R.layout.fragment_essence_video; 54 } 55 56 @Override 57 public void initContentView(View contentView) { 58 xRefreshView = (XRefreshView) contentView.findViewById(R.id.xrefreshview); 59 // 设置是否可以下拉刷新 60 xRefreshView.setPullRefreshEnable(true); 61 // 设置是否可以上拉加载 62 xRefreshView.setPullLoadEnable(true); 63 // 设置刷新完成以后,headerview固定的时间 64 xRefreshView.setPinnedTime(1000); 65 // 静默加载模式不能设置footerview 66 // 设置支持自动刷新 67 xRefreshView.setAutoLoadMore(true); 68 //设置静默加载时提前加载的item个数 69 // xRefreshView.setPreLoadCount(2); 70 71 recyclerView = (RecyclerView) contentView.findViewById(R.id.recycler_view_test_rv); 72 recyclerView.setHasFixedSize(true); 73 74 recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); 75 76 videoAdapter = new EssenceVideoAdapter(postList,getContext()); 77 recyclerView.setAdapter(videoAdapter); 78 videoAdapter.setCustomLoadMoreView(new XRefreshViewFooter(getContext())); 79 80 xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() { 81 82 @Override 83 public void onRefresh() { 84 loadData(true); 85 } 86 87 @Override 88 public void onLoadMore(boolean isSlience) { 89 loadData(false); 90 } 91 }); 92 } 93 94 @Override 95 public void initData() { 96 loadData(true); 97 } 98 99 private void loadData(final boolean isDownRefresh){ 100 presenter.getEssenceList(mType, isDownRefresh, new BasePresenter.OnUIThreadListener<List<PostsListBean.PostList>>() { 101 @Override 102 public void onResult(List<PostsListBean.PostList> result) { 103 if (isDownRefresh){ 104 xRefreshView.stopRefresh(); 105 }else{ 106 xRefreshView.stopLoadMore(); 107 } 108 if (result == null){ 109 ToastUtil.showToast(getContext(),"加载失败"); 110 }else { 111 ToastUtil.showToast(getContext(),"加载成功"); 112 //刷新UI 113 if (isDownRefresh){ 114 //如果你是下拉刷新,我就情况列表 115 postList.clear(); 116 } 117 postList.addAll(result); 118 videoAdapter.notifyDataSetChanged(); 119 } 120 } 121 }); 122 } 123 124 }

【来自于真实的网络数据】

浙公网安备 33010602011771号