第一轮铁大树洞APP开发冲刺(6)

写在前面

今天基本上睡了一天,下午起来后开始处理队友做完的登录注册模块与后面模块的联系。最后做完了“我的”模块。在写代码的过程中遇到了很多粗心的问题,也有很多底子不稳的问题。但主要还是业务逻辑没有理清。理清业务逻辑后基本写代码就一路顺畅了。到今天我负责的后台代码已经全部写完了,我算了算,大概一共2500行...其实真的写起来也感觉不到很多,主要还是业务逻辑要理清。
今日团队冲刺博客地址:https://www.cnblogs.com/three3/p/12756092.html

实现截图

登录和注册就不再演示了。
直接演示我的模块:


实现逻辑

整体的实现逻辑和之前类似,书写API,然后对API中获取的数据进行处理即可:

package com.androidlearing.tdtreehole.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.androidlearing.tdtreehole.R;
import com.androidlearing.tdtreehole.activity.RepostActivity;
import com.androidlearing.tdtreehole.adapter.RepostForSelfAdapter;
import com.androidlearing.tdtreehole.bean.ItemSelfRepostBean;
import com.androidlearing.tdtreehole.httputils.HttpUtils;
import com.androidlearing.tdtreehole.pojo.Post;
import com.androidlearing.tdtreehole.pojo.Repost;
import com.androidlearing.tdtreehole.pojo.Reposts;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class Fragment_My_receive extends Fragment {

    private static final String TAG = "Fragment_My_receive";
    private static final int GET_POSTS_SELF = 0;
    private XRecyclerView mXRecyclerView;
    private Handler mHandler;
    private int mIdKey;
    private List<Reposts> postPastList;
    private String mUsername;
    private List<ItemSelfRepostBean> rePostList;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //取得传来的id值
        Bundle bundle = getArguments();
        mIdKey = bundle.getInt("idKey");
        Log.d(TAG,"idKey =="+ mIdKey);
        //取得数据
        getRePosts();
        //处理数据
        handleMsg();
    }
    //处理数据
    private void handleMsg() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message msg) {
                boolean isHandled = false;
                switch(msg.what){
                    case GET_POSTS_SELF:
                        if(msg.obj!=null){
                            List<Reposts> reposts_temp = (List<Reposts>) msg.obj;
                            postPastList = reposts_temp;
                            List<ItemSelfRepostBean> itemSelfRepostBeans = new ArrayList<>();
                            for(Reposts reposts:reposts_temp){
                                ItemSelfRepostBean itemSelfRepostBean = new ItemSelfRepostBean();
                                itemSelfRepostBean.setContent(reposts.getRePost().getContent());
                                itemSelfRepostBean.setPost(reposts.getPost());
                                itemSelfRepostBean.setTime(reposts.getRePost().getPublishtime());
                                itemSelfRepostBean.setTitle(reposts.getPost().getTitle());
                                Log.d(TAG,"itemSelfRepostBean title=="+itemSelfRepostBean.getTitle());
                                itemSelfRepostBeans.add(itemSelfRepostBean);
                            }
                            //判断List是否为空,若不空则清空后再添加
                            if (rePostList != null) {
                                rePostList = null;
                            }
                            rePostList = itemSelfRepostBeans;
                            showReposts();
                            isHandled = true;
                        }
                }
                return isHandled;
            }
        });
    }
    //显示数据
    private void showReposts() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false);
        mXRecyclerView.setLayoutManager(layoutManager);
        RepostForSelfAdapter adapter = new RepostForSelfAdapter(rePostList);
        //判断适配器是否为空,如果不为空则清空后再添加数据
        if(mXRecyclerView.getAdapter()!=null){
            mXRecyclerView.removeAllViews();
        }
        adapter.setOnItemClickListener(new RepostForSelfAdapter.onItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(getActivity().getApplicationContext(), RepostActivity.class);
                Post post = postPastList.get(position).getPost();
                intent.putExtra("postKey", post);
                startActivity(intent);
            }
        });
        mXRecyclerView.setAdapter(adapter);
        mXRecyclerView.setRefreshProgressStyle(ProgressStyle.LineScalePulseOutRapid);
        //设置刷新事件
        mXRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //下拉刷新事件
                getRePosts();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mXRecyclerView.refreshComplete();
                    }
                },1500);
            }

            @Override
            public void onLoadMore() {
                //上拉加载更多时间
                getRePosts();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mXRecyclerView.loadMoreComplete();
                    }
                },1500);
            }
        });
    }


    //取得数据
    private void getRePosts() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpUtils httpUtils = new HttpUtils();
                Call call = httpUtils.getCalls("http://39.97.109.245/TDTreeHole/getRepostsSelf?id="+ mIdKey);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        //失败的回调方法
                        Log.d(TAG,"onFailure... and Exception is -->"+e);
                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        //成功的回调方法:使用Gson提取json数据并转换成List格式
                        Log.d(TAG,"onResponse....");
                        Gson gson = new Gson();
                        Type type = new TypeToken<List<Reposts>>(){}.getType();
                        List<Reposts> reposts = gson.fromJson(response.body().string(), type);
                        for(Reposts reposts1:reposts){
                            Log.d(TAG,"repost =="+reposts1.getRePost()+"post ==  "+reposts1.getPost());
                        }
                        //使用Handler和message来请求主线程更新UI
                        Message message = new Message();
                        message.what = GET_POSTS_SELF;
                        message.obj = reposts;
                        mHandler.sendMessage(message);
                    }
                });
            }
        }).start();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_my_receive, container, false);
        mXRecyclerView = rootView.findViewById(R.id.recycler_view_my_receive);
        return rootView;
    }

}

这里只贴一点

总结

今天大部分时间都在理清业务逻辑。也再次提醒我自己要好好思考后再下手。

posted @ 2020-04-22 23:08  武神酱丶  阅读(228)  评论(0编辑  收藏  举报