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

Android ListView仿微信聊天界面

 更新時(shí)間:2021年06月30日 14:29:03   作者:luinsist  
這篇文章主要為大家詳細(xì)介紹了ListView仿微信聊天界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Android ListView仿聊天界面效果圖的具體代碼,供大家參考,具體內(nèi)容如下

1.首先頁(yè)面總布局(ListView + LinearLayout(TextView+Button))

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     >
 
    <ListView
        android:id="@+id/msg_list_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:divider="#000000"
         />
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText 
            android:id="@+id/input_text"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:maxLines="2"/>
        <Button 
            android:id="@+id/send"
            android:text="發(fā)送"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:gravity="center"/>
    </LinearLayout>
 
</LinearLayout>

2.為L(zhǎng)istView定制Adapter

public class MsgAdapter extends ArrayAdapter<Msg>{
 
 private int resourceID;
 
 public MsgAdapter(Context context, int resource, List<Msg> objects) {
  super(context, resource, objects);
  resourceID = resource;
 }
 
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  Msg msg = getItem(position);
  View view;
  ViewHolder viewHolder;
  if(convertView == null) {
   view = LayoutInflater.from(getContext()).inflate(resourceID,  null);
   viewHolder = new ViewHolder();
   viewHolder.leftLayout = (LinearLayout)view.findViewById(R.id.left_layout);
   viewHolder.rightLayout = (LinearLayout) view.findViewById(R.id.right_layout);
   viewHolder.leftMsg = (TextView) view.findViewById(R.id.left_msg);
   viewHolder.rightMsg = (TextView) view.findViewById(R.id.right_msg);
   view.setTag(viewHolder);
  }else {
   view = convertView;
   viewHolder = (ViewHolder) view.getTag();
  }
  if(msg.getType() == Msg.MSG_RECEIVE) {
   viewHolder.leftLayout.setVisibility(View.VISIBLE);
   viewHolder.rightLayout.setVisibility(View.GONE);
   viewHolder.leftMsg.setText(msg.getMessage());
  }else {
   viewHolder.rightLayout.setVisibility(View.VISIBLE);
   viewHolder.leftLayout.setVisibility(View.GONE);
   viewHolder.rightMsg.setText(msg.getMessage());
  }
  return view;
 }
 
 class ViewHolder {
  LinearLayout leftLayout;
  
  LinearLayout rightLayout;
  
  TextView leftMsg;
  
  TextView rightMsg;
  
 }
 
}
public class Msg {
 public static final int MSG_RECEIVE = 0;
 public static final int MSG_SEND = 1;
 
 private int type;
 private String content;
 
 public Msg(String content, int type) {
  this.content = content;
  this.type = type;
 }
 
 public String getMessage() {
  return content;
 }
 public int getType() {
  return type;
 }
}

3.ListView單個(gè)view布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    
 <LinearLayout 
     android:id="@+id/left_layout"
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:layout_gravity="start"
     android:gravity="center"
     
     >
     <ImageView
         android:id="@+id/left_image"
         android:src="@drawable/yan"
         android:layout_height="wrap_content"
         android:layout_width="wrap_content" 
         />
     <LinearLayout 
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:background="@drawable/msg">
         <TextView 
         android:id="@+id/left_msg"
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         />
     </LinearLayout>    
     
 </LinearLayout>
 
 <LinearLayout 
     android:id="@+id/right_layout"
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:layout_gravity="end"
     android:gravity="center"
     >
     <LinearLayout 
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:background="@drawable/msg">
         <TextView 
         android:id="@+id/right_msg"
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         />
     </LinearLayout>
     <ImageView
         android:id="@+id/right_image"
         android:src="@drawable/meng"
         android:layout_height="wrap_content"
         android:layout_width="wrap_content" 
         />
     
 </LinearLayout>
</LinearLayout>

4.ListView加載Adapter

public class MainActivity extends Activity {
 
 private ListView listView;
 
 private MsgAdapter msgAdapter;
 
 private List<Msg> msgList = new ArrayList<Msg>();
 
 private EditText input;
 
 private Button send;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  listView = (ListView) findViewById(R.id.msg_list_view);
  initMsg();
  msgAdapter  = new MsgAdapter(this, R.layout.msg_item, msgList);
  listView.setAdapter(msgAdapter);
  
  input = (EditText) findViewById(R.id.input_text);
  send = (Button) findViewById(R.id.send);
  send.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    String message = input.getText().toString();
    if(!"".equals(message)) {
     Msg msg = new Msg(message, Msg.MSG_SEND);
     msgList.add(msg);
     msgAdapter.notifyDataSetChanged();//當(dāng)有新消息時(shí)刷新
     listView.setSelection(msgList.size());
    }else {
     Toast.makeText(MainActivity.this, "input can't be empty", Toast.LENGTH_SHORT).show();
    }
    input.setText("");
   }
  });
 }
 
 private void initMsg() {
  Msg msg;
  msg = new Msg("Hi, boy", Msg.MSG_RECEIVE);
  msgList.add(msg);
  msg = new Msg("Hi, girl", Msg.MSG_SEND);
  msgList.add(msg);
  msg = new Msg("what's up", Msg.MSG_RECEIVE);
  msgList.add(msg);
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android源碼解析onResume方法中獲取不到View寬高

    Android源碼解析onResume方法中獲取不到View寬高

    這篇文章主要為大家介紹了Android源碼解析onResume方法中獲取不到View寬高示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Android實(shí)現(xiàn)判斷某個(gè)服務(wù)是否正在運(yùn)行的方法

    Android實(shí)現(xiàn)判斷某個(gè)服務(wù)是否正在運(yùn)行的方法

    這篇文章主要介紹了Android實(shí)現(xiàn)判斷某個(gè)服務(wù)是否正在運(yùn)行的方法,涉及Android針對(duì)系統(tǒng)服務(wù)運(yùn)行狀態(tài)的判斷技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • Android編程實(shí)現(xiàn)類似于圓形ProgressBar的進(jìn)度條效果

    Android編程實(shí)現(xiàn)類似于圓形ProgressBar的進(jìn)度條效果

    這篇文章主要介紹了Android編程實(shí)現(xiàn)類似于圓形ProgressBar的進(jìn)度條效果,結(jié)合實(shí)例形式分析了Android通過(guò)自定義View實(shí)現(xiàn)圓形進(jìn)度條效果的操作方法,需要的朋友可以參考下
    2017-03-03
  • Android中WebView加載網(wǎng)頁(yè)設(shè)置進(jìn)度條

    Android中WebView加載網(wǎng)頁(yè)設(shè)置進(jìn)度條

    這篇文章主要為大家詳細(xì)介紹了Android中WebView加載網(wǎng)頁(yè)設(shè)置進(jìn)度條的相關(guān)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Android學(xué)習(xí)筆記(一)環(huán)境安裝及第一個(gè)hello world

    Android學(xué)習(xí)筆記(一)環(huán)境安裝及第一個(gè)hello world

    最近在學(xué)習(xí)安卓開發(fā),記錄下環(huán)境安裝和第一個(gè)hello world的誕生過(guò)程,希望對(duì)大家有所幫助
    2014-07-07
  • Kotlin基本數(shù)據(jù)類型詳解

    Kotlin基本數(shù)據(jù)類型詳解

    大家好,本篇文章主要講的是Kotlin基本數(shù)據(jù)類型詳解,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • Flutter 中 Dart的Mixin示例詳解

    Flutter 中 Dart的Mixin示例詳解

    這篇文章主要介紹了Flutter 中 Dart的Mixin的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Kotlin與Java相互調(diào)用的完整實(shí)例

    Kotlin與Java相互調(diào)用的完整實(shí)例

    Kotlin的設(shè)計(jì)過(guò)程中就考慮到了與Java的互操作性,在Kotlin中可以直接調(diào)用既有的Java代碼,反過(guò)來(lái)在Java中也可以很流暢地使用Kotlin代碼,這篇文章主要給大家介紹了關(guān)于Kotlin與Java相互調(diào)用的相關(guān)資料,需要的朋友可以參考下
    2021-12-12
  • Android中post和get的提交方式【三種】

    Android中post和get的提交方式【三種】

    本文主要對(duì)Android中三種POST和GET的提交方式進(jìn)行詳細(xì)介紹。通過(guò)任何一種方式可以實(shí)現(xiàn)的功能是,從安卓手機(jī)端提交數(shù)據(jù)到服務(wù)器端,服務(wù)器端進(jìn)行判斷,并返回相應(yīng)的結(jié)果。三種方式各有利弊,實(shí)現(xiàn)效果相同,在實(shí)際的使用過(guò)程中可以根據(jù)本身的需要進(jìn)行選擇。
    2016-12-12
  • Android常見(jiàn)的幾種內(nèi)存泄漏小結(jié)

    Android常見(jiàn)的幾種內(nèi)存泄漏小結(jié)

    本篇文章主要介紹了Android常見(jiàn)的幾種內(nèi)存泄漏小結(jié)。詳細(xì)的介紹了內(nèi)存泄漏的原因及影響和解決方法,有興趣的可以了解一下。
    2017-03-03

最新評(píng)論

龙井市| 特克斯县| 林周县| 什邡市| 梁河县| 北海市| 新化县| 五台县| 桑植县| 若羌县| 长子县| 宁远县| 千阳县| 牙克石市| 龙州县| 城口县| 临漳县| 胶州市| 白沙| 齐齐哈尔市| 磐安县| 宝山区| 大邑县| 都匀市| 城市| 温宿县| 江阴市| 牡丹江市| 岗巴县| 尖扎县| 红河县| 华容县| 平和县| 晋州市| 遵义县| 德安县| 静海县| 平南县| 安康市| 夏邑县| 浪卡子县|