Android評論功能的實(shí)現(xiàn)過程
目前,各種App的社區(qū)或者用戶曬照片、發(fā)說說的地方,都提供了評論功能,為了更好地學(xué)習(xí),自己把這個功能實(shí)現(xiàn)了一下,做了個小的Demo。
首先推薦一款實(shí)用的插件LayoutCreater,可以幫助開發(fā)者自動生成布局代碼,具體用法可以去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、創(chuàng)建評論內(nèi)容實(shí)體類、 內(nèi)容適配器、內(nèi)容的Item布局
1)內(nèi)容實(shí)體類 Comment
public class Comment {
String name; //評論者
String content; //評論內(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;
}
/**
* 添加一條評論,刷新列表
* @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() {
// 初始化評論列表
comment_list = (ListView) findViewById(R.id.comment_list);
// 初始化數(shù)據(jù)
data = new ArrayList<>();
// 初始化適配器
adapterComment = new AdapterComment(getApplicationContext(), data);
// 為評論列表設(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);
// 顯示評論框
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);
// 隱藏輸入法,然后暫存當(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ā)送評論
*/
public void sendComment(){
if(comment_content.getText().toString().equals("")){
Toast.makeText(getApplicationContext(), "評論不能為空!", Toast.LENGTH_SHORT).show();
}else{
// 生成評論數(shù)據(jù)
Comment comment = new Comment();
comment.setName("評論者"+(data.size()+1)+":");
comment.setContent(comment_content.getText().toString());
adapterComment.addComment(comment);
// 發(fā)送完,清空輸入框
comment_content.setText("");
Toast.makeText(getApplicationContext(), "評論成功!", Toast.LENGTH_SHORT).show();
}
}
}
注意:
因?yàn)锳ndroid 手機(jī)類型比較雜,所以有的手機(jī)中會出現(xiàn)底部輸入框和輸入法重疊,如下圖,畫紅圈的這部分是沒有的:

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

顯示輸入框的界面

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)朋友圈評論回復(fù)列表
- Android 仿微信朋友圈點(diǎn)贊和評論彈出框功能
- Android實(shí)現(xiàn)評論欄隨Recyclerview滑動左右移動
- Android studio導(dǎo)入項(xiàng)目的方法詳解(簡單快速)
- Android 仿抖音的評論列表的UI和效果的實(shí)現(xiàn)代碼
- Android中使用PopupWindow 仿微信點(diǎn)贊和評論彈出
- Android模擬登錄評論CSDN實(shí)現(xiàn)代碼
- Android評論圖片可移動順序選擇器(推薦)
- Android實(shí)現(xiàn)跑馬燈效果的代碼詳解
- Android如何實(shí)現(xiàn)社交應(yīng)用中的評論與回復(fù)功能詳解
相關(guān)文章
Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽浮動效果
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽浮動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-06-06
詳解Android中使用OkHttp發(fā)送HTTP的post請求的方法
OkHttp(github.com/square/okhttp)是近來人氣迅速攀升的一款第三方安卓HTTP支持包,這里我們就來詳解Android中使用OkHttp發(fā)送HTTP的post請求的方法2016-07-07
Android AlertDialog對話框詳解及實(shí)例
這篇文章主要介紹了Android AlertDialog對話框詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-12-12
Android 7.0 運(yùn)行時權(quán)限彈窗問題的解決
這篇文章主要介紹了Android 7.0 運(yùn)行時權(quán)限彈窗問題的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Kotlin使用滾動控件RecyclerView實(shí)例教程
RecyclerView是Android一個更強(qiáng)大的控件,其不僅可以實(shí)現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。其可以實(shí)現(xiàn)數(shù)據(jù)縱向滾動,也可以實(shí)現(xiàn)橫向滾動(ListView做不到橫向滾動)。接下來講解RecyclerView的用法2022-12-12

