高仿知乎日报(四)[主页面条目显示]

by seaicelin
转载请注明出处:http://blog.csdn.net/amd123456789/article/details/52447826

这篇继续解决上一篇遗留下来的问题:

  • 点击条目显示具体的知乎日报信息怎么实现?

很简单,让ContentActivity的recylerView响应点击事件便可。先来看看我的代码结构吧,方便查看对应是哪个类。请看下图:
这里写图片描述


  1. ViewPager的点击事件响应,本来一开始打算用setOnPageChangeListener来实现,但发现这个回调已经在实现轮询播放的时候占用了并且每次滑动都会调用,所以不应该在这里做处理。那么只能在viewPager的adapter里面加载页面的时候做响应。RecylerClickListener是一个接口,用来响应条目的点击事件,在ContentActivity实现这个接口。下面是HomeViewPagerAdapter:

    package com.example.seaice.zhihuribao.adapter;
    
    import android.support.v4.view.PagerAdapter;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;
    import com.example.seaice.zhihuribao.R;
    import com.example.seaice.zhihuribao.RecylerClickListener;
    import com.example.seaice.zhihuribao.Utils.BaseApplication;
    import com.example.seaice.zhihuribao.bean.HomeTopInfo;
    import com.squareup.picasso.Picasso;
    import java.util.List;
    import butterknife.ButterKnife;
    
    /**
     * viewPager的adapter
     * Created by seaice on 2016/8/18.
     */
    public class HomeViewPagerAdapter extends PagerAdapter {
       private List<HomeTopInfo> homeTopInfoList;
       private RecylerClickListener mListener;
    
       public HomeViewPagerAdapter(List<HomeTopInfo> imageViewList) {
           this.homeTopInfoList = imageViewList;
       }
    
       //设置item listener
       public void setOnItemClickListener(RecylerClickListener listener) {
           this.mListener = listener;
       }
    
       @Override
       public Object instantiateItem(ViewGroup viewGroup, final int position) {
           LayoutInflater layoutInflater = LayoutInflater.from(BaseApplication.getApplication());
           View RootView = layoutInflater.inflate(R.layout.top_view_pager, viewGroup, false);
           //响应viewPager的点击事件
           RootView.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   if (mListener != null) {
                       mListener.onPagerClick(null, position);
                   }
               }
           });
           ButterKnife.inject(RootView);
           TextView topTitle = ButterKnife.findById(RootView, R.id.top_title);
           ImageView topImage = ButterKnife.findById(RootView, R.id.top_image);
           topTitle.setText(homeTopInfoList.get(position).getTitle());
                     Picasso.with(BaseApplication.getApplication()).load(homeTopInfoList.get(position).getImage()).fit().into(topImage);
           viewGroup.addView(RootView);
           return RootView;
       }
    
       @Override
       public int getCount() {
           return homeTopInfoList.size();
       }
    
       @Override
       public boolean isViewFromObject(View view, Object object) {
           return view == object;
       }
    
       @Override
       public void destroyItem(ViewGroup viewGroup, int position, Object object) {
           viewGroup.removeView((View) object);
       }
    }

  2. RecylerView对应条目的点击事件,同样也是调用RecylerClickListener这个接口来实现:

    package com.example.seaice.zhihuribao;
    import android.view.View;
    
    public interface RecylerClickListener {
        void onItemClck(View view, int position);//用于recyleview的普通item
        void onPagerClick(View view, int position);//用于viewPager来实现
    }

    在HomeRecylerViewAdapter的MyViewHolder里面实现点击,调用这个接口。之前在MyViewHolder里面的控件设置setOnClickListener都没有效果,至今还没弄清楚为啥?最后发现必须是设置Holder.itemView的click监听才能实现点击回调,这个需要注意一下,请看代码:

    //显示日报条目的Holder
    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public ImageView iv_title;
        public TextView tv_title;
        private RecylerClickListener listener;
    
        public MyViewHolder(View view, RecylerClickListener listener) {
            super(view);
            iv_title = ButterKnife.findById(view, R.id.iv_title);
            tv_title = ButterKnife.findById(view, R.id.tv_title);
            this.listener = listener;
            this.itemView.setOnClickListener(this);//必须是itemView才能设置
        }
    
        @Override
        public void onClick(View v) {
            LogUtils.e("myviewholder onclick");
            if (listener != null) {
                listener.onItemClck(v, getPosition() - 1);
            }
        }
    }

  3. ContentActivity实现RecylerClickListener接口,RecylerView的条目点击后进入到日报的详情页面NewsInfoActivity:

        //recyler条目点击的回调
        @Override
        public void onItemClck(View view, int position) {
            HomeStoriesInfo info = homeInfo.getHomeStoriesInfos().get(position);
            Toast.makeText(this, info.getTitle(), Toast.LENGTH_SHORT).show();
            String id = info.getId();
            Intent intent = new Intent();
            intent.putExtra("id", id);
            intent.setClass(this, NewsInfoActivity.class);
            startActivity(intent);
        }
    
        //viewpager点击的回调
        @Override
        public void onPagerClick(View view, int position) {
            HomeTopInfo info = homeInfo.getHomeTopInfoList().get(position);
            String id = info.getId();
            Intent intent = new Intent();
            intent.putExtra("id", id);
            intent.setClass(this, NewsInfoActivity.class);
            startActivity(intent);
        }

  4. NewsInfoActivity就是显示日报具体某条新闻的页面,组成就是一条顶端的bar+webView。直接看布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <com.example.seaice.zhihuribao.view.NewsScrollView
        android:id="@+id/newsScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
    
       <RelativeLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent">
           <RelativeLayout
               android:id="@+id/rr_header"
               android:layout_width="match_parent"
               android:layout_height="300dp">
    
               <ImageView
                   android:id="@+id/iv_news"
                   android:layout_width="match_parent"
                   android:layout_height="300dp"
                   android:scaleType="fitXY" />
    
               <TextView
                   android:id="@+id/tv_title"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_alignParentBottom="true"
                   android:layout_margin="5dp"
                   android:padding="10dp"
                   android:textColor="@android:color/white"
                   android:textSize="25sp" />
    
               <TextView
                   android:id="@+id/tv_autor"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_alignParentBottom="true"
                   android:layout_alignParentRight="true"
                   android:layout_margin="5dp"
                   android:textColor="@android:color/darker_gray"
                   android:textSize="15sp" />
           </RelativeLayout>
    
           <WebView
               android:id="@+id/webView"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:layout_below="@id/rr_header"></WebView>
       </RelativeLayout>
    </com.example.seaice.zhihuribao.view.NewsScrollView>
    
    <include
        android:id="@+id/bar"
        layout="@layout/news_info_bar"></include>
    </RelativeLayout>

    NewsInfoActivity实际上不用做什么操作,直接加载布局文件就搭建出页面来了,当然部分功能未实现,先把大概写出来吧。
    这里写图片描述

    package com.example.seaice.zhihuribao;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.widget.ImageView;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    import com.example.seaice.zhihuribao.Utils.BaseApplication;
    import com.example.seaice.zhihuribao.Utils.LogUtils;
    import com.example.seaice.zhihuribao.Utils.ThreadMgr;
    import com.example.seaice.zhihuribao.Utils.UiUtils;
    import com.example.seaice.zhihuribao.bean.NewsBarInfo;
    import com.example.seaice.zhihuribao.bean.NewsInfo;
    import com.example.seaice.zhihuribao.protocol.NewsBarProtocol;
    import com.example.seaice.zhihuribao.protocol.NewsItemProtocol;
    import com.example.seaice.zhihuribao.view.NewsScrollView;
    import com.squareup.picasso.Picasso;
    import butterknife.ButterKnife;
    import butterknife.InjectView;
    
    
    public class NewsInfoActivity extends AppCompatActivity implements NewsScrollView.OnScrollListener {
    
    @InjectView(R.id.webView)
    WebView webView;
    @InjectView(R.id.iv_news)
    ImageView iv_news;
    @InjectView(R.id.tv_title)
    TextView tv_title;
    @InjectView(R.id.tv_autor)
    TextView tv_autor;
    @InjectView(R.id.bar)
    RelativeLayout rrBar;
    @InjectView(R.id.newsScrollView)
    NewsScrollView newsScrollView;
    @InjectView(R.id.iv_back)
    ImageView iv_back;
    @InjectView(R.id.iv_share)
    ImageView iv_share;
    @InjectView(R.id.tv_collected)
    ImageView tv_collected;
    @InjectView(R.id.tv_comment)
    TextView tv_comment;
    @InjectView(R.id.tv_praise)
    TextView tv_praise;
    
    private NewsInfo newsInfo;
    private NewsBarInfo barInfo;
    private String id;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_info);
        ButterKnife.inject(this);
    
        initData();
        initView();
    }
    
    private void initView() {
    
    }
    
    private void initData() {
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        LogUtils.e(id);
    
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                NewsItemProtocol newsProtocol = new NewsItemProtocol(id);
                newsInfo = newsProtocol.loadData();
                NewsBarProtocol newsBarProtocol = new NewsBarProtocol(id);
                barInfo = newsBarProtocol.loadData();
                LogUtils.e(barInfo.toString());
                UiUtils.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        setDisplayheader();
                        setWebViewDiplay();
                        setNewsBarDisplay();
                    }
                });
            }
        };
        ThreadMgr.getThreadPool().execute(runnable);
    }
    
    //设置顶部bar显示
    private void setNewsBarDisplay() {
        tv_comment.setText(String.valueOf(barInfo.getComments()));
        tv_praise.setText(String.valueOf(barInfo.getPopularity()));
    }
    
    //显示文章的作者和题目
    private void setDisplayheader() {
        Picasso.with(BaseApplication.getApplication()).load(newsInfo.getImage()).into(iv_news);
        tv_title.setText(newsInfo.getTitle());
        tv_autor.setText(newsInfo.getImage_source());
    }
    
    //显示webView
    private void setWebViewDiplay() {
        String css = "<link rel=\"stylesheet\" href=\"file:///android_asset/css/news.css\" type=\"text/css\">";
        String html = "<html><head>" + css + "</head><body>" + newsInfo.getBody() + "</body></html>";
        html = html.replace("<div class=\"img-place-holder\">", "");
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
        newsScrollView.setOnScrollListener(this);
    }
    
    //滑动页面时,设置顶端bar的透明度
    @Override
    public void onScroll(int scrollY) {
        float barAlpha = iv_news.getHeight() - scrollY;
        if (barAlpha <= 0) {
            barAlpha = 0;
            rrBar.setVisibility(View.GONE);
            rrBar.setAlpha(barAlpha);
        } else {
            barAlpha = barAlpha / iv_news.getHeight();
            rrBar.setAlpha(barAlpha);
            rrBar.setVisibility(View.VISIBLE);
        }
    }
    
    public void pressBackBtn(View view) {
        finish();
    }
    
    //分享
    public void pressShareBtn(View view) {
        UiUtils.showToast("分享待实现");
    }
    
    //收藏
    public void pressCollectedBtn(View view) {
        UiUtils.showToast("收藏待实现");
    }
    
    //评论
    public void pressCommentBtn(View view) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("id", id);
        bundle.putInt("comments", barInfo.getComments());
        bundle.putInt("shortComment", barInfo.getShort_comments());
        bundle.putInt("longComment", barInfo.getLong_comments());
        intent.putExtras(bundle);
        intent.setClass(this, CommentActivity.class);
        startActivity(intent);
    }
    
    //点赞
    public void pressPraiseBtn(View view) {
        UiUtils.showToast("点赞待实现");
    }
    }

  5. CommentActivity是显示评论的界面,这里顺手也写出来,同样是顶端bar+RecylerView来实现。原版知乎日报的UI效果目前我还做不出来,只能先把大体显示出来。布局文件activity_comment:
    这里写图片描述

        <?xml version="1.0" encoding="utf-8"?>
        <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <include
                android:id="@+id/commentBar"
                layout="@layout/news_comment_bar">
            </include>
            <android.support.v7.widget.RecyclerView
                android:id="@+id/long_lv"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="5dp"
                android:focusable="true">
            </android.support.v7.widget.RecyclerView>
        </LinearLayout>
        </ScrollView>

    CommentActivity文件如下:

    package com.example.seaice.zhihuribao;
    
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Canvas;
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.Gravity;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.WindowManager;
    import android.view.animation.Animation;
    import android.view.animation.ScaleAnimation;
    import android.widget.Button;
    import android.widget.PopupWindow;
    import android.widget.TextView;
    import com.example.seaice.zhihuribao.Utils.BaseApplication;
    import com.example.seaice.zhihuribao.Utils.LogUtils;
    import com.example.seaice.zhihuribao.Utils.ThreadMgr;
    import com.example.seaice.zhihuribao.Utils.UiUtils;
    import com.example.seaice.zhihuribao.adapter.CommentRecylerViewAdapter;
    import com.example.seaice.zhihuribao.bean.CommentsInfo;
    import com.example.seaice.zhihuribao.protocol.CommentProtocol;
    import butterknife.ButterKnife;
    import butterknife.InjectView;
    
    public class CommentActivity extends AppCompatActivity
        implements CommentRecylerViewAdapter.CommentRecylerViewListemer, View.OnClickListener {
    
    private String id;
    private int commentsNum;
    
    private CommentsInfo mLongCommentsInfo;
    private CommentsInfo mShortCommentInfo;
    
    private CommentRecylerViewAdapter adapter;
    private PopupWindow popMenuWin;
    
    @InjectView(R.id.tv_comments)
    TextView tv_comments;
    @InjectView(R.id.long_lv)
    RecyclerView long_lv;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);
        ButterKnife.inject(this);
    
        initData();
        initView();
    }
    
    private void initView() {
        tv_comments.setText(String.valueOf(commentsNum + "个评论"));
    }
    
    private void initData() {
        Bundle bundle = this.getIntent().getExtras();
        id = bundle.getString("id");
        commentsNum = bundle.getInt("comments");
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                CommentProtocol protocol = new CommentProtocol(id, true);
                mLongCommentsInfo = protocol.loadData();
                CommentProtocol shortProtocol = new CommentProtocol(id, false);
                mShortCommentInfo = shortProtocol.loadData();
                if (mLongCommentsInfo != null) {
                    UiUtils.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            LogUtils.e(mLongCommentsInfo.toString());
                            setLongLv();
                        }
                    });
                }
            }
        };
        ThreadMgr.getThreadPool().execute(runnable);
    }
    
    //设置recylerView
    private void setLongLv() {
        if (mLongCommentsInfo.getComments().size() > 0 || mShortCommentInfo.getComments().size() > 0) {
            adapter = new CommentRecylerViewAdapter(mLongCommentsInfo, mShortCommentInfo);
            adapter.setItemClickListener(this);
            LinearLayoutManager layoutManager = new LinearLayoutManager(BaseApplication.getApplication());
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            long_lv.setLayoutManager(layoutManager);
            long_lv.addItemDecoration(new SimpleDividerItemDecoration(BaseApplication.getApplication()));
            long_lv.setAdapter(adapter);
        }
    }
    
    //返回键
    public void pressBackBtn(View view) {
        finish();
    }
    
    //写评论
    public void writeCommentBtn(View view) {
        Intent intent = new Intent();
        intent.setClass(this, LogInActivity.class);
        startActivity(intent);
    }
    
    //comment item click listener
    @Override
    public void onItemClickListener(View v, CommentsInfo.Comment comment) {
        showPopupMenu(v);
    }
    
    //显示弹出菜单
    private void showPopupMenu(View v) {
        dismissPopupMenu();
        View contentView = View.inflate(BaseApplication.getApplication(), R.layout.popup_menu_win, null);
        int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
        int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
        ScaleAnimation sa = new ScaleAnimation(0.5f, 1.0f, 0.5f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        sa.setDuration(300);
        popMenuWin = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popMenuWin.setFocusable(true);
        popMenuWin.setOutsideTouchable(true);
        int[] location = new int[2];
        v.getLocationInWindow(location);
        popMenuWin.showAtLocation(v, Gravity.LEFT + Gravity.TOP, screenWidth/2, screenHeight / 2);
        popMenuWin.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1.0f);
            }
        });
        setBackgroundAlpha(0.5f);
        contentView.startAnimation(sa);
    
        Button btn_agree = ButterKnife.findById(contentView, R.id.pop_agree);
        Button btn_jubao = ButterKnife.findById(contentView, R.id.pop_jubao);
        Button btn_fuzhi = ButterKnife.findById(contentView, R.id.pop_fuzhi);
        Button btn_reply = ButterKnife.findById(contentView, R.id.pop_reply);
        btn_agree.setOnClickListener(this);
        btn_jubao.setOnClickListener(this);
        btn_fuzhi.setOnClickListener(this);
        btn_reply.setOnClickListener(this);
    }
    
    //取消显示框
    private void dismissPopupMenu() {
        if (popMenuWin != null && popMenuWin.isShowing()) {
            popMenuWin.dismiss();
            popMenuWin = null;
            setBackgroundAlpha(1.0f);
        }
    }
    
    //弹出框后背景变为半透明
    private void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.pop_agree:
                UiUtils.showToast("待实现");
                break;
            case R.id.pop_jubao:
                UiUtils.showToast("待实现");
                break;
            case R.id.pop_fuzhi:
                UiUtils.showToast("待实现");
                break;
            case R.id.pop_reply:
                UiUtils.showToast("待实现");
                break;
            default:break;
        }
    }
    
    //添加recyclerview的divider
    public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration {
        private Drawable mDivider;
    
        public SimpleDividerItemDecoration(Context context) {
            mDivider = context.getResources().getDrawable(R.drawable.line_divider);
        }
    
        @Override
        public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
            int left = parent.getPaddingLeft();
            int right = parent.getWidth() - parent.getPaddingRight();
    
            int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View child = parent.getChildAt(i);
                RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
                int top = child.getBottom() + params.bottomMargin;
                int bottom = top + mDivider.getIntrinsicHeight();
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        }
    }
    
    @Override
    protected void onDestroy() {
        dismissPopupMenu();
        super.onDestroy();
    }
    }

    实际上来讲,实现评论列表显示的,还得靠RecylerView的Adapter,CommentRecylerViewAdapter:

package com.example.seaice.zhihuribao.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.seaice.zhihuribao.R;
import com.example.seaice.zhihuribao.Utils.BaseApplication;
import com.example.seaice.zhihuribao.bean.CommentsInfo;
import com.squareup.picasso.Picasso;
import java.util.List;
import butterknife.ButterKnife;

/**
 * Created by seaice on 2016/9/5.
 */
public class CommentRecylerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_LONG_HEADER = 0;
    private static final int TYPE_LONG_ITEM = 1;
    private static final int TYPE_SHORT_HEADER = 2;
    private static final int TYPE_SHORT_ITEM = 3;

    private CommentsInfo mLongCommentsInfo;
    private CommentsInfo mShortCommentInfo;

    private LayoutInflater mInflate;

    private CommentRecylerViewListemer mListener;

    public CommentRecylerViewAdapter(CommentsInfo mLongCommentsInfo, CommentsInfo mShortCommentInfo) {
        this.mLongCommentsInfo = mLongCommentsInfo;
        this.mShortCommentInfo = mShortCommentInfo;
        initData();
    }

    private void initData() {
        mInflate = LayoutInflater.from(BaseApplication.getApplication());
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_LONG_HEADER) {
            View view = mInflate.inflate(R.layout.item_comment_header, parent, false);
            return new LongHeaderHolder(view);
        } else if (viewType == TYPE_SHORT_HEADER) {
            View view = mInflate.inflate(R.layout.item_comment_header, parent, false);
            return new ShortHeaderHolder(view);
        } else if (viewType == TYPE_LONG_ITEM) {
            View view = mInflate.inflate(R.layout.item_comment_view, parent, false);
            return new LongCommentsHolder(view);
        } else if (viewType == TYPE_SHORT_ITEM) {
            View view = mInflate.inflate(R.layout.item_comment_view, parent, false);
            return new ShortCommensHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof LongHeaderHolder) {
            LongHeaderHolder lvh = (LongHeaderHolder) holder;
            lvh.longHeaderText.setText(mLongCommentsInfo.getComments().size() + "条长评论");
        } else if (holder instanceof ShortHeaderHolder) {
            ShortHeaderHolder svh = (ShortHeaderHolder) holder;
            svh.shortHeaderText.setText(mShortCommentInfo.getComments().size() + "条短评论");
        } else if (holder instanceof LongCommentsHolder) {
            CommentsInfo.Comment comment = mLongCommentsInfo.getComments().get(position - 1);
            LongCommentsHolder lvh = (LongCommentsHolder) holder;
            lvh.setComtentView(comment);
        } else if (holder instanceof ShortCommensHolder) {
            CommentsInfo.Comment comment = mShortCommentInfo.getComments().get(position - mLongCommentsInfo.getComments().size() - 2);
            ShortCommensHolder lvh = (ShortCommensHolder) holder;
            lvh.setComtentView(comment);
        }
    }

    @Override
    public int getItemCount() {
        return mLongCommentsInfo.getComments().size() + mShortCommentInfo.getComments().size() + 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (isLongCommentHeader(position)) {
            return TYPE_LONG_HEADER;
        } else if (isShortCommentHeader(position)) {
            return TYPE_SHORT_HEADER;
        } else if (isLongCommentItem(position)) {
            return TYPE_LONG_ITEM;
        } else {
            return TYPE_SHORT_ITEM;
        }
    }

    //是否是短评论
    private boolean isShortCommentItem(int position) {
        List<CommentsInfo.Comment> comments = mShortCommentInfo.getComments();
        if (position > mLongCommentsInfo.getComments().size() + 1 && comments.size() > 0) {
            return true;
        }
        return false;
    }

    //是否是长评论
    private boolean isLongCommentItem(int position) {
        List<CommentsInfo.Comment> comments = mLongCommentsInfo.getComments();
        if (position > 0 && comments.size() > 0 && position <= comments.size()) {
            return true;
        }
        return false;
    }

    //是否是短评论的开头
    private boolean isShortCommentHeader(int position) {
        int count = mLongCommentsInfo.getComments().size() + 1;
        return position == count;
    }

    //是否是长评论的开头
    private boolean isLongCommentHeader(int position) {
        return position == 0;
    }

    //长评论
    public class LongCommentsHolder extends BaseCommentsHolder {
        protected LongCommentsHolder(View view) {
            super(view);
        }
    }

    //短评论
    public class ShortCommensHolder extends BaseCommentsHolder {
        protected ShortCommensHolder(View view) {
            super(view);
        }
    }

    //多少个长评论
    public class LongHeaderHolder extends RecyclerView.ViewHolder {
        public TextView longHeaderText;

        public LongHeaderHolder(View view) {
            super(view);
            longHeaderText = ButterKnife.findById(view, R.id.tv_comment_header);
        }
    }

    //多少个短评论
    public class ShortHeaderHolder extends RecyclerView.ViewHolder {
        public TextView shortHeaderText;

        public ShortHeaderHolder(View view) {
            super(view);
            shortHeaderText = ButterKnife.findById(view, R.id.tv_comment_header);
        }
    }

    public class BaseCommentsHolder extends RecyclerView.ViewHolder {
        public ImageView icon;
        public TextView author;
        public TextView likes;
        public TextView content;
        public TextView tvTime;
        public TextView tvExpand;

        protected BaseCommentsHolder(View view) {
            super(view);
            initView(view);
        }

        protected void initView(View view) {
            icon = ButterKnife.findById(view, R.id.iv_icon);
            author = ButterKnife.findById(view, R.id.tv_autor);
            likes = ButterKnife.findById(view, R.id.tv_likes);
            content = ButterKnife.findById(view, R.id.tv_content);
            tvTime = ButterKnife.findById(view, R.id.comment_time);
            tvExpand = ButterKnife.findById(view, R.id.tv_expand);
        }

        public void setComtentView(final CommentsInfo.Comment comment){
            Picasso.with(BaseApplication.getApplication()).load(comment.getAvatar()).into(this.icon);
            this.author.setText(comment.getAuthor());
            this.likes.setText(comment.getLikes());
            this.content.setText(comment.getContent());
            this.tvTime.setText(comment.getTime());
            this.tvExpand.setText("zhangkai");
            this.itemView.setBackgroundResource(R.drawable.ripple_bg);
            this.itemView.setClickable(true);
            this.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mListener != null){
                        mListener.onItemClickListener(v, comment);
                    }
                }
            });
        }
    }

    public interface CommentRecylerViewListemer{
        void onItemClickListener(View v, CommentsInfo.Comment comment);
    }

    public void setItemClickListener(CommentRecylerViewListemer listener){
        this.mListener = listener;
    }
}

好了,以上就基本的UI可以显示出来了,尽管部分功能没实现。现在就可以看到具体条目的新闻还有评论了。
下一篇,该实现什么功能呢,我也不知道实现哪个功能,有点倦!
不过还是敬请期待吧!!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值