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

Android中使用RecylerView實(shí)現(xiàn)聊天框效果

 更新時(shí)間:2018年08月22日 14:48:38   作者:北極熊的微笑  
這篇文章主要介紹了Android中使用RecylerView實(shí)現(xiàn)聊天框效果,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

從Android 5.0開始,谷歌公司推出了一個(gè)用于大量數(shù)據(jù)展示的新控件RecylerView,可以用來(lái)代替?zhèn)鹘y(tǒng)的ListView,更加強(qiáng)大和靈活。在上篇文章給大家介紹了Android RecylerView入門教程,大家可以點(diǎn)擊查看詳情。

效果圖如下:(其中,聊天框背景圖用9-patch圖,可以內(nèi)容自適應(yīng)調(diào)節(jié)。利用AndroidStudio自帶的功能制作就行了,圖片->右鍵->create 9-patch file。

其中要注意的是:

1、將9-patch圖保存到drawable目錄下才管用。

2、要將背景圖片處理一下,縮放到足夠小,它會(huì)自動(dòng)伸縮。)

 

1、activity_main.xml的代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
  android:layout_height="match_parent"
  xmlns:android="http://schemas.android.com/apk/res/android">
  <android.support.v7.widget.RecyclerView
    android:id="@+id/Main_rView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="50dp"></android.support.v7.widget.RecyclerView>
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:padding="5dp"
    android:orientation="horizontal">
    <EditText
      android:id="@+id/Main_etContent"
      android:layout_width="0dp"
      android:layout_weight="1"
      android:layout_height="wrap_content"
      android:minLines="1"
      android:maxLines="3"
      android:hint="說(shuō)點(diǎn)什么吧"
      android:textSize="14dp"/>
    <Button
      android:id="@+id/Main_btnSend"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textSize="14dp"
      android:text="Send"
      android:textAllCaps="false"/>
  </LinearLayout>
</RelativeLayout>

2、layout_item_content.xml的代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:padding="10dp"
  xmlns:android="http://schemas.android.com/apk/res/android">
  <LinearLayout
    android:id="@+id/Layout_Item_Content_lLayoutReceive"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:layout_alignParentLeft="true"
    android:background="@drawable/bg_chat2"
    android:orientation="vertical">
    <TextView
      android:id="@+id/Layout_Item_Content_tvContentReceive"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginBottom="10dp"
      android:text="內(nèi)容內(nèi)容內(nèi)"
      android:textSize="14dp"/>
  </LinearLayout>
  <LinearLayout
    android:id="@+id/Layout_Item_Content_lLayoutSend"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:layout_alignParentRight="true"
    android:background="@drawable/bg_chat1"
    android:orientation="vertical">
    <TextView
      android:id="@+id/Layout_Item_Content_tvContentSend"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="內(nèi)容"
      android:textSize="14dp"
      android:layout_marginTop="20dp"
      android:layout_marginLeft="5dp"
      android:layout_marginRight="5dp"
      />
  </LinearLayout>
</RelativeLayout>

3、RecyclerViewAdapter.java的代碼如下:

package com.deepreality.recyclerviewdemo;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
  private Context mContext;
  private List<Tb_ChatContent> tbChatContentList;
  private Tb_ChatContent tb_chatContent;
  static class ViewHolder extends RecyclerView.ViewHolder {
    private LinearLayout lLayoutReceive, lLayoutSend;
    private TextView tvReceive, tvSend;
    public ViewHolder(View itemView) {
      super(itemView);
      lLayoutReceive = itemView.findViewById(R.id.Layout_Item_Content_lLayoutReceive);
      lLayoutSend = itemView.findViewById(R.id.Layout_Item_Content_lLayoutSend);
      tvReceive = itemView.findViewById(R.id.Layout_Item_Content_tvContentReceive);
      tvSend = itemView.findViewById(R.id.Layout_Item_Content_tvContentSend);
    }
  }
  public RecyclerViewAdapter(Context mContext, List<Tb_ChatContent> tbChatContentList) {
    this.mContext = mContext;
    this.tbChatContentList = tbChatContentList;
  }
  @NonNull
  @Override
  public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.layout_item_content, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
  }
  @Override
  public void onBindViewHolder(@NonNull RecyclerViewAdapter.ViewHolder holder, int position) {
    tb_chatContent = tbChatContentList.get(position);
    if (tb_chatContent.getType() == 0) {
      holder.lLayoutReceive.setVisibility(View.VISIBLE);
      holder.lLayoutSend.setVisibility(View.GONE);
      holder.tvReceive.setText(tb_chatContent.getContent());
    } else {
      holder.lLayoutReceive.setVisibility(View.GONE);
      holder.lLayoutSend.setVisibility(View.VISIBLE);
      holder.tvSend.setText(tb_chatContent.getContent());
    }
  }
  @Override
  public int getItemCount() {
    return tbChatContentList.size();
  }
}

4、MainActivity.java的代碼如下:

package com.deepreality.recyclerviewdemo;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  private Context mContext;
  private RecyclerView rViewChat;
  private EditText etContent;
  private Button btnSend;
  private List<Tb_ChatContent> tbChatContentList;
  private Tb_ChatContent tb_chatContent;
  private RecyclerViewAdapter recyclerViewAdapter;
  private String[] arrayContents = new String[]{"How are you", "Fine,Thank you.", "How are you"
      , "Fine,Thank you.", "How are you", "Fine,Thank you."};
  private int[] arrayTypes = new int[] {0, 1, 0, 1, 0, 1};
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    baseDataInit();
    bindViews();
    viewsAddListener();
    viewsDataInit();
  }
  private void baseDataInit() {
    mContext = this;
    tbChatContentList = new ArrayList<>();
  }
  private void bindViews() {
    rViewChat = findViewById(R.id.Main_rView);
    etContent = findViewById(R.id.Main_etContent);
    btnSend = findViewById(R.id.Main_btnSend);
  }
  private void viewsAddListener() {
    btnSend.setOnClickListener(this);
  }
  private void viewsDataInit() {
    rViewSetAdapter();
  }
  private void rViewSetAdapter() {
    for (int i = 0; i < arrayContents.length; i++) {
      tb_chatContent = new Tb_ChatContent(arrayContents[i], arrayTypes[i]);
      tbChatContentList.add(tb_chatContent);
    }
    //設(shè)置RecylerView的排列方式(線性,網(wǎng)格,瀑布流三種)
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
    rViewChat.setLayoutManager(linearLayoutManager);
    //創(chuàng)建并綁定數(shù)據(jù)適配器
    recyclerViewAdapter = new RecyclerViewAdapter(mContext, tbChatContentList);
    rViewChat.setAdapter(recyclerViewAdapter);
  }
  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.Main_btnSend: {
        tb_chatContent = new Tb_ChatContent(etContent.getText().toString(), Tb_ChatContent.TYPE_SEND);
        tbChatContentList.add(tb_chatContent);
        //數(shù)據(jù)刷新
        recyclerViewAdapter.notifyDataSetChanged();
        //滑動(dòng)到某一位置
        rViewChat.smoothScrollToPosition(tbChatContentList.size() - 1);
        break;
      }
      default:break;
    }
  }
}

總結(jié)

以上所述是小編給大家介紹的Android中使用RecylerView實(shí)現(xiàn)聊天框效果,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

您可能感興趣的文章:

相關(guān)文章

  • Android模仿美團(tuán)頂部的滑動(dòng)菜單實(shí)例代碼

    Android模仿美團(tuán)頂部的滑動(dòng)菜單實(shí)例代碼

    最近在工作遇到一個(gè)需要,要做一個(gè)滑動(dòng)菜單,實(shí)現(xiàn)的效果類似美團(tuán)頂部的滑動(dòng)菜單,所以下面這篇文章主要給大家介紹了關(guān)于Android如何模仿美團(tuán)頂部滑動(dòng)菜單的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • Android 錄制音視頻的完整代碼

    Android 錄制音視頻的完整代碼

    Android中,如果要錄制音頻的話有兩個(gè)選擇,一個(gè)是MediaRecorder,另一個(gè)就是AudioRecord,前者使用簡(jiǎn)單,后者就相對(duì)復(fù)雜點(diǎn),本文通過(guò)代碼給大家介紹Android 錄制音視頻的相關(guān)知識(shí),一起看看吧
    2021-06-06
  • Android Studio新建工程默認(rèn)在build.gradle中加入maven阿里源的問(wèn)題

    Android Studio新建工程默認(rèn)在build.gradle中加入maven阿里源的問(wèn)題

    這篇文章主要介紹了Android Studio新建工程默認(rèn)在build.gradle中加入maven阿里源的問(wèn)題,本文通過(guò)實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Android實(shí)現(xiàn)下拉菜單Spinner效果

    Android實(shí)現(xiàn)下拉菜單Spinner效果

    這篇文章主要介紹了Android實(shí)現(xiàn)下拉菜單Spinner效果,學(xué)習(xí)Spinner組件的使用方法,非常好用的一款組件,相當(dāng)于從下拉列表中選擇項(xiàng)目,感興趣的小伙伴們可以參考一下
    2016-04-04
  • Android編程實(shí)現(xiàn)圖片拍照剪裁的方法

    Android編程實(shí)現(xiàn)圖片拍照剪裁的方法

    這篇文章主要介紹了Android編程實(shí)現(xiàn)圖片拍照剪裁的方法,涉及Android調(diào)用裁剪工具操作圖片的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-12-12
  • Flutter TV Android端開發(fā)技巧詳細(xì)教程

    Flutter TV Android端開發(fā)技巧詳細(xì)教程

    這篇文章主要為大家介紹了Flutter TV Android端開發(fā)技巧詳細(xì)教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • Android實(shí)現(xiàn)語(yǔ)音識(shí)別代碼

    Android實(shí)現(xiàn)語(yǔ)音識(shí)別代碼

    語(yǔ)音識(shí)別在android上使用起來(lái)很方便也很簡(jiǎn)單.但是有個(gè)前提條件,就是android機(jī)器上必須預(yù)先安裝google的語(yǔ)音搜索工具,今天我們就來(lái)詳細(xì)探討下
    2015-06-06
  • Android手機(jī)懸浮窗口小案例

    Android手機(jī)懸浮窗口小案例

    這篇文章主要為大家詳細(xì)介紹了Android手機(jī)懸浮窗口小案例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android studio 實(shí)現(xiàn)手機(jī)掃描二維碼功能

    Android studio 實(shí)現(xiàn)手機(jī)掃描二維碼功能

    這篇文章主要介紹了Android studio 實(shí)現(xiàn)手機(jī)掃描二維碼功能,需要的朋友可以參考下
    2019-10-10
  • Android編程Widget創(chuàng)建與使用方法簡(jiǎn)明教程

    Android編程Widget創(chuàng)建與使用方法簡(jiǎn)明教程

    這篇文章主要介紹了Android編程Widget創(chuàng)建與使用方法,結(jié)合實(shí)例形式分析了Widget的功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2016-10-10

最新評(píng)論

宜昌市| 罗田县| 抚顺市| 忻州市| 津市市| 调兵山市| 马尔康县| 灌南县| 项城市| 林口县| 勃利县| 上虞市| 泗洪县| 邯郸市| 乌兰县| 芦溪县| 乌拉特中旗| 炉霍县| 自治县| 涞源县| 雅江县| 赣州市| 宜君县| 拜泉县| 宜宾县| 陈巴尔虎旗| 明水县| 宜章县| 襄城县| 大城县| 东乡县| 广丰县| 东乌| 远安县| 临夏市| 建昌县| 淅川县| 凤山市| 治县。| 灵台县| 濮阳市|