CommentActivity.java 3.27 KB
package com.cnlive.im.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cnlive.im.R;
import com.cnlive.im.adapter.CommentAdapter;
import com.cnlive.libs.util.chat.model.ErrorMessage;
import com.cnlive.libs.util.comment.CommentUtil;
import com.cnlive.libs.util.comment.base.CommentCallback;
import com.cnlive.libs.util.comment.base.CommentListCallback;
import com.cnlive.libs.util.model.CommentItem;
import com.cnlive.libs.util.model.CommentPage;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 陈硕
 * @time 2017/6/9  9:28
 * @desc ${TODD}
 */
public class CommentActivity extends AppCompatActivity implements View.OnClickListener {


    private Button btn_sent;
    private EditText edit_comment;
    private RecyclerView comment_receycel;
    private CommentUtil commentUtil;
    private List<CommentItem> items = new ArrayList<>();
    private CommentAdapter commentAdapter;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);
        commentAdapter = new CommentAdapter(CommentActivity.this);
        initUI();


    }

    private void initUI() {
        recyclerView = (RecyclerView) findViewById(R.id.comment_content);
        recyclerView.setLayoutManager(new LinearLayoutManager(CommentActivity.this));
        btn_sent = (Button) findViewById(R.id.sendBtn_comment);
        edit_comment = (EditText) findViewById(R.id.et_search);
        btn_sent.setOnClickListener(this);
        intiDate();

    }

    private void intiDate() {
        if (commentUtil == null) {
            commentUtil = new CommentUtil();
        }
        getCommentMesaage();
    }


    @Override
    public void onClick(View v) {
        final String text = edit_comment.getText().toString();
        if (TextUtils.isEmpty(text)) {
            Toast.makeText(CommentActivity.this, "输入内容不能为空", Toast.LENGTH_SHORT).show();
        } else {
            commentUtil.insertComment("123", "12344", text, new CommentCallback() {
                @Override
                public void onResponse(ErrorMessage errorMessage) {
                    Log.v("errorMessage", errorMessage.getErrorMessage());
                    edit_comment.setText("");
                    commentAdapter.clearItems();
                    getCommentMesaage();
                }
            });
        }
    }


    public void getCommentMesaage() {
        commentUtil.readAllComment("123", 1, 10, new CommentListCallback() {
            @Override
            public void onResponse(CommentPage commentPage) {
                if (commentPage.getData() != null) {

                    List<CommentItem> comments = commentPage.getData().getComments();

                    recyclerView.setAdapter(commentAdapter);
                    commentAdapter.addItems(comments);
                    Log.v("comments", commentPage.toString());


                }
            }
        });
    }
}