java点赞关注功能_团队开发冲刺第九天(实现收藏,点赞,关注功能)

packagecom.example.newbsh.UI.home.hometype.news.shownews;importandroidx.annotation.NonNull;importandroidx.annotation.RequiresApi;importandroidx.appcompat.app.AppCompatActivity;importandroid.annotation.SuppressLint;importandroid.content.Context;importandroid.os.Build;importandroid.os.Bundle;importandroid.os.Handler;importandroid.os.Message;importandroid.text.method.ScrollingMovementMethod;importandroid.util.Log;importandroid.view.Gravity;importandroid.view.View;importandroid.view.inputmethod.InputMethodManager;importandroid.widget.Button;importandroid.widget.ImageView;importandroid.widget.LinearLayout;importandroid.widget.TextView;importandroid.widget.Toast;importcom.bumptech.glide.Glide;importcom.bumptech.glide.request.RequestOptions;importcom.example.newbsh.HttpUtil.HttpUtil;importcom.example.newbsh.MainActivity;importcom.example.newbsh.R;importcom.example.newbsh.UI.home.hometype.news.shownews.comment.BlogComment;importorg.json.JSONArray;importorg.json.JSONException;importorg.json.JSONObject;importjava.lang.ref.WeakReference;importjava.util.Objects;public class ShowNewsActivity extends AppCompatActivity implementsView.OnClickListener {

TextView textViewtitle,textViewdoc,textViewdate,textViewbkauthorname;

LinearLayout piclinearLayout,commentLayout;

TextView msg;

Button send;privateImageView support,collect,bkphoto;privateButton attention;privateString title,authorname;private String username=MainActivity.getUserName();privateShowBlogHandler blogHandler;

String url;

@RequiresApi(api=Build.VERSION_CODES.M)

@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

setContentView(R.layout.activity_show_news);

blogHandler= new ShowBlogHandler(this);

init();//初始化文章的内容

isAttention();//判断是否关注

isCollect();//判断是否收藏

isSupport();//判断是否点赞

showComment();//展示评论

}private voidinit() {

textViewtitle=findViewById(R.id.NewsTitle);

textViewdoc=findViewById(R.id.Newsdoc);

textViewdate=findViewById(R.id.Newsdate);

piclinearLayout=findViewById(R.id.picturelinearlayout);

commentLayout=findViewById(R.id.comment_layout);

textViewdoc.setMovementMethod(newScrollingMovementMethod());

support=findViewById(R.id.support);

collect=findViewById(R.id.collect);

attention=findViewById(R.id.attention);

msg=findViewById(R.id.msg);

send=findViewById(R.id.send);

bkphoto=findViewById(R.id.userphoto);

textViewbkauthorname=findViewById(R.id.bkname);

initdoc();

support.setOnClickListener(this);

collect.setOnClickListener(this);

attention.setOnClickListener(this);

send.setOnClickListener(newView.OnClickListener() {

@Overridepublic voidonClick(View v) {

String comment=msg.getText().toString();if(comment.isEmpty()){

Toast.makeText(ShowNewsActivity.this, "发送内容不能为空", Toast.LENGTH_SHORT).show();return;

}

writeComment(comment);//开启线程

}

});

}public voidonClick(View v) {switch(v.getId()) {caseR.id.attention:

attentionAction();break;caseR.id.collect:

collectAction();break;caseR.id.support:

supportAction();break;default:break;

}

}private void isSupport() { //是否点赞

new Thread(newRunnable() {

@Overridepublic voidrun() {

Log.d("data", "点赞");

String requrl= "http://39.97.181.86/BaiXiaoSheng/findsupport?username=" + username + "&title=" + title + "";boolean isthumb = Boolean.parseBoolean(String.valueOf(HttpUtil.sendPost(requrl, "")));

Message msg= newMessage();

msg.what= 300;

msg.obj=isthumb;

blogHandler.sendMessage(msg);

Log.d("点赞", String.valueOf(isthumb));

}

}).start();

}private void isCollect() { //是否收藏

new Thread(newRunnable() {

@Overridepublic voidrun() {

Log.d("data", "收藏");

String requrl= "http://39.97.181.86/BaiXiaoSheng/findcollect?username=" + username + "&title=" + title + "";boolean isStore = Boolean.parseBoolean(String.valueOf(HttpUtil.sendPost(requrl, "")));

Message msg= newMessage();

msg.what= 400;

msg.obj=isStore;

blogHandler.sendMessage(msg);

Log.d("收藏", String.valueOf(isStore));

}

}).start();

}private void isAttention() { //是否关注

new Thread(newRunnable() {

@Overridepublic voidrun() {

Log.d("data", "关注");

String requrl= "http://39.97.181.86/BaiXiaoSheng/findattention?username=" + username + "&authorname=" + authorname + "";boolean isAttention = Boolean.parseBoolean(String.valueOf(HttpUtil.sendPost(requrl, "")));

Message msg= newMessage();

msg.what= 500;

msg.obj=isAttention;

blogHandler.sendMessage(msg);

Log.d("关注", String.valueOf(isAttention));

}

}).start();

}private voidattentionAction() {new Thread(newRunnable() {

@Overridepublic voidrun() {

String text=attention.getText().toString();

String requrl= "http://39.97.181.86/BaiXiaoSheng/attentionaction?username=" + username + "&authorname=" + authorname +

"&type=" + text.equals("关注") + "";boolean flag = Boolean.parseBoolean(String.valueOf(HttpUtil.sendPost(requrl, "")));

Message msg= newMessage();

msg.what= 700;

msg.obj=flag;

blogHandler.sendMessage(msg);

}

}).start();

}private voidcollectAction() {new Thread(newRunnable() {

@Overridepublic voidrun() {

String requrl= "http://39.97.181.86/BaiXiaoSheng/collectaction?username=" + username + "&title=" + title + "&" +

"type=" + judegIsDraw(collect) + "";boolean flag = Boolean.parseBoolean(String.valueOf(HttpUtil.sendPost(requrl, "")));

Message msg= newMessage();

msg.what= 800;

msg.obj=flag;

blogHandler.sendMessage(msg);

}

}).start();

}private voidsupportAction() {new Thread(newRunnable() {

@Overridepublic voidrun() {

String requrl= "http://39.97.181.86/BaiXiaoSheng/supportaction?username=" + username + "&title=" + title + "&" +

"type=" + judegIsDraw(support) + "";boolean flag = Boolean.parseBoolean(String.valueOf(HttpUtil.sendPost(requrl, "")));

Message msg= newMessage();

msg.what= 900;

msg.obj=flag;

blogHandler.sendMessage(msg);

}

}).start();

}private boolean judegIsDraw(View view) { //判断当前资源文件

boolean flag=false;switch(view.getId()) {caseR.id.collect:

flag=Objects.equals(((ImageView) view).getDrawable().getCurrent().getConstantState(),

getResources().getDrawable(R.drawable.ic_turned_in_not).getConstantState());

Log.d("collect",String.valueOf(flag));break;caseR.id.support:

flag=Objects.equals(((ImageView) view).getDrawable().getCurrent().getConstantState(),

getResources().getDrawable(R.drawable.ic_thumb_not_up).getConstantState());

Log.d("support",String.valueOf(flag));break;default:break;

}returnflag;

}/*写评论调用的线程*/

private voidwriteComment(String comment)

{new Thread(newRunnable() {

@Overridepublic voidrun() {

String reqdata="http://39.97.181.86/BaiXiaoSheng/sendnewscomment?username="+username+"&comment="+comment+"&title="+title;

String json=String.valueOf(HttpUtil.sendPost(reqdata,""));

Message msg=newMessage();

msg.what=1000;

msg.obj=json;

blogHandler.sendMessage(msg);

Log.d("writecomment",json);

}

}).start();

}/*初始化展示该博客的评论内容*/

private voidshowComment() {new Thread(newRunnable() {

@Overridepublic voidrun() {

Log.d("data", "评论");

String requrl="http://39.97.181.86/BaiXiaoSheng/findallcomments?title="+title;

String json= String.valueOf(HttpUtil.sendPost(requrl, ""));

Message msg= newMessage();

msg.what= 600;

msg.obj=json;

blogHandler.sendMessage(msg);

Log.d("评论", json);

}

}).start();

}/*加载新闻文章*/

private voidinitdoc() {

Bundle bundle=getIntent().getExtras();/*获取图片的链接*/url=bundle.getString("url");

String []urls=url.split(" ");/*设置文章的内容*/

if (bundle != null) {

title=bundle.getString("title");

textViewtitle.setText(bundle.getString("title"));

textViewdoc.setText(bundle.getString("doc"));

textViewdate.setText("文章发布时间为:"+bundle.getString("date"));

textViewbkauthorname.setText(bundle.getString("authorname"));

authorname=bundle.getString("authorname");

Glide.with(this)

.load(bundle.getString("authorurl"))

.circleCrop()

.into(bkphoto);

}else{

Log.i("bundle", "为空。。。。。。");

}/*动态加载图片*/RequestOptions options= newRequestOptions()

.error(R.drawable.error)

.placeholder(R.drawable.loading);for(int i=0;i

{

ImageView imageView=new ImageView(this);

Glide.with(this)

.load(urls[i])

.apply(options)

.into(imageView);

LinearLayout.LayoutParams layout= newLinearLayout.LayoutParams(

LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

layout.setMargins(0, 10, 0, 0);

imageView.setLayoutParams(layout);

piclinearLayout.addView(imageView);

}

}static class ShowBlogHandler extends Handler { //防止内存泄漏

private finalShowNewsActivity mcontext;

ShowBlogHandler(ShowNewsActivity activity) {

WeakReference weakReference = new WeakReference<>(activity);

mcontext=weakReference.get();

}

@SuppressLint("SetTextI18n")

@Overridepublic voidhandleMessage(@NonNull Message msg) {super.handleMessage(msg);switch(msg.what) {case 300:if(Boolean.parseBoolean(msg.obj.toString()))

mcontext.support.setImageResource(R.drawable.ic_thumb_up);break;case 400:if(Boolean.parseBoolean(msg.obj.toString()))

mcontext.collect.setImageResource(R.drawable.ic_turned_in);break;case 500:if(Boolean.parseBoolean(msg.obj.toString()))

mcontext.attention.setText("已关注");break;case 600:try{

JSONArray comments= newJSONArray(msg.obj.toString());

Log.i("length:", ""+comments.length());for (int i = 0; i < comments.length(); i++) {

JSONObject object=comments.getJSONObject(i);

BlogComment blogComment= newBlogComment(mcontext);if (!object.isNull("imgurl")) {

Glide.with(mcontext)

.load(object.getString("imgurl"))

.circleCrop()

.into(blogComment.getImageView());

}

blogComment.getName().setText(object.getString("username"));

blogComment.getComment().setText(object.getString("comment"));

blogComment.getDate().setText(object.getString("date"));

Log.i("次数:", ""+object.getString("comment"));

mcontext.commentLayout.addView(blogComment,0);

}

}catch(JSONException e) {

e.printStackTrace();

}break;case 700:if(Boolean.parseBoolean(msg.obj.toString())) {

String text=mcontext.attention.getText().toString();if (text.equals("关注")) {

mcontext.attention.setText("已关注");

Toast toast= Toast.makeText(mcontext, "关注成功", Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER,0, 0);

toast.show();

}else{

mcontext.attention.setText("关注");

Toast toast= Toast.makeText(mcontext, "已取消关注", Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER,0, 0);

toast.show();

}

}else{

Toast toast= Toast.makeText(mcontext, "未知原因,操作失败", Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER,0, 0);

toast.show();

}break;case 800:if(Boolean.parseBoolean(msg.obj.toString())) {if(mcontext.judegIsDraw(mcontext.collect)) {

mcontext.collect.setImageResource(R.drawable.ic_turned_in);

Toast toast= Toast.makeText(mcontext, "收藏成功", Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER,0, 0);

toast.show();

}else{

mcontext.collect.setImageResource(R.drawable.ic_turned_in_not);

Toast toast= Toast.makeText(mcontext, "已取消收藏", Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER,0, 0);

toast.show();

}

}else{

Toast toast= Toast.makeText(mcontext, "未知原因,操作失败", Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER,0, 0);

toast.show();

}break;case 900:if(Boolean.parseBoolean(msg.obj.toString())) {if(mcontext.judegIsDraw(mcontext.support)) {

mcontext.support.setImageResource(R.drawable.ic_thumb_up);

Toast toast= Toast.makeText(mcontext, "点赞成功", Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER,0, 0);

toast.show();

}else{

mcontext.support.setImageResource(R.drawable.ic_thumb_not_up);

Toast toast= Toast.makeText(mcontext, "已取消点赞", Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER,0, 0);

toast.show();

}

}else{

Toast toast= Toast.makeText(mcontext, "未知原因,操作失败", Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER,0, 0);

toast.show();

}break;case 1000:try{

JSONObject object=newJSONObject(msg.obj.toString());

BlogComment blogComment=newBlogComment(mcontext);

blogComment.getDate().setText(object.getString("date"));

blogComment.getComment().setText(object.getString("comment"));

blogComment.getName().setText(object.getString("username"));if(!object.isNull("imgurl")){if(object.getString("imgurl").length()>0)

Glide.with(mcontext)

.load(object.getString("imgurl"))

.circleCrop()

.into(blogComment.getImageView());

}

mcontext.commentLayout.addView(blogComment,0);

mcontext.msg.setText("");

mcontext.msg.clearFocus();

InputMethodManager imm= (InputMethodManager) mcontext.getSystemService(Context.INPUT_METHOD_SERVICE); //键盘收缩

if (imm != null) {

imm.hideSoftInputFromWindow(mcontext.msg.getWindowToken(),0);

}

Toast toast= Toast.makeText(mcontext, "评论成功", Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER,0, 0);

toast.show();

}catch(JSONException e) {

e.printStackTrace();

}break;default:break;

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值