最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android評論功能的實現(xiàn)過程

 更新時間:2016年08月10日 14:18:03   作者:yypccc  
這篇文章為大家詳細介紹了Android評論功能的實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

目前,各種App的社區(qū)或者用戶曬照片、發(fā)說說的地方,都提供了評論功能,為了更好地學習,自己把這個功能實現(xiàn)了一下,做了個小的Demo。

首先推薦一款實用的插件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)容實體類、 內(nèi)容適配器、內(nèi)容的Item布局

1)內(nèi)容實體類 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選中布局,然后菜單欄點擊 Code —> LayoutCreater,確定要生成的布局代碼后,點擊confirm完成


接下來再完善,具體的實現(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);
  // 為評論列表設置適配器
  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();
 }

 /**
  * 設置監(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);
    // 隱藏輸入法,然后暫存當前輸入框的內(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();
  }
 }
}

注意:

因為Android 手機類型比較雜,所以有的手機中會出現(xiàn)底部輸入框和輸入法重疊,如下圖,畫紅圈的這部分是沒有的:

當出現(xiàn)這個問題時,可以在Manifest.xml文件中,給對應的Activity添加一條屬性

android:windowSoftInputMode="stateHidden|adjustResize"

這樣,輸入法就可以自動調節(jié),顯示畫紅圈的部分,底部輸入框和輸入法就不會重疊了。

4、最后的效果圖如下

隱藏輸入框的界面

顯示輸入框的界面


以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Android使用EventBus多次接收消息

    Android使用EventBus多次接收消息

    這篇文章主要為大家詳細介紹了Android使用EventBus多次接收消息,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Android自定義ViewGroup實現(xiàn)標簽浮動效果

    Android自定義ViewGroup實現(xiàn)標簽浮動效果

    這篇文章主要為大家詳細介紹了Android自定義ViewGroup實現(xiàn)標簽浮動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-06-06
  • 深入剖析Android消息機制原理

    深入剖析Android消息機制原理

    本篇文章主要介紹了Android消息機制,深入的了解了android的消息機制,需要的朋友可以了解一下、
    2016-11-11
  • 詳解Android中使用OkHttp發(fā)送HTTP的post請求的方法

    詳解Android中使用OkHttp發(fā)送HTTP的post請求的方法

    OkHttp(github.com/square/okhttp)是近來人氣迅速攀升的一款第三方安卓HTTP支持包,這里我們就來詳解Android中使用OkHttp發(fā)送HTTP的post請求的方法
    2016-07-07
  • Android指紋識別API初試

    Android指紋識別API初試

    這篇文章主要為大家詳細介紹了Android指紋識別API,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Android仿IOS上拉下拉彈性效果的實例代碼

    Android仿IOS上拉下拉彈性效果的實例代碼

    下面小編就為大家分享一篇Android仿IOS上拉下拉彈性效果的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • Android AlertDialog對話框詳解及實例

    Android AlertDialog對話框詳解及實例

    這篇文章主要介紹了Android AlertDialog對話框詳解及實例的相關資料,需要的朋友可以參考下
    2016-12-12
  • Android 7.0 運行時權限彈窗問題的解決

    Android 7.0 運行時權限彈窗問題的解決

    這篇文章主要介紹了Android 7.0 運行時權限彈窗問題的解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-03-03
  • 實例講解建立Android項目

    實例講解建立Android項目

    在本篇內(nèi)容中我們給大家整理了關于建立Android項目的步驟和相關知識點,有興趣的讀者們學習下。
    2019-03-03
  • Kotlin使用滾動控件RecyclerView實例教程

    Kotlin使用滾動控件RecyclerView實例教程

    RecyclerView是Android一個更強大的控件,其不僅可以實現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。其可以實現(xiàn)數(shù)據(jù)縱向滾動,也可以實現(xiàn)橫向滾動(ListView做不到橫向滾動)。接下來講解RecyclerView的用法
    2022-12-12

最新評論

邢台市| 博罗县| 宁蒗| 荥经县| 石林| 永昌县| 邛崃市| 林甸县| 遵化市| 雷山县| 大同县| 额尔古纳市| 堆龙德庆县| 连云港市| 泰宁县| 威远县| 襄汾县| 田阳县| 宜君县| 舟山市| 英吉沙县| 苍南县| 香格里拉县| 大足县| 淮北市| 大理市| 卢湾区| 邳州市| 永济市| 出国| 洛隆县| 尤溪县| 桃江县| 灵川县| 阳春市| 油尖旺区| 垦利县| 南陵县| 灵丘县| 酒泉市| 安多县|