当前位置: 移动技术网 > 移动技术>移动开发>Android > Android评论功能的实现过程

Android评论功能的实现过程

2019年07月24日  | 移动技术网移动技术  | 我要评论

目前,各种app的社区或者用户晒照片、发说说的地方,都提供了评论功能,为了更好地学习,自己把这个功能实现了一下,做了个小的demo。

首先推荐一款实用的插件layoutcreater,可以帮助开发者自动生成布局代码,具体用法可以去gihub上看看:

github地址:https://github.com/boredream/boreplugin

1、新建一个android工程,写mainactivity的布局 activity_main.xml

<relativelayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@color/grey">

 <listview
  android:id="@+id/comment_list"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_margintop="10dp"
  android:layout_marginbottom="50dp" />

 <linearlayout
  android:id="@+id/rl_enroll"
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:orientation="horizontal"
  android:layout_alignparentbottom="true"
  android:background="@color/white">

  <imageview
   android:id="@+id/comment"
   android:layout_width="32dp"
   android:layout_height="32dp"
   android:src="@drawable/comment"
   android:layout_weight="1"
   android:layout_gravity="center" />

  <imageview
   android:id="@+id/chat"
   android:layout_width="23dp"
   android:layout_height="23dp"
   android:src="@drawable/chat"
   android:layout_weight="1"
   android:layout_gravity="center"/>
 </linearlayout>

 <relativelayout
  android:id="@+id/rl_comment"
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:background="@color/white"
  android:visibility="gone"
  android:layout_alignparentbottom="true">

  <view
   android:layout_width="match_parent"
   android:layout_height="1dp"
   android:background="@color/grey" />

  <textview
   android:id="@+id/hide_down"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/hide_down"
   android:textsize="13sp"
   android:textcolor="@color/txtgrey"
   android:drawablebottom="@drawable/hide_dowm"
   android:layout_alignparentleft="true"
   android:layout_centervertical="true"
   android:layout_marginleft="10dp"/>
  <view
   android:layout_width="1dp"
   android:layout_height="match_parent"
   android:background="@color/grey"
   android:layout_torightof="@id/hide_down"
   android:layout_marginleft="10dp"/>
  <edittext
   android:id="@+id/comment_content"
   android:hint="@string/comment_content"
   android:textsize="15sp"
   android:singleline="true"
   android:layout_width="240dp"
   android:layout_height="match_parent"
   android:background="@null"
   android:layout_torightof="@id/hide_down"
   android:layout_marginleft="20dp"/>

  <button
   android:id="@+id/comment_send"
   android:layout_width="50dp"
   android:layout_height="35dp"
   android:layout_margin="5dp"
   android:text="@string/send"
   android:textsize="13sp"
   android:textcolor="@color/white"
   android:background="@color/maincolor"
   android:layout_alignparentright="true"
   android:layout_marginright="10dp"
   android:layout_marginleft="15dp"/>
 </relativelayout>
</relativelayout>

2、创建评论内容实体类、 内容适配器、内容的item布局

1)内容实体类 comment

public class comment {

 string name; //评论者
 string content; //评论内容

 public comment(){
  
 }

 public comment(string name, string content){
  this.name = name;
  this.content = content;
 }

 public string getname() {
  return name;
 }

 public void setname(string name) {
  this.name = name;
 }

 public string getcontent() {
  return content;
 }

 public void setcontent(string content) {
  this.content = content;
 }
}

2)内容适配器 adaptercomment

public class adaptercomment extends baseadapter {

 context context;
 list<comment> data;

 public adaptercomment(context c, list<comment> data){
  this.context = c;
  this.data = data;
 }

 @override
 public int getcount() {
  return data.size();
 }

 @override
 public object getitem(int i) {
  return data.get(i);
 }

 @override
 public long getitemid(int i) {
  return i;
 }

 @override
 public view getview(int i, view convertview, viewgroup viewgroup) {
  viewholder holder;
  // 重用convertview
  if(convertview == null){
   holder = new viewholder();
   convertview = layoutinflater.from(context).inflate(r.layout.item_comment, null);
   holder.comment_name = (textview) convertview.findviewbyid(r.id.comment_name);
   holder.comment_content = (textview) convertview.findviewbyid(r.id.comment_content);

   convertview.settag(holder);
  }else{
   holder = (viewholder) convertview.gettag();
  }
  // 适配数据
  holder.comment_name.settext(data.get(i).getname());
  holder.comment_content.settext(data.get(i).getcontent());

  return convertview;
 }

 /**
  * 添加一条评论,刷新列表
  * @param comment
  */
 public void addcomment(comment comment){
  data.add(comment);
  notifydatasetchanged();
 }

 /**
  * 静态类,便于gc回收
  */
 public static class viewholder{
  textview comment_name;
  textview comment_content;
 }
}

3)内容的item布局 item_comment.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="horizontal"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <textview
  android:id="@+id/comment_name"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textcolor="@color/maincolor"
  android:textsize="15sp"
  android:layout_marginleft="15dp"
  android:layout_marginright="3dp"/>

 <textview
  android:id="@+id/comment_content"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textcolor="@color/coloraccent"
  android:textsize="15sp" />

</linearlayout>


3、在mainactivity选中布局,然后菜单栏点击 code —> layoutcreater,确定要生成的布局代码后,点击confirm完成


接下来再完善,具体的实现我已经在代码中做了注释,就不具体说了

public class mainactivity extends activity implements view.onclicklistener {

 private imageview comment;
 private textview hide_down;
 private edittext comment_content;
 private button comment_send;

 private linearlayout rl_enroll;
 private relativelayout rl_comment;

 private listview comment_list;
 private adaptercomment adaptercomment;
 private list<comment> data;

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  initview();
 }

 private void initview() {

  // 初始化评论列表
  comment_list = (listview) findviewbyid(r.id.comment_list);
  // 初始化数据
  data = new arraylist<>();
  // 初始化适配器
  adaptercomment = new adaptercomment(getapplicationcontext(), data);
  // 为评论列表设置适配器
  comment_list.setadapter(adaptercomment);

  comment = (imageview) findviewbyid(r.id.comment);
  hide_down = (textview) findviewbyid(r.id.hide_down);
  comment_content = (edittext) findviewbyid(r.id.comment_content);
  comment_send = (button) findviewbyid(r.id.comment_send);

  rl_enroll = (linearlayout) findviewbyid(r.id.rl_enroll);
  rl_comment = (relativelayout) findviewbyid(r.id.rl_comment);

  setlistener();
 }

 /**
  * 设置监听
  */
 public void setlistener(){
  comment.setonclicklistener(this);

  hide_down.setonclicklistener(this);
  comment_send.setonclicklistener(this);
 }

 @override
 public void onclick(view v) {
  switch (v.getid()) {
   case r.id.comment:
    // 弹出输入法
    inputmethodmanager imm = (inputmethodmanager) getapplicationcontext().getsystemservice(context.input_method_service);
    imm.togglesoftinput(0, inputmethodmanager.hide_not_always);
    // 显示评论框
    rl_enroll.setvisibility(view.gone);
    rl_comment.setvisibility(view.visible);
    break;
   case r.id.hide_down:
    // 隐藏评论框
    rl_enroll.setvisibility(view.visible);
    rl_comment.setvisibility(view.gone);
    // 隐藏输入法,然后暂存当前输入框的内容,方便下次使用
    inputmethodmanager im = (inputmethodmanager)getapplicationcontext().getsystemservice(context.input_method_service);
    im.hidesoftinputfromwindow(comment_content.getwindowtoken(), 0);
    break;
   case r.id.comment_send:
    sendcomment();
    break;
   default:
    break;
  }
 }

 /**
  * 发送评论
  */
 public void sendcomment(){
  if(comment_content.gettext().tostring().equals("")){
   toast.maketext(getapplicationcontext(), "评论不能为空!", toast.length_short).show();
  }else{
   // 生成评论数据
   comment comment = new comment();
   comment.setname("评论者"+(data.size()+1)+":");
   comment.setcontent(comment_content.gettext().tostring());
   adaptercomment.addcomment(comment);
   // 发送完,清空输入框
   comment_content.settext("");

   toast.maketext(getapplicationcontext(), "评论成功!", toast.length_short).show();
  }
 }
}

注意:

因为android 手机类型比较杂,所以有的手机中会出现底部输入框和输入法重叠,如下图,画红圈的这部分是没有的:

当出现这个问题时,可以在manifest.xml文件中,给对应的activity添加一条属性

android:windowsoftinputmode="statehidden|adjustresize"

这样,输入法就可以自动调节,显示画红圈的部分,底部输入框和输入法就不会重叠了。

4、最后的效果图如下

隐藏输入框的界面

显示输入框的界面


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网