Android評(píng)論功能的實(shí)現(xiàn)過程
目前,各種App的社區(qū)或者用戶曬照片、發(fā)說說的地方,都提供了評(píng)論功能,為了更好地學(xué)習(xí),自己把這個(gè)功能實(shí)現(xiàn)了一下,做了個(gè)小的Demo。
首先推薦一款實(shí)用的插件LayoutCreater,可以幫助開發(fā)者自動(dòng)生成布局代碼,具體用法可以去GiHub上看看:
GitHub地址:https://github.com/boredream/BorePlugin
1、新建一個(gè)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、創(chuàng)建評(píng)論內(nèi)容實(shí)體類、 內(nèi)容適配器、內(nèi)容的Item布局
1)內(nèi)容實(shí)體類 Comment
public class Comment {
String name; //評(píng)論者
String content; //評(píng)論內(nèi)容
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)內(nèi)容適配器 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();
}
// 適配數(shù)據(jù)
holder.comment_name.setText(data.get(i).getName());
holder.comment_content.setText(data.get(i).getContent());
return convertView;
}
/**
* 添加一條評(píng)論,刷新列表
* @param comment
*/
public void addComment(Comment comment){
data.add(comment);
notifyDataSetChanged();
}
/**
* 靜態(tài)類,便于GC回收
*/
public static class ViewHolder{
TextView comment_name;
TextView comment_content;
}
}
3)內(nèi)容的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選中布局,然后菜單欄點(diǎn)擊 Code —> LayoutCreater,確定要生成的布局代碼后,點(diǎn)擊confirm完成

接下來再完善,具體的實(shí)現(xiàn)我已經(jīng)在代碼中做了注釋,就不具體說了
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() {
// 初始化評(píng)論列表
comment_list = (ListView) findViewById(R.id.comment_list);
// 初始化數(shù)據(jù)
data = new ArrayList<>();
// 初始化適配器
adapterComment = new AdapterComment(getApplicationContext(), data);
// 為評(píng)論列表設(shè)置適配器
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();
}
/**
* 設(shè)置監(jiān)聽
*/
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);
// 顯示評(píng)論框
rl_enroll.setVisibility(View.GONE);
rl_comment.setVisibility(View.VISIBLE);
break;
case R.id.hide_down:
// 隱藏評(píng)論框
rl_enroll.setVisibility(View.VISIBLE);
rl_comment.setVisibility(View.GONE);
// 隱藏輸入法,然后暫存當(dāng)前輸入框的內(nèi)容,方便下次使用
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;
}
}
/**
* 發(fā)送評(píng)論
*/
public void sendComment(){
if(comment_content.getText().toString().equals("")){
Toast.makeText(getApplicationContext(), "評(píng)論不能為空!", Toast.LENGTH_SHORT).show();
}else{
// 生成評(píng)論數(shù)據(jù)
Comment comment = new Comment();
comment.setName("評(píng)論者"+(data.size()+1)+":");
comment.setContent(comment_content.getText().toString());
adapterComment.addComment(comment);
// 發(fā)送完,清空輸入框
comment_content.setText("");
Toast.makeText(getApplicationContext(), "評(píng)論成功!", Toast.LENGTH_SHORT).show();
}
}
}
注意:
因?yàn)锳ndroid 手機(jī)類型比較雜,所以有的手機(jī)中會(huì)出現(xiàn)底部輸入框和輸入法重疊,如下圖,畫紅圈的這部分是沒有的:

當(dāng)出現(xiàn)這個(gè)問題時(shí),可以在Manifest.xml文件中,給對(duì)應(yīng)的Activity添加一條屬性
android:windowSoftInputMode="stateHidden|adjustResize"
這樣,輸入法就可以自動(dòng)調(diào)節(jié),顯示畫紅圈的部分,底部輸入框和輸入法就不會(huì)重疊了。
4、最后的效果圖如下
隱藏輸入框的界面

顯示輸入框的界面

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)朋友圈評(píng)論回復(fù)列表
- Android 仿微信朋友圈點(diǎn)贊和評(píng)論彈出框功能
- Android實(shí)現(xiàn)評(píng)論欄隨Recyclerview滑動(dòng)左右移動(dòng)
- Android 仿今日頭條評(píng)論時(shí)鍵盤自動(dòng)彈出的效果(推薦)
- Android 仿抖音的評(píng)論列表的UI和效果的實(shí)現(xiàn)代碼
- Android中使用PopupWindow 仿微信點(diǎn)贊和評(píng)論彈出
- Android模擬登錄評(píng)論CSDN實(shí)現(xiàn)代碼
- Android評(píng)論圖片可移動(dòng)順序選擇器(推薦)
- Android仿微信朋友圈點(diǎn)擊評(píng)論自動(dòng)定位到相關(guān)行功能
- Android如何實(shí)現(xiàn)社交應(yīng)用中的評(píng)論與回復(fù)功能詳解
相關(guān)文章
分享一個(gè)輕量級(jí)圖片加載類 ImageLoader
這篇文章給大家分享一個(gè)輕量級(jí)圖片加載類 ImageLoader,需要的朋友可以參考下2016-08-08
從零開始使用gradle配置即可執(zhí)行的Hook庫詳解
這篇文章主要為大家介紹了從零開始使用gradle配置即可執(zhí)行的Hook庫詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android ListView 實(shí)例講解清晰易懂
這篇文章主要通過實(shí)例介紹了Android ListView,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
Android AIDL實(shí)現(xiàn)進(jìn)程間通信探索
這篇文章主要為大家詳細(xì)介紹了Android AIDL實(shí)現(xiàn)進(jìn)程間通信的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Android實(shí)現(xiàn)指定時(shí)間定時(shí)觸發(fā)方法
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)指定時(shí)間定時(shí)觸發(fā)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
android控件實(shí)現(xiàn)多張圖片漸變切換
這篇文章主要為大家詳細(xì)介紹了android控件實(shí)現(xiàn)多張圖片漸變切換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android啟動(dòng)畫面的實(shí)現(xiàn)方法
這篇文章主要介紹了Android啟動(dòng)畫面的實(shí)現(xiàn)方法,分析了布局文件及加載啟動(dòng)文件的實(shí)現(xiàn)方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-01-01
Android 開發(fā) 使用WebUploader解決安卓微信瀏覽器上傳圖片中遇到的bug
這篇文章主要介紹了Android 開發(fā) 使用WebUploader解決安卓微信瀏覽器上傳圖片中遇到的bug問題,本文給介紹的非常詳細(xì),需要的朋友可以參考下2016-11-11

