1. 程式人生 > >ListView多條目訊息模擬

ListView多條目訊息模擬

MainActivity主方法

package com.example.mutil_item;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

  private MessageAdapter mAdapter;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView listView = findViewById(R.id.messages);
    mAdapter = new MessageAdapter(this);
    listView.setAdapter(mAdapter);

    mockData();
  }

  @SuppressLint("StaticFieldLeak") private void mockData() {
    new AsyncTask<Void, Void, List<MessageBean>>(){
      @Override protected List<MessageBean> doInBackground(Void... voids) {
        List<MessageBean> result = new ArrayList<>();

        Random random = new Random();

        for (int i = 0; i < 20; i++) {
          result.add(new MessageBean("訊息: " + i, random.nextBoolean()));
        }

        return result;
      }

      @Override protected void onPostExecute(List<MessageBean> messageBeans) {
        mAdapter.setDatas(messageBeans);
      }
    }.execute();
  }
}


Activity介面卡

package com.example.mutil_item;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

public class MessageAdapter extends BaseAdapter {
  private List<MessageBean> mDatas;
  private Context mContext;
  //總共有兩種條目:左    右
  private final int ITEM_COUNT = 2;// 0 - 1
  private final int LEFT_ITEM = 0;
  private final int RIGHT_ITEM = 1;
  //1  0
  //為什麼

  @Override public int getItemViewType(int position) {
    if (getItem(position).isRec()) {
      return LEFT_ITEM;
    } else {
      return RIGHT_ITEM;
    }
  }

  @Override public int getViewTypeCount() {
    return ITEM_COUNT;
  }

  public MessageAdapter(Context mContext) {
    this.mContext = mContext;
    mDatas = new ArrayList<>();
  }

  public void setDatas(List<MessageBean> datas) {
    this.mDatas = datas;
    notifyDataSetChanged();
  }

  @Override public int getCount() {
    return mDatas.size();
  }

  @Override public MessageBean getItem(int position) {
    return mDatas.get(position);
  }

  @Override public long getItemId(int position) {
    return position;
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    if (convertView == null) {
      //載入佈局
      convertView = LayoutInflater.from(mContext).inflate(
          getItemViewType(position) == LEFT_ITEM ? R.layout.left_item : R.layout.right_item
          , parent, false);
      viewHolder = new ViewHolder();
      viewHolder.message = convertView.findViewById(R.id.message);
      convertView.setTag(viewHolder);
    } else {
      viewHolder = (ViewHolder) convertView.getTag();
    }

    //
    viewHolder.message.setText(getItem(position).getMessage());

    return convertView;
  }

  class ViewHolder {
    private TextView message;
  }
}

activity_main.xml主佈局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    >

  <ListView
      android:id="@+id/messages"
      android:layout_width="0dp"
      android:layout_height="0dp"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      />

</android.support.constraint.ConstraintLayout>

left _item.xml傳送佈局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    >

  <TextView
      android:id="@+id/message"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      android:textSize="18sp"
      android:textColor="@android:color/black"
      />

</android.support.constraint.ConstraintLayout>

right_item.xml接收佈局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    >

  <TextView
      android:id="@+id/message"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      android:textSize="18sp"
      android:textColor="@android:color/black"
      />

</android.support.constraint.ConstraintLayout>

MessageBean工具

package com.example.mutil_item;

public class MessageBean {
  //訊息
  private String message;
  //是否接收:左 / 右
  private boolean isRec;

  public MessageBean(String message, boolean isRec) {
    this.message = message;
    this.isRec = isRec;
  }

  public String getMessage() {
    return message;
  }

  public boolean isRec() {
    return isRec;
  }
}