Browse Source

1:增加News详情Activity;

guiying712 8 years ago
parent
commit
7171c7049b

+ 5 - 0
module_news/src/main/AndroidManifest.xml

@@ -6,6 +6,11 @@
             android:name=".main.NewsCenterActivity"
             android:theme="@style/AppTheme.NoActionBar"
             android:screenOrientation="portrait" />
+        <activity
+            android:name=".detail.NewsDetailActivity"
+            android:screenOrientation="portrait"
+            android:theme="@style/AppTheme.NoActionBar" />
     </application>
 
+
 </manifest>

+ 11 - 3
module_news/src/main/java/com/guiying/news/data/NewsDataSource.java

@@ -1,6 +1,7 @@
 package com.guiying.news.data;
 
-import com.guiying.common.http.InfoCallback;
+import com.guiying.common.base.InfoCallback;
+import com.guiying.news.data.bean.MessageDetail;
 import com.guiying.news.data.bean.StoryList;
 
 /**
@@ -14,13 +15,20 @@ public interface NewsDataSource {
 
 
     /**
-     * 获取
+     * 获取当天的新闻列表
      *
-     * @param date
+     * @param date     日期
      * @param callback 回调
      */
     void getNewsList(String date, InfoCallback<StoryList> callback);
 
+    /**
+     * 获取某条新闻详情
+     *
+     * @param id       新闻Id
+     * @param callback 回调
+     */
+    void getNewsDetail(String id, InfoCallback<MessageDetail> callback);
 
 }
 

+ 15 - 0
module_news/src/main/java/com/guiying/news/data/bean/MessageDetail.java

@@ -84,4 +84,19 @@ public class MessageDetail {
     public void setId(String id) {
         this.id = id;
     }
+
+    @Override
+    public String toString() {
+        return "MessageDetail{" +
+                "body='" + body + '\'' +
+                ", image_source='" + image_source + '\'' +
+                ", title='" + title + '\'' +
+                ", image='" + image + '\'' +
+                ", share_url='" + share_url + '\'' +
+                ", recommenders=" + recommenders +
+                ", ga_prefix='" + ga_prefix + '\'' +
+                ", type='" + type + '\'' +
+                ", id='" + id + '\'' +
+                '}';
+    }
 }

+ 31 - 1
module_news/src/main/java/com/guiying/news/data/source/RemoteNewsDataSource.java

@@ -1,11 +1,12 @@
 package com.guiying.news.data.source;
 
+import com.guiying.common.base.InfoCallback;
 import com.guiying.common.http.DataType;
 import com.guiying.common.http.HttpClient;
-import com.guiying.common.http.InfoCallback;
 import com.guiying.common.http.OnResultListener;
 import com.guiying.news.Constants;
 import com.guiying.news.data.NewsDataSource;
+import com.guiying.news.data.bean.MessageDetail;
 import com.guiying.news.data.bean.StoryList;
 
 /**
@@ -42,4 +43,33 @@ public class RemoteNewsDataSource implements NewsDataSource {
             }
         });
     }
+
+
+    @Override
+    public void getNewsDetail(String id, final InfoCallback<MessageDetail> callback) {
+        HttpClient client = new HttpClient.Builder()
+                .baseUrl(Constants.ZHIHU_DAILY_BEFORE_MESSAGE_DETAIL)
+                .url(id)
+                .bodyType(DataType.JSON_OBJECT, MessageDetail.class)
+                .build();
+        client.get(new OnResultListener<MessageDetail>() {
+
+            @Override
+            public void onSuccess(MessageDetail result) {
+                callback.onSuccess(result);
+            }
+
+            @Override
+            public void onError(int code, String message) {
+                callback.onError(code, message);
+            }
+
+            @Override
+            public void onFailure(String message) {
+                callback.onError(0, message);
+            }
+        });
+    }
+
+
 }

+ 25 - 0
module_news/src/main/java/com/guiying/news/detail/NewsDetailActivity.java

@@ -0,0 +1,25 @@
+package com.guiying.news.detail;
+
+import android.os.Bundle;
+
+import com.guiying.common.base.BaseActivity;
+
+/**
+ * <p>类说明</p>
+ *
+ * @author 张华洋 2017/7/1 13:13
+ * @version V1.2.0
+ * @name NewsDetailActivity
+ */
+public class NewsDetailActivity extends BaseActivity {
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        NewsDetailView detailView = new NewsDetailView(this);
+        setContentView(detailView);
+        String id = getIntent().getStringExtra("id");
+        new NewsDetailPresenter(detailView).getNewsDetail(id);
+    }
+
+}

+ 35 - 0
module_news/src/main/java/com/guiying/news/detail/NewsDetailContract.java

@@ -0,0 +1,35 @@
+package com.guiying.news.detail;
+
+import com.guiying.common.base.BasePresenter;
+import com.guiying.common.base.BaseView;
+import com.guiying.news.data.bean.MessageDetail;
+
+/**
+ * <p>类说明</p>
+ *
+ * @author 张华洋 2017/2/22 20:33
+ * @version V1.2.0
+ * @name NewsContract
+ */
+public interface NewsDetailContract {
+
+    interface View extends BaseView<Presenter> {
+
+        boolean isActive();
+
+        void showNewsDetail(MessageDetail detail);
+
+    }
+
+    interface Presenter extends BasePresenter {
+
+        /**
+         * 获取最新列表
+         *
+         * @param newsId 新闻id
+         */
+        void getNewsDetail(String newsId);
+
+    }
+
+}

+ 49 - 0
module_news/src/main/java/com/guiying/news/detail/NewsDetailPresenter.java

@@ -0,0 +1,49 @@
+package com.guiying.news.detail;
+
+import com.guiying.common.base.InfoCallback;
+import com.guiying.news.data.NewsDataSource;
+import com.guiying.news.data.bean.MessageDetail;
+import com.guiying.news.data.source.RemoteNewsDataSource;
+
+/**
+ * <p>类说明</p>
+ *
+ * @author 张华洋 2017/2/22 20:33
+ * @version V1.2.0
+ * @name GirlsPresenter
+ */
+public class NewsDetailPresenter implements NewsDetailContract.Presenter {
+
+    private NewsDetailContract.View mView;
+    private NewsDataSource mDataSource;
+
+    public NewsDetailPresenter(NewsDetailContract.View view) {
+        mView = view;
+        mDataSource = new RemoteNewsDataSource();
+        mView.setPresenter(this);
+    }
+
+    @Override
+    public void start() {
+
+    }
+
+
+    @Override
+    public void getNewsDetail(String newsId) {
+        mDataSource.getNewsDetail(newsId, new InfoCallback<MessageDetail>() {
+            @Override
+            public void onSuccess(MessageDetail detail) {
+                if (mView.isActive()) {
+                    mView.showNewsDetail(detail);
+                }
+            }
+
+            @Override
+            public void onError(int code, String message) {
+
+            }
+        });
+    }
+
+}

+ 129 - 0
module_news/src/main/java/com/guiying/news/detail/NewsDetailView.java

@@ -0,0 +1,129 @@
+package com.guiying.news.detail;
+
+import android.content.Context;
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.drawable.BitmapDrawable;
+import android.graphics.drawable.Drawable;
+import android.support.design.widget.CollapsingToolbarLayout;
+import android.text.Html;
+import android.text.method.LinkMovementMethod;
+import android.util.AttributeSet;
+import android.widget.FrameLayout;
+import android.widget.ImageView;
+import android.widget.TextView;
+
+import com.bumptech.glide.Glide;
+import com.bumptech.glide.request.animation.GlideAnimation;
+import com.bumptech.glide.request.target.SimpleTarget;
+import com.guiying.news.R;
+import com.guiying.news.data.bean.MessageDetail;
+
+/**
+ * <p>类说明</p>
+ *
+ * @author 张华洋 2017/7/1 13:18
+ * @version V1.2.0
+ * @name NewsDetailView
+ */
+
+public class NewsDetailView extends FrameLayout implements NewsDetailContract.View {
+
+    private boolean isActive = false;
+    private NewsDetailContract.Presenter mPresenter;
+    private ImageView mToolbarImage;
+    private TextView mToolbarText;
+    private CollapsingToolbarLayout mCollapsingLayout;
+    private TextView mDetailText;
+
+
+    public NewsDetailView(Context context) {
+        super(context);
+        initView();
+    }
+
+    public NewsDetailView(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        initView();
+    }
+
+    private void initView() {
+        inflate(getContext(), R.layout.view_news_detail, this);
+        mToolbarImage = (ImageView) findViewById(R.id.toolbar_image);
+        mToolbarText = (TextView) findViewById(R.id.toolbar_text);
+        mCollapsingLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_layout);
+        mDetailText = (TextView) findViewById(R.id.news_detail_text);
+
+        isActive = true;
+    }
+
+    @Override
+    protected void onAttachedToWindow() {
+        super.onAttachedToWindow();
+        isActive = true;
+    }
+
+    @Override
+    protected void onDetachedFromWindow() {
+        super.onDetachedFromWindow();
+        isActive = false;
+    }
+
+    @Override
+    public boolean isActive() {
+        return isActive;
+    }
+
+
+    @Override
+    public void setPresenter(NewsDetailContract.Presenter presenter) {
+        mPresenter = presenter;
+    }
+
+    @Override
+    public void showNewsDetail(MessageDetail detail) {
+        mCollapsingLayout.setTitle(detail.getTitle());
+        //设置还没收缩时状态下字体颜色
+        mCollapsingLayout.setExpandedTitleColor(Color.WHITE);
+        //设置收缩后Toolbar上字体的颜色
+        mCollapsingLayout.setCollapsedTitleTextColor(Color.WHITE);
+        mToolbarText.setText(detail.getImage_source());
+        Glide.with(getContext())
+                .load(detail.getImage())
+                .thumbnail(0.2f)
+                .into(mToolbarImage);
+        mDetailText.setMovementMethod(LinkMovementMethod.getInstance());
+        mDetailText.setText(Html.fromHtml(detail.getBody(), new Html.ImageGetter() {
+            @Override
+            public Drawable getDrawable(String source) {
+                final URLDrawable urlDrawable = new URLDrawable();
+                Glide.with(getContext()).load(source).asBitmap().into(new SimpleTarget<Bitmap>() {
+
+                    @Override
+                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
+                        urlDrawable.bitmap = resource;
+                        urlDrawable.setBounds(0, 0, resource.getWidth(), resource.getHeight());
+                        mDetailText.invalidate();
+                        // 解决图文重叠
+                        mDetailText.setText(mDetailText.getText());
+                    }
+                });
+                return urlDrawable;
+            }
+        }, null));
+    }
+
+
+    private class URLDrawable extends BitmapDrawable {
+
+        private Bitmap bitmap;
+
+        @Override
+        public void draw(Canvas canvas) {
+            if (bitmap != null) {
+                canvas.drawBitmap(bitmap, 0, 0, getPaint());
+            }
+        }
+    }
+}

+ 12 - 1
module_news/src/main/java/com/guiying/news/main/NewsListAdapter.java

@@ -1,6 +1,8 @@
 package com.guiying.news.main;
 
 import android.content.Context;
+import android.content.Intent;
+import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ImageView;
 import android.widget.TextView;
@@ -9,6 +11,7 @@ import com.bumptech.glide.Glide;
 import com.bumptech.glide.load.engine.DiskCacheStrategy;
 import com.guiying.news.R;
 import com.guiying.news.data.bean.Story;
+import com.guiying.news.detail.NewsDetailActivity;
 import com.jude.easyrecyclerview.adapter.BaseViewHolder;
 import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
 
@@ -44,7 +47,7 @@ public class NewsListAdapter extends RecyclerArrayAdapter<Story> {
         }
 
         @Override
-        public void setData(Story data) {
+        public void setData(final Story data) {
             super.setData(data);
             mTextView.setText(data.getTitle());
             Glide.with(getContext())
@@ -53,6 +56,14 @@ public class NewsListAdapter extends RecyclerArrayAdapter<Story> {
                     .crossFade()
                     .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                     .into(mImageView);
+            itemView.setOnClickListener(new View.OnClickListener() {
+                @Override
+                public void onClick(View view) {
+                    Intent intent = new Intent(getContext(), NewsDetailActivity.class);
+                    intent.putExtra("id", data.getId());
+                    getContext().startActivity(intent);
+                }
+            });
         }
     }
 }

+ 1 - 1
module_news/src/main/java/com/guiying/news/main/NewsListPresenter.java

@@ -1,6 +1,6 @@
 package com.guiying.news.main;
 
-import com.guiying.common.http.InfoCallback;
+import com.guiying.common.base.InfoCallback;
 import com.guiying.news.data.NewsDataSource;
 import com.guiying.news.data.bean.StoryList;
 import com.guiying.news.data.source.RemoteNewsDataSource;

+ 6 - 1
module_news/src/main/java/com/guiying/news/main/NewsListViewAdapter.java

@@ -4,6 +4,8 @@ import android.support.v4.view.PagerAdapter;
 import android.view.View;
 import android.view.ViewGroup;
 
+import java.text.DateFormat;
+import java.util.Calendar;
 import java.util.List;
 
 /**
@@ -50,7 +52,10 @@ public class NewsListViewAdapter extends PagerAdapter {
         if (mTabList == null) {
             return null;
         }
-        return mTabList.get(position);
+        Calendar displayDate = Calendar.getInstance();
+        displayDate.add(Calendar.DAY_OF_YEAR, -position);
+
+        return DateFormat.getDateInstance().format(displayDate.getTime());
     }
 
     /**

+ 10 - 4
module_news/src/main/module/AndroidManifest.xml

@@ -9,15 +9,21 @@
         android:label="@string/news_name"
         android:supportsRtl="true"
         android:theme="@style/AppTheme">
-        <activity
-            android:name=".main.NewsCenterActivity"
-            android:theme="@style/AppTheme.NoActionBar"
-            android:screenOrientation="portrait">
+        <activity android:name="debug.LauncherActivity">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
 
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
+
+        <activity
+            android:name=".main.NewsCenterActivity"
+            android:screenOrientation="portrait"
+            android:theme="@style/AppTheme.NoActionBar" />
+        <activity
+            android:name=".detail.NewsDetailActivity"
+            android:screenOrientation="portrait"
+            android:theme="@style/AppTheme.NoActionBar" />
     </application>
 </manifest>

+ 6 - 8
module_news/src/main/res/layout/activity_news_details.xml → module_news/src/main/res/layout/view_news_detail.xml

@@ -7,10 +7,10 @@
 
     <android.support.design.widget.AppBarLayout
         android:layout_width="match_parent"
-        android:layout_height="160dp">
+        android:layout_height="200dp">
 
         <android.support.design.widget.CollapsingToolbarLayout
-            android:id="@+id/ctl"
+            android:id="@+id/collapsing_layout"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             app:contentScrim="@color/colorPrimary"
@@ -24,14 +24,14 @@
                 app:layout_collapseMode="parallax">
 
                 <ImageView
-                    android:id="@+id/iv_appbar"
+                    android:id="@+id/toolbar_image"
                     android:layout_width="match_parent"
                     android:layout_height="match_parent"
                     android:contentDescription="@null"
                     android:scaleType="fitXY" />
 
                 <TextView
-                    android:id="@+id/tv_img_source"
+                    android:id="@+id/toolbar_text"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_alignParentBottom="true"
@@ -59,12 +59,10 @@
         app:layout_behavior="@string/appbar_scrolling_view_behavior">
 
         <TextView
-            android:id="@+id/tv_body"
+            android:id="@+id/news_detail_text"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
-            android:layout_marginLeft="15dp"
-            android:layout_marginRight="15dp"
-            android:layout_marginTop="15dp"
+            android:layout_margin="@dimen/size_15dp"
             android:textSize="17sp" />
 
     </android.support.v4.widget.NestedScrollView>

+ 1 - 1
module_news/src/main/res/values/strings.xml

@@ -1,5 +1,5 @@
 <resources>
-    <string name="news_name">News</string>
+    <string name="news_name">News组件</string>
 
     <string name="news_activity_title">News</string>