码迷,mamicode.com
首页 > 微信 > 详细

仿微信朋友圈点赞和评论功能

时间:2016-07-14 15:42:46      阅读:546      评论:0      收藏:0      [点我收藏+]

标签:

仿微信朋友圈点赞和评论功能

最近在做朋友圈的项目,所以写一个Demo,代码就是简单实现了一下功能,没有做优化,凑合看

图文排列是用的RecyclerView实现的,弹窗效果是用的自定义的PopupWindow,点赞应该是在本地请求数据库,设置一个flag,获取当前用户的id后,带着id向服务器post一个flag,评论就比较简单了,也是获取当前朋友id(或者昵称),带着内容,向服务器post
技术分享
贴代码:

package com.example.lenovo.dianzandemo;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
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.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private int[] pics;
    private String[] descs;
    public Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = getApplicationContext();
        pics = new int[]{R.mipmap.test1, R.mipmap.test5, R.mipmap.test6, R.mipmap.test7, R.mipmap.test8, R.mipmap.test9, R.mipmap.test10, R.mipmap.test12, R.mipmap.test13, R.mipmap.test14, R.mipmap.test1, R.mipmap.test5, R.mipmap.test6, R.mipmap.test7, R.mipmap.test8, R.mipmap.test9, R.mipmap.test10, R.mipmap.test12, R.mipmap.test13, R.mipmap.test14};
        descs = new String[]{
                "诗人承陈隋风流,浮靡相矜。",
                "古今诗人众矣,而杜子美为首,岂非以其流落饥寒,终身不用,而一饭未尝忘君也欤?",
                "老杜诗,凡一篇皆工拙相半,古人文章类如此。",
                "王介甫只知巧语之为诗,而不知拙语亦诗也;山谷只知奇语之为诗,而不知常语亦诗也",
                "子美诗奄有古今。学者能识国风、骚人之旨,然后知子美用意处",
                "或问王荆公云:编四家诗,以杜甫为第一,李白为第四,岂白之才格词致不逮甫也?",
                "山谷云,老杜作诗,退之作文,无一字无来处,盖后人读书少,故谓韩、杜自作此语耳。",
                "观杜者不唯见其律,而有见其骚者焉;不唯见其骚,而有见其雅者焉",
                "盛唐一味秀丽雄浑。杜则精粗、巨细、巧拙、新陈、险易、浅深、浓淡、肥瘦靡不毕具",
                "大概杜有三难:极盛难继,首创难工,遘衰难挽",
                "诗人承陈隋风流,浮靡相矜。",
                "古今诗人众矣,而杜子美为首,岂非以其流落饥寒,终身不用,而一饭未尝忘君也欤?",
                "老杜诗,凡一篇皆工拙相半,古人文章类如此。",
                "王介甫只知巧语之为诗,而不知拙语亦诗也;山谷只知奇语之为诗,而不知常语亦诗也",
                "子美诗奄有古今。学者能识国风、骚人之旨,然后知子美用意处",
                "或问王荆公云:编四家诗,以杜甫为第一,李白为第四,岂白之才格词致不逮甫也?",
                "山谷云,老杜作诗,退之作文,无一字无来处,盖后人读书少,故谓韩、杜自作此语耳。",
                "观杜者不唯见其律,而有见其骚者焉;不唯见其骚,而有见其雅者焉",
                "盛唐一味秀丽雄浑。杜则精粗、巨细、巧拙、新陈、险易、浅深、浓淡、肥瘦靡不毕具",
                "大概杜有三难:极盛难继,首创难工,遘衰难挽"
        };
        initView();
    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        RecyclerAdapter recyclerAdapter = new RecyclerAdapter(pics, descs, mContext);
        recyclerView.setAdapter(recyclerAdapter);
    }


}


class RecyclerAdapter extends RecyclerView.Adapter<MyViewHolder>  {


    private Context mContext;
    private String[] descs;
    private int[] pics;
    private PopupWindow mPop;

    public RecyclerAdapter(int[] pics, String[] descs, Context context) {
        this.pics = pics;
        this.descs = descs;
        this.mContext = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_layout, null);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.desc.setText(descs[position]);
        holder.icon.setImageResource(pics[position]);
        holder.zan.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
//                Toast.makeText(mContext, position + "被点击了", Toast.LENGTH_SHORT).show();
                int zanWidth = holder.zan.getWidth();
                int zanHeight = holder.zan.getHeight();
                View contentView = LayoutInflater.from(mContext).inflate(R.layout.pop_layout, null);
                TextView popZan = (TextView) contentView.findViewById(R.id.pop_zan);
                TextView popPinlun = (TextView) contentView.findViewById(R.id.pop_pinlun);
                popZan.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(mContext, "点赞", Toast.LENGTH_SHORT).show();
                    }
                });
                popPinlun.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(mContext, "评论", Toast.LENGTH_SHORT).show();
                    }
                });

                if (mPop == null) { //优化:当popwindow为空的时候才创建,不为空,直接复用
                    int width = zanWidth * 5; //设置PopupWindow的宽
                    int height = zanHeight; //设置PopupWindow的高
                    boolean focusable = true; //设置PopupWindow可获取焦点
                    mPop = new PopupWindow(contentView, width, height, focusable);
                    //为PopupWindow设置背景,获取焦点才能生效(谷歌官方要求)
                    mPop.setBackgroundDrawable(new ColorDrawable());
                }
                View anchor = holder.zan; //指定PopupWindow在哪个控件下面显示
                int xoff = -zanWidth;//指定PopupWindow在x轴方向上的偏移量
                int yoff = -zanHeight;//指定PopupWindow在Y轴方向上的偏移量
                mPop.showAsDropDown(anchor, xoff, yoff);
            }
        });
    }

    @Override
    public int getItemCount() {
        return pics.length;
    }


}

class MyViewHolder extends RecyclerView.ViewHolder {

    public TextView desc;
    public ImageView icon;
    public ImageView zan;

    public MyViewHolder(View itemView) {
        super(itemView);
        desc = (TextView) itemView.findViewById(R.id.desc);
        icon = (ImageView) itemView.findViewById(R.id.icon);
        zan = (ImageView) itemView.findViewById(R.id.zan);
    }
}

具体的弹窗位置和点赞按钮,评论按钮实现效果只用了吐司弹出,可以加自己的业务逻辑进去

仿微信朋友圈点赞和评论功能

标签:

原文地址:http://blog.csdn.net/vxwjf/article/details/51907957

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!