欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android利用RecyclerView編寫聊天界面

 更新時間:2017年07月17日 15:10:59   作者:嗯哼  
這篇文章主要為大家詳細介紹了Android利用RecyclerView編寫聊天界面,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android RecyclerView編寫聊天界面的具體代碼,供大家參考,具體內容如下

1、待會兒會用到RecyclerView,首先在app/build.gradle(注意有兩個build.gradle,選擇app下的那個)當中添加依賴庫,如下:

dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 compile 'com.android.support:appcompat-v7:24.2.1'
 compile 'com.android.support:recyclerview-v7:24.2.1'
 testCompile 'junit:junit:4.12'
 androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
  exclude group: 'com.android.support', module: 'support-annotations'
 })
}

添加完之后記得點擊Sync Now進行同步。

2、開始編寫主界面,修改activity_main.xml中的代碼,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/activity_main"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#d8e0e8"
 >
 <android.support.v7.widget.RecyclerView
  android:id="@+id/msg_recycler_view"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="1"
  />
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
  <EditText
   android:id="@+id/input_text"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:hint="Type something here"
   android:maxLines="2"
   />
  <Button
   android:id="@+id/send"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="send"
   />
 </LinearLayout>
</LinearLayout>

RecyclerView用于顯示聊天的消息內容(因為不是內置在系統(tǒng)SDK中的,所以需要把完整的包路徑寫出來);

放置一個EditView用于輸入消息,一個Button用于發(fā)送消息。

3、定義消息的實體類,新建Msg,代碼如下:

public class Msg {
 public static final int TYPE_RECEIVED=0;
 public static final int TYPE_SENT=1;
 private String content;
 private int type;
 public Msg(String content,int type){
  this.content=content;
  this.type=type;
 }
 public String getContent(){
  return content;
 }

 public int getType(){
  return type;
 }
}

Msg只有兩個字段,content表示消息的內容,type表示消息的類型(二值可選,一個是TYPE_RECRIVED,一個是TYPE_SENT)。

4、接著編寫RecyclerView子項的布局,新建msg_item.xml,代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:padding="10dp"
 >

 <LinearLayout
  android:id="@+id/left_layout"
  android:layout_width="283dp"
  android:layout_height="106dp"
  android:layout_gravity="left"
  android:background="@drawable/zuo"
  android:weightSum="1">

  <TextView
   android:id="@+id/left_msg"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:layout_margin="10dp"
   />
 </LinearLayout>

 <LinearLayout
  android:id="@+id/right_layout"
  android:layout_width="229dp"
  android:layout_height="109dp"
  android:layout_gravity="right"
  android:background="@drawable/you"
  >
  <TextView
   android:id="@+id/right_msg"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:layout_margin="10dp"
   />
 </LinearLayout>

</LinearLayout>

收到的消息局左對齊,發(fā)出的消息居右對齊,并用相應的圖片作為背景。

5、創(chuàng)建RecyclerView的適配器類,新建MsgAdapter,代碼如下:

public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
 private List<Msg> mMsgList;
 static class ViewHolder extends RecyclerView.ViewHolder{
  LinearLayout leftLayout;
  LinearLayout rightLayout;
  TextView leftMsg;
  TextView rightMsg;
  public ViewHolder(View view){
   super(view);
   leftLayout=(LinearLayout)view.findViewById(R.id.left_layout);
   rightLayout=(LinearLayout)view.findViewById(R.id.right_layout);
   leftMsg=(TextView)view.findViewById(R.id.left_msg);
   rightMsg=(TextView)view.findViewById(R.id.right_msg);
  }
 }
 public MsgAdapter(List<Msg> msgList){
  mMsgList=msgList;
 }
 @Override
 public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){    
 //onCreateViewHolder()用于創(chuàng)建ViewHolder實例
  View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
  return new ViewHolder(view);             
 //把加載出來的布局傳到構造函數(shù)中,再返回
 }
 @Override
 public void onBindViewHolder(ViewHolder Holder,int position){      
 //onBindViewHolder()用于對RecyclerView子項的數(shù)據(jù)進行賦值,會在每個子項被滾動到屏幕內的時候執(zhí)行
  Msg msg=mMsgList.get(position);
  if(msg.getType()==Msg.TYPE_RECEIVED){           
 //增加對消息類的判斷,如果這條消息是收到的,顯示左邊布局,是發(fā)出的,顯示右邊布局
   Holder.leftLayout.setVisibility(View.VISIBLE);
   Holder.rightLayout.setVisibility(View.GONE);
   Holder.leftMsg.setText(msg.getContent());
  }else if(msg.getType()==Msg.TYPE_SENT) {
   Holder.rightLayout.setVisibility(View.VISIBLE);
   Holder.leftLayout.setVisibility(View.GONE);
   Holder.rightMsg.setText(msg.getContent());
  }
 }
 @Override
 public int getItemCount(){
  return mMsgList.size();
 }
}

6、最后修改MainActivity中的代碼,來為RecyclerView初始化一些數(shù)據(jù),并給發(fā)送按鈕加入事件響應,代碼如下:

public class MainActivity extends AppCompatActivity {
 private List<Msg> msgList=new ArrayList<>();
 private EditText inputText;
 private Button send;
 private RecyclerView msgRecyclerView;
 private MsgAdapter adapter;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  initMsgs();               //初始化消息數(shù)據(jù)
  inputText=(EditText)findViewById(R.id.input_text);
  send=(Button)findViewById(R.id.send);
  msgRecyclerView=(RecyclerView)findViewById(R.id.msg_recycler_view);

  LinearLayoutManager layoutManager=new LinearLayoutManager(this); 

  //LinearLayoutLayout即線性布局,創(chuàng)建對象后把它設置到RecyclerView當中
  msgRecyclerView.setLayoutManager(layoutManager);

  adapter=new MsgAdapter(msgList);         

  //創(chuàng)建MsgAdapter的實例并將數(shù)據(jù)傳入到MsgAdapter的構造函數(shù)中
  msgRecyclerView.setAdapter(adapter);

  send.setOnClickListener(new View.OnClickListener(){     

 //發(fā)送按鈕點擊事件
   @Override
   public void onClick(View v){
    String content=inputText.getText().toString();    

  //獲取EditText中的內容
    if(!"".equals(content)){         

  //內容不為空則創(chuàng)建一個新的Msg對象,并把它添加到msgList列表中
     Msg msg=new Msg(content,Msg.TYPE_SENT);
     msgList.add(msg);
     adapter.notifyItemInserted(msgList.size()-1);   

  //調用適配器的notifyItemInserted()用于通知列表有新的數(shù)據(jù)插入,這樣新增的一條消息才能在RecyclerView中顯示
     msgRecyclerView.scrollToPosition(msgList.size()-1); 

  //調用scrollToPosition()方法將顯示的數(shù)據(jù)定位到最后一行,以保證可以看到最后發(fā)出的一條消息
     inputText.setText("");         

  //調用EditText的setText()方法將輸入的內容清空
    }
   }
  });
 }

 private void initMsgs(){
  Msg msg1=new Msg("Hello guy.",Msg.TYPE_RECEIVED);
  msgList.add(msg1);
  Msg msg2=new Msg("Hello.Who is that?",Msg.TYPE_SENT);
  msgList.add(msg2);
  Msg msg3=new Msg("This is Tom!",Msg.TYPE_RECEIVED);
  msgList.add(msg3);
 }
}

運行程序,效果如下:

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

相關文章

  • Android實現(xiàn)帶節(jié)點的進度條

    Android實現(xiàn)帶節(jié)點的進度條

    這篇文章主要為大家詳細介紹了Android實現(xiàn)帶節(jié)點的進度條,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • android自定義ListView實現(xiàn)底部View自動隱藏和消失的功能

    android自定義ListView實現(xiàn)底部View自動隱藏和消失的功能

    本篇文章主要介紹了android自定義ListView實現(xiàn)底部View自動隱藏和消失的功能 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • Android繪制跟隨手指移動的小球

    Android繪制跟隨手指移動的小球

    這篇文章主要為大家詳細介紹了Android繪制跟隨手指移動的小球,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • 仿餓了嗎點餐界面兩個ListView聯(lián)動效果

    仿餓了嗎點餐界面兩個ListView聯(lián)動效果

    這篇文章主要介紹了仿餓了點餐界面2個ListView聯(lián)動效果的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • 全面解析Android之ANR日志

    全面解析Android之ANR日志

    不論從事安卓應用開發(fā),還是安卓系統(tǒng)研發(fā),應該都遇到應用無響應(簡稱ANR)問題,當應用程序一段時間無法及時響應,則會彈出ANR對話框,讓用戶選擇繼續(xù)等待,還是強制關閉。本文將帶你全面解析Android之ANR日志
    2021-06-06
  • Android利用貝塞爾曲線繪制動畫的示例代碼

    Android利用貝塞爾曲線繪制動畫的示例代碼

    本篇就借由動畫驅動貝塞爾曲線繪制看看動起來的貝塞爾曲線什么效果。文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2022-05-05
  • Android亮屏速度分析總結

    Android亮屏速度分析總結

    今天小編就為大家分享一篇關于Android亮屏速度分析總結,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • Android中Rxjava實現(xiàn)三級緩存的兩種方式

    Android中Rxjava實現(xiàn)三級緩存的兩種方式

    這篇文章主要介紹了Android中Rxjava實現(xiàn)三級緩存的兩種方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-04-04
  • android繪制多個黑豎線條

    android繪制多個黑豎線條

    這篇文章主要為大家詳細介紹了android繪制多個黑豎線條,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • 淺談Android LruCache的緩存策略

    淺談Android LruCache的緩存策略

    這篇文章主要介紹了淺談Android LruCache的緩存策略,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04

最新評論