Przeglądaj źródła

完成朋友圈自动加载更多朋友圈 和 首页朋友圈底部评论

Junqin Wang 8 lat temu
rodzic
commit
47a4532a11

+ 24 - 0
app/res/layout/circle_comment_preview_item.xml

@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:padding="6dp">
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:orientation="horizontal">
+        <ImageView
+            android:id="@+id/circle_comment_preview_item_img"
+            android:layout_width="24dp"
+            android:layout_height="24dp" />
+
+        <TextView
+            android:id="@+id/circle_comment_preview_item_tv"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_marginLeft="2dp"
+            android:layout_marginStart="2dp"/>
+    </LinearLayout>
+
+</LinearLayout>

+ 5 - 7
app/res/layout/circle_item.xml

@@ -150,10 +150,10 @@
                     android:layout_height="wrap_content"
                     android:background="@color/grey_light_light"
                     android:gravity="start|center_vertical"
-                    android:padding="3dp"
+                    android:padding="4dp"
                     android:layout_marginTop="16dp"
                     android:orientation="horizontal"
-                    android:visibility="visible">
+                    android:visibility="gone">
                     <ImageView
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
@@ -171,11 +171,9 @@
                     android:layout_width="match_parent"
                     android:layout_height="wrap_content"
                     android:orientation="vertical"
-                    android:background="@color/grey_light_light">
-                    <TextView
-                        android:id="@+id/"
-                        android:layout_width="match_parent"
-                        android:layout_height="wrap_content" />
+                    android:background="@color/grey_light_light"
+                    android:padding="4dp"
+                    android:visibility="gone">
                 </LinearLayout>
             </LinearLayout>
         </LinearLayout>

+ 9 - 0
app/src/com/sheishuo/app/common/util/net/NetStatus.java

@@ -0,0 +1,9 @@
+package com.sheishuo.app.common.util.net;
+
+/**
+ * Created by KN on 2017/7/24.
+ */
+
+public class NetStatus {
+    public static boolean IS_BUSY = false;
+}

+ 71 - 9
app/src/com/sheishuo/app/main/adapter/CircleListAdapter.java

@@ -1,16 +1,18 @@
 package com.sheishuo.app.main.adapter;
 
+import android.content.Context;
 import android.support.v7.widget.CardView;
 import android.support.v7.widget.RecyclerView;
+import android.text.Html;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ImageView;
+import android.widget.LinearLayout;
 import android.widget.TextView;
 
 import com.bumptech.glide.Glide;
-import com.netease.nim.uikit.robot.parser.elements.group.LinearLayout;
 import com.netease.nimlib.sdk.NIMClient;
 import com.netease.nimlib.sdk.RequestCallback;
 import com.netease.nimlib.sdk.uinfo.UserService;
@@ -18,18 +20,19 @@ import com.netease.nimlib.sdk.uinfo.model.NimUserInfo;
 import com.sheishuo.app.R;
 import com.sheishuo.app.SheishuoApplication;
 import com.sheishuo.app.common.beans.CircleBean;
-import com.sheishuo.app.common.beans.NearbyGroupsBean;
 import com.sheishuo.app.common.util.img.ImgUtil;
+import com.sheishuo.app.common.util.net.NetStatus;
 import com.sheishuo.app.common.views.GridViewAdapter;
 import com.sheishuo.app.common.views.ImgGridView;
+import com.sheishuo.app.main.fragment.CircleOfFriendsFragment;
+
+import org.w3c.dom.Text;
 
 import java.util.ArrayList;
 import java.util.List;
 
+import static com.sheishuo.app.R.id.circle_comment_preview_item_tv;
 import static com.sheishuo.app.R.id.circle_liked_user_tv;
-import static com.sheishuo.app.R.id.holder;
-import static com.sheishuo.app.R.id.layout_scr_bottom;
-import static com.sheishuo.app.R.id.name;
 import static com.sheishuo.app.R.id.visit_num_tv;
 
 /**
@@ -40,10 +43,14 @@ public class CircleListAdapter  extends RecyclerView.Adapter<CircleListAdapter.C
 
     private List<CircleBean.DBean.ListBean> data;
     private OnCircleItemClickListener listener;
+    private Context context;
+    private CircleOfFriendsFragment circleFragment;
 
-    public CircleListAdapter(CircleBean bean){
+    public CircleListAdapter(CircleOfFriendsFragment fragment,CircleBean bean){
         data = new ArrayList<>();
         this.addAll(bean);
+        this.context = fragment.getActivity();
+        this.circleFragment = fragment;
     }
 
     @Override
@@ -158,11 +165,50 @@ public class CircleListAdapter  extends RecyclerView.Adapter<CircleListAdapter.C
 
         }
 
+        //加载评论
+        if (bean.getComments_list().size() > 0){
+            holder.commentsLayout.setVisibility(View.VISIBLE);
+            for (CircleBean.DBean.ListBean.Comment comment : bean.getComments_list()){
+                View commentView = LayoutInflater.from(context).inflate(R.layout.circle_comment_preview_item,null);
+                ImageView imageView = (ImageView) commentView.findViewById(R.id.circle_comment_preview_item_img);
+                TextView textView = (TextView) commentView.findViewById(circle_comment_preview_item_tv);
+
+                String avatarUrl = NIMClient.getService(UserService.class).getUserInfo(comment.getUser_id()).getAvatar();
+                if (avatarUrl.equals("")){
+                    Glide.with(context).load(R.drawable.user_account_icon).into(imageView);
+                }else {
+                    Glide.with(context).load(avatarUrl).into(imageView);
+                }
 
 
-        //加载两条评论
+                if (comment.getReply_id().equals("0")){
+                    textView.setText(Html.fromHtml("<font color=\"#337a9b\">"
+                            + getUserName(comment.getUser_id())
+                            + "</font> "
+                            + comment.getComment()));
+                    System.out.println(comment.getComment());
+                }
 
 
+                holder.commentsLayout.addView(commentView);
+            }
+
+            TextView moreCommentsTV = new TextView(context);
+            moreCommentsTV.setText(Html.fromHtml("<font color=\"#337a9b\"> 所有"
+                    + bean.getComments()
+                    + "条评论</font>"));
+            holder.commentsLayout.addView(moreCommentsTV);
+
+        }
+
+
+        //判断是否需要加载更多评论
+        //判断标志为缓存的Holder小于两个且最底部的Item ID不为"1"时则自动加载
+        if (position > data.size() - 2 && !data.get(data.size() - 1).getId().equals("1")) {
+            Log.e("CircleListAdapter",data.get(data.size() - 1).getId());
+            circleFragment.loadCircleListMore(data.get(data.size() - 1).getId());
+        }
+
 
 
     }
@@ -172,7 +218,12 @@ public class CircleListAdapter  extends RecyclerView.Adapter<CircleListAdapter.C
         super.onViewRecycled(holder);
         //解决Glide不能使用添加了Tag的ImageView的问题
         holder.likedImg.setTag(null);
+
+        //重置所有隐藏元素
         holder.likedUserLayout.setVisibility(View.GONE);
+        holder.commentsLayout.setVisibility(View.GONE);
+        holder.commentsLayout.removeAllViews();
+
     }
 
     @Override
@@ -224,20 +275,27 @@ public class CircleListAdapter  extends RecyclerView.Adapter<CircleListAdapter.C
 
     class CircleListViewHolder extends RecyclerView.ViewHolder{
         private CardView layout;
+        private LinearLayout commentsLayout;
         private TextView usernameTV
                 ,contentTV
                 ,locationTV
                 ,dateTV
                 ,visitNumTv
                 ,likesNumTV
-                ,likedUserTV;
+                ,likedUserTV
+                ,firstCommentTV
+                ,secondCommentTV;
         private ImgGridView gridView;
-        private ImageView avatar,likedImg;
+        private ImageView avatar
+                ,likedImg
+                ,firstCommentImg
+                ,secondCommentImg;
         private View likedUserLayout;
 
         public CircleListViewHolder(View v) {
             super(v);
             layout = (CardView) v.findViewById(R.id.circle_item_cardview);
+            commentsLayout = (LinearLayout) v.findViewById(R.id.circle_two_comments_layout);
             likedUserLayout = v.findViewById(R.id.circle_liked_user_layout);
             gridView = (ImgGridView) v.findViewById(R.id.gridview_imgs);
             usernameTV = (TextView) v.findViewById(R.id.username_tv);
@@ -247,9 +305,13 @@ public class CircleListAdapter  extends RecyclerView.Adapter<CircleListAdapter.C
             visitNumTv = (TextView) v.findViewById(visit_num_tv);
             likesNumTV = (TextView) v.findViewById(R.id.likes_num_tv);
             likedUserTV = (TextView) v.findViewById(circle_liked_user_tv);
+            //firstCommentTV = (TextView) v.findViewById(R.id.circle_comment_first_tv);
+            //secondCommentTV = (TextView) v.findViewById(R.id.circle_comment_second_tv);
 
             avatar = (ImageView) v.findViewById(R.id.avatar_img);
             likedImg = (ImageView) v.findViewById(R.id.liked_img);
+            //firstCommentImg = (ImageView) v.findViewById(R.id.circle_comment_first_img);
+            //secondCommentImg = (ImageView) v.findViewById(R.id.circle_comment_second_img);
 
         }
     }

+ 8 - 2
app/src/com/sheishuo/app/main/fragment/CircleOfFriendsFragment.java

@@ -117,7 +117,7 @@ public class CircleOfFriendsFragment extends MainTabFragment{
     }
 
     /**
-     *  以下为开放给Presenter调用的通用方法
+     *  以下为开放调用的方法
      */
 
 
@@ -134,11 +134,13 @@ public class CircleOfFriendsFragment extends MainTabFragment{
 
 
     public void onCircleListLoaded(final CircleBean bean){
+
+
         handler.post(new Runnable() {
             @Override
             public void run() {
                 if (adapter == null){
-                    adapter = new CircleListAdapter(bean);
+                    adapter = new CircleListAdapter(CircleOfFriendsFragment.this,bean);
                     recyclerView.setLayoutManager(layoutManager);
                     //初始化adapter监听事件
                     adapter.setOnItemClickListener(new OnCircleItemClickListener() {
@@ -164,6 +166,10 @@ public class CircleOfFriendsFragment extends MainTabFragment{
         });
     }
 
+    public void loadCircleListMore(String lastId){
+        presenter.loadCircleList(AccountCache.getAccount().getId(),lastId);
+    }
+
 
 
 

+ 8 - 1
app/src/com/sheishuo/app/main/presenter/CirclePresenter.java

@@ -5,6 +5,7 @@ import android.os.Handler;
 import android.widget.Toast;
 
 import com.sheishuo.app.common.beans.CircleBean;
+import com.sheishuo.app.common.util.net.NetStatus;
 import com.sheishuo.app.main.fragment.CircleOfFriendsFragment;
 import com.sheishuo.app.main.model.CircleModel;
 
@@ -30,11 +31,17 @@ public class CirclePresenter {
 
     //加载列表
     public void loadCircleList(String user_id,String last_id){
-        model.getCircleList(user_id,last_id);
+        if (NetStatus.IS_BUSY){
+            fragment.showToast("正在加载中...");
+        }else {
+            NetStatus.IS_BUSY = true;
+            model.getCircleList(user_id,last_id);
+        }
     }
 
     //加载完成回调
     public void onCircleListLoaded(CircleBean bean){
+        NetStatus.IS_BUSY = false;
         fragment.onCircleListLoaded(bean);
     }