The first round of Tiedashudong APP development sprint (4)

Write in front

Today, the functions of reading and replying to posts are mainly realized. This function was conceived at more than ten o'clock last night. As a result, the more you write, the more unclear the idea. Many types of adapters can't write well. I couldn't hold it until twelve o'clock, so I simply went to sleep. After reviewing the ideas on the bed, I finished the function in less than half an hour after I got up in the morning to finish the class. This also tells me that when writing code, you must think about it first, especially when doing projects. It is very likely that changing a function will affect the overall framework. Now our project code volume is also two or three thousand, and the modules I am responsible for writing are less than one thousand (mostly the logic layer). If I can't figure it out myself, it's easy to get bugs. Therefore, on the basis of drawing lessons, continue to do the reply function in the afternoon. Writing code is very smooth, but in the end there is an unknown bug. In the end, it was also solved for unknown reasons. Today's team sprint blog:
https://www.cnblogs.com/three3/p/12740291.html Today is the fourth day of the team sprint. Take a break tomorrow and sort out the overall logical thinking. In case there is a fatal bug in the end.

Function screenshot

Today is mainly to realize the post and reply post, as shown

in the figure: there is a comment function at the bottom of the comment area, after entering the content, click reply to reply to the post.

Post logic implementation logic

The first is to read the post function. A multi-entry RecyclerView is used here. The implementation logic is similar to that of ordinary RecyclerView. We first define a class to store the content of each entry:

public class PostAndRepost {
    private int type;
    private ItemBean post;
    private ItemBeanRepost reposts;
}

The code of ItemBeanRepost is as follows:

public class ItemBeanRepost {
    private String name;
    private String content;
    private String posttime;
}

After the most critical, we have to set the adapter:

package com.androidlearing.tdtreehole.adapter;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.androidlearing.tdtreehole.R;
import com.androidlearing.tdtreehole.pojo.Post;
import com.androidlearing.tdtreehole.pojo.PostAndRepost;
import com.androidlearing.tdtreehole.pojo.Repost;

import org.w3c.dom.Text;

import java.util.List;

/**
 * @ProjectName: TDTreeHole
 * @Package: com.androidlearing.tdtreehole.adapter
 * @ClassName: PostAndRepostAdapter
 * @Description: 看帖模块的适配器
 * @Author: 武神酱丶
 * @CreateDate: 2020/4/19 23:02
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/4/19 23:02
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class PostAndRepostAdapter extends RecyclerView.Adapter {
    private static final String TAG = "PostAndRepostAdapter";
    //定义两个List变量
    private final List<PostAndRepost> mData;
    //定义两个常量标识,因为有两种类型
    public static final int TYPE_POST_CONTENT = 0;
    public static final int TYPE_REPOST_CONTENT = 1;
    //使用构造方法获得数据
    public PostAndRepostAdapter(List<PostAndRepost> data){
        this.mData = data;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //这里去创建ViewHolder
        View view;
        if(viewType == TYPE_POST_CONTENT){
            view = View.inflate(parent.getContext(), R.layout.item_repost_postcontent,null);
            return new PostContentViewHolder(view);
        }else{
            view = View.inflate(parent.getContext(),R.layout.item_repost_repostcontent,null);
            return new RepostContentViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        //根据条目类型绑定数据
        if(holder instanceof PostContentViewHolder){
            PostContentViewHolder postContentViewHolder = (PostContentViewHolder) holder;
            postContentViewHolder.mUsername.setText(mData.get(position).getPost().getUsername());
            postContentViewHolder.mPostContent.setText(mData.get(position).getPost().getContent());
            postContentViewHolder.mPostTime.setText(mData.get(position).getPost().getPosttime());
        }else if(holder instanceof RepostContentViewHolder){
            RepostContentViewHolder viewHolder = (RepostContentViewHolder) holder;
            viewHolder.mRepostUsername.setText(mData.get(position).getReposts().getName());
            viewHolder.mRepostTime.setText(mData.get(position).getReposts().getPosttime());
            viewHolder.mRepostContent.setText(mData.get(position).getReposts().getContent());

        }
    }
    //这里返回条目数量
    @Override
    public int getItemCount() {
        return mData.size();
    }
    //复写该方法来根据条件取得条目种类
    @Override
    public int getItemViewType(int position) {
        PostAndRepost postAndRepost = mData.get(position);
        if(postAndRepost.getType() ==TYPE_POST_CONTENT){
            return TYPE_POST_CONTENT;
        }else{
            return TYPE_REPOST_CONTENT;
        }
    }

    private class PostContentViewHolder extends RecyclerView.ViewHolder {

        private final TextView mUsername;
        private final TextView mPostContent;
        private final TextView mPostTime;

        public PostContentViewHolder(View view) {
            super(view);
            //找到控件
            mUsername = view.findViewById(R.id.user_name_tv);
            mPostTime = view.findViewById(R.id.post_time_tv);
            mPostContent = view.findViewById(R.id.post_content_tv);
        }
    }

    private class RepostContentViewHolder extends RecyclerView.ViewHolder {


        private final TextView mRepostUsername;
        private final TextView mRepostTime;
        private final TextView mRepostContent;

        public RepostContentViewHolder(View view) {
            super(view);
            //找到控件
            mRepostUsername = view.findViewById(R.id.repost_user_name_tv);
            mRepostTime = view.findViewById(R.id.reposts_content_time_tv);
            mRepostContent = view.findViewById(R.id.reposts_content_tv);
        }
    }

}

The key here is to overwrite the GetItemType method to get our own item type, and then set the view and InnerHolder according to different item types.
After setting up the adapter, the rest is simple. We pass the ID and other information of the post through the Intent to the post activity, and then read the reply information corresponding to the post in it, and finally display all the posts.

Implementation logic of reply function

To achieve the reply function, we must first have a layout layout. After checking the information here, I found that it can be achieved by adding a footview to RecyclerView. But native controls do not include this feature. Therefore, the xRecyclerView that encapsulates this function is used for implementation. It also includes the encapsulation of pull-down loading, pull-up refresh and so on. After adding the layout, we can implement the click event (here I encountered a very bloody BUG, ​​my button click event could not be triggered. I was annoyed for N for a long time, and finally realized it by changing a way, but EditText couldn't Was monitored. After showing the code to the teammates, the problem was solved temporarily. Here may be the problem of overlapping focus.). The specific code is as follows:

    public void TestRepost(View view){
        Log.d(TAG,"onClick...");
        mContent = mRepostContent.getText().toString();
        int postid = mPostForSelf.getPostid();
        Log.d(TAG,"content =="+mContent);
        //设置日期格式
        DateFormat dateFormat = DateFormat.getDateInstance(2);
        //获取当前时间
        String publishtime =dateFormat.format(new Date());
        //简单的判空处理
        if (TextUtils.isEmpty(mContent)) {
            Toast.makeText(RepostActivity.this,"请输入回复内容!",Toast.LENGTH_SHORT).show();
            return;
        }
        final Repost repost = new Repost();
        repost.setPostid(postid);
        repost.setContent(mContent);
        repost.setPublishtime(publishtime);
        //暂时使用测试数据,等登录注册模块做好后Intent传值过来
        repost.setUserid(1);
        //去插入数据
        insertRepost(repost);
    }

The code for fetching data and inserting data is no longer put, otherwise the blog is too long.

to sum up

I learned a lot from writing code this time. For example, you must sort your thoughts before writing the code. For example, do n’t try to solve it all the time when you encounter a bug. A timely break is also necessary. In short, sprint learned a lot. Come on Ollie!

Guess you like

Origin www.cnblogs.com/wushenjiang/p/12741016.html