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

Posted xiaofengzai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了团队开发冲刺第九天(实现收藏,点赞,关注功能)相关的知识,希望对你有一定的参考价值。

技术图片

 

 技术图片

 

 技术图片

 

 技术图片

 

 

昨天的bug今天已经完成,原因是布局的问题,导致评论数据部分没有显示出来。解决的办法是:用滑动布局将照片和评论数据包裹起来,之后设置总体的布局与底部评论栏间隔开。这样就不会使评论框挡住评论的数据。

 

今天的完成:实现收藏,点赞,关注功能。

遇到的困难有,在使用handler+Thread,子线程不能直接修改UI,需要创建handler对象,然后在子线程中发消息,然后在handler中捕获所需消息,实现响应。学会这个后,接下来就好设计收藏,点赞,关注这些功能,同时在逻辑思考上要设置图标的颜色变化,在这里我学习了一个“如何判断当前的资源文件是哪一个”,第二个比较繁琐的地方就是“做web服务器的访问数据请求”,比较容易。

不得不说这个handler+Thread线程真棒

 

明天的任务:实现全局搜索(含搜索记录)

 

代码:android

package com.example.newbsh.UI.home.hometype.news.shownews;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.newbsh.HttpUtil.HttpUtil;
import com.example.newbsh.MainActivity;
import com.example.newbsh.R;
import com.example.newbsh.UI.home.hometype.news.shownews.comment.BlogComment;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.Objects;

public class ShowNewsActivity extends AppCompatActivity implements View.OnClickListener {
    TextView textViewtitle,textViewdoc,textViewdate,textViewbkauthorname;
    LinearLayout piclinearLayout,commentLayout;
    TextView msg;
    Button send;
    private ImageView support,collect,bkphoto;
    private Button attention;
    private String title,authorname;
    private String username= MainActivity.getUserName();
    private ShowBlogHandler blogHandler;
    String url;
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_news);
        blogHandler = new ShowBlogHandler(this);
        init();//初始化文章的内容
        isAttention();//判断是否关注
        isCollect();//判断是否收藏
        isSupport();//判断是否点赞
        showComment();//展示评论
    }



    private void init() {
        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(new ScrollingMovementMethod());
        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(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String comment=msg.getText().toString();
                if(comment.isEmpty()){
                    Toast.makeText(ShowNewsActivity.this, "发送内容不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                writeComment(comment);      //开启线程
            }
        });

    }


    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.attention:
                attentionAction();
                break;
            case R.id.collect:
                collectAction();
                break;
            case R.id.support:
                supportAction();
                break;
            default:
                break;
        }
    }
    private void isSupport() {    //是否点赞
        new Thread(new Runnable() {
            @Override
            public void run() {
                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 = new Message();
                msg.what = 300;
                msg.obj = isthumb;
                blogHandler.sendMessage(msg);
                Log.d("点赞", String.valueOf(isthumb));
            }
        }).start();
    }

    private void isCollect() {    //是否收藏
        new Thread(new Runnable() {
            @Override
            public void run() {
                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 = new Message();
                msg.what = 400;
                msg.obj = isStore;
                blogHandler.sendMessage(msg);
                Log.d("收藏", String.valueOf(isStore));
            }
        }).start();
    }

    private void isAttention() {   //是否关注
        new Thread(new Runnable() {
            @Override
            public void run() {
                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 = new Message();
                msg.what = 500;
                msg.obj = isAttention;
                blogHandler.sendMessage(msg);
                Log.d("关注", String.valueOf(isAttention));
            }
        }).start();
    }

    private void attentionAction() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                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 = new Message();
                msg.what = 700;
                msg.obj = flag;
                blogHandler.sendMessage(msg);
            }
        }).start();
    }

    private void collectAction() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                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 = new Message();
                msg.what = 800;
                msg.obj = flag;
                blogHandler.sendMessage(msg);
            }
        }).start();
    }

    private void supportAction() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                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 = new Message();
                msg.what = 900;
                msg.obj = flag;
                blogHandler.sendMessage(msg);
            }
        }).start();
    }

    private boolean judegIsDraw(View view) {   //判断当前资源文件
        boolean flag=false;
        switch (view.getId()) {
            case R.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;
            case R.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;
        }
        return flag;
    }
/*
写评论调用的线程
 */
    private void writeComment(String comment)
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String reqdata="http://39.97.181.86/BaiXiaoSheng/sendnewscomment?username="+username+"&comment="+comment+"&title="+title;
                String json=String.valueOf(HttpUtil.sendPost(reqdata,""));
                Message msg=new Message();
                msg.what=1000;
                msg.obj=json;
                blogHandler.sendMessage(msg);
                Log.d("writecomment",json);
            }
        }).start();
    }
 /*
 初始化展示该博客的评论内容
  */
    private void showComment() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d("data", "评论");
                String requrl="http://39.97.181.86/BaiXiaoSheng/findallcomments?title="+title;
                String json = String.valueOf(HttpUtil.sendPost(requrl, ""));
                Message msg = new Message();
                msg.what = 600;
                msg.obj = json;
                blogHandler.sendMessage(msg);
                Log.d("评论", json);
            }
        }).start();
    }

/*
加载新闻文章
 */
    private void initdoc() {
        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 = new RequestOptions()
                .error(R.drawable.error)
                .placeholder(R.drawable.loading);
        for(int i=0;i<urls.length;i++)
        {
            ImageView imageView=new ImageView(this);
            Glide.with(this)
                    .load(urls[i])
                    .apply(options)
                    .into(imageView);
            LinearLayout.LayoutParams layout = new LinearLayout.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 final ShowNewsActivity mcontext;

        ShowBlogHandler(ShowNewsActivity activity) {
            WeakReference<ShowNewsActivity> weakReference = new WeakReference<>(activity);
            mcontext = weakReference.get();
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void handleMessage(@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 = new JSONArray(msg.obj.toString());
                        Log.i("length:", ""+comments.length());
                        for (int i = 0; i < comments.length(); i++) {
                            JSONObject object = comments.getJSONObject(i);
                            BlogComment blogComment = new BlogComment(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=new JSONObject(msg.obj.toString());
                            BlogComment blogComment=new BlogComment(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;
            }
        }
    }

}

 

以上是关于团队开发冲刺第九天(实现收藏,点赞,关注功能)的主要内容,如果未能解决你的问题,请参考以下文章

团队第二次冲刺第九天

第二阶段--团队冲刺--第九天

团队开发冲刺第十天(实现页面展示评论数与点赞数)

团队冲刺第九天-KeepRunningAPP

团队冲刺第十九天

团队冲刺第九天