MainActivity
package com.example.adapter;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ArrayList<String> data = null;
private ArrayAdapter adapter=null;
private ListView listView;
private EditText msgEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = new ArrayList<String>();
listView = (ListView) findViewById(R.id.list);
adapter = new MyAdapter(this, -1);
listView.setAdapter(adapter);
msgEditText = (EditText) findViewById(R.id.msgEditText);
Button send = (Button) findViewById(R.id.msgSend);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage();
}
});
}
private void sendMessage() {
data.add(msgEditText.getText() + "");
adapter.notifyDataSetChanged();
// 发送后清空输入框内容
msgEditText.setText(null);
// 输入框发送消息后将ListView滚动到最底部
listView.setSelection(ListView.FOCUS_DOWN);
}
private class MyAdapter extends ArrayAdapter {
private LayoutInflater inflater = null;
public MyAdapter(Context context, int resource) {
super(context, resource);
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = inflater.inflate(R.layout.item, null);
TextView text = (TextView) convertView.findViewById(R.id.textView1);
text.setText(data.get(position));
return convertView;
}
@Override
public int getCount() {
return data.size();
}
}
}
activity_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/jjj">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/commentLinearLayout"
android:layout_alignParentTop="true"
android:divider="@android:color/transparent"
android:dividerHeight="15dip"
android:scrollbars="none" />
<LinearLayout
android:id="@+id/commentLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#e0e0e0"
android:orientation="horizontal">
<EditText
android:id="@+id/msgEditText"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="8"
android:hint="发送消息" />
<Button
android:id="@+id/msgSend"
style="?android:attr/buttonStyleSmall"
android:background="@drawable/round"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="发送" />
</LinearLayout>
</RelativeLayout>
item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/image"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/image"
android:background="@drawable/round"
android:layout_marginRight="5dip"
android:padding="10dip"
android:text="没有数据"
android:textSize="15sp" />
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/circle"
android:layout_alignParentRight="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
round
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 背景填充颜色值 -->
<solid android:color="#2196f3" />
<!-- radius值越大,越趋于圆形 -->
<corners android:radius="20dip" />
<!-- 圆角图像内部填充四周的大小 ,将会以此挤压内部布置的view -->
<padding
android:bottom="5dip"
android:left="5dip"
android:right="5dip"
android:top="5dip" />
</shape>
circle
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "oval"
android:useLevel= "false" >
<solid android:color= "#FFA500" />
</shape>