溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android ListView怎么實(shí)現(xiàn)微信聊天界面

發(fā)布時(shí)間:2022-03-30 10:56:32 來源:億速云 閱讀:901 作者:iii 欄目:移動開發(fā)

這篇文章主要介紹“Android ListView怎么實(shí)現(xiàn)微信聊天界面”,在日常操作中,相信很多人在Android ListView怎么實(shí)現(xiàn)微信聊天界面問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Android ListView怎么實(shí)現(xiàn)微信聊天界面”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

效果圖如下

Android ListView怎么實(shí)現(xiàn)微信聊天界面

1.首先頁面總布局(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.為ListView定制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單個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);
 }
}

到此,關(guān)于“Android ListView怎么實(shí)現(xiàn)微信聊天界面”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI