聊天界面

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>(){
      @Override protected List doInBackground(Void... voids) {
        List 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 messageBeans) {
        mAdapter.setDatas(messageBeans);
      }
    }.execute();
  }
}

Adapter


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 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 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;
  }
}

Bean

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;
  }
}

 

你可能感兴趣的:(聊天界面)