Android仿微信朋友圈点赞和评论功能-创新互联
最近在做朋友圈的项目,所以写一个Android仿朋友圈点赞和评论功能Demo,代码就是简单实现了一下功能,没有做优化,凑合看。
创新互联建站是一家专注网站建设、网络营销策划、成都小程序开发、电子商务建设、网络推广、移动互联开发、研究、服务为一体的技术型公司。公司成立10余年以来,已经为1000多家楼梯护栏各业的企业公司提供互联网服务。现在,服务的1000多家客户与我们一路同行,见证我们的成长;未来,我们一起分享成功的喜悦。图文排列是用的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{ 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); } }
文章标题:Android仿微信朋友圈点赞和评论功能-创新互联
网页URL:http://azwzsj.com/article/dgshgc.html