package com.wechart.activity;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class TalkingView extends RelativeLayout {
private Context context;
private LayoutInflater inflater = null;
private RelativeLayout talingView = null;
private Button bt_send_msg = null;
private EditText et_msg_info = null;
private ListViewAdapter lvadapter = null;
private List<String> list_message = null;
private ListView lv_info_contain = null;
public TalkingView(Context context) {
super(context);
this.context = context;
this.inflater = LayoutInflater.from(this.context);
this.list_message = new ArrayList<String>();
this.inItList();
this.inIt();
this.lvadapter = new ListViewAdapter();
this.lv_info_contain.setAdapter(this.lvadapter);
this.lv_info_contain.setSelection(list_message.size() - 1);
}
private void inItList(){
this.list_message.add("放大!");
this.list_message.add("我也想放啊>_<");
this.list_message.add("大cd早点说V_V");
this.list_message.add("。。。。");
}
private void inIt(){//初始化界面
this.talingView = (RelativeLayout) this.inflater.inflate(R.layout.message_layout,null);
this.addView(talingView);
this.bt_send_msg = (Button) talingView.findViewById(R.id.bt_send_msginfo);
this.et_msg_info = (EditText) talingView.findViewById(R.id.et_msgInfo);
this.lv_info_contain = (ListView) talingView.findViewById(R.id.lv_contain_msg);
this.bt_send_msg.setOnClickListener(new OnClickListener() {//发送消息
@Override
public void onClick(View v) {
String info = et_msg_info.getText().toString();
if(info.equals("")){
Toast.makeText(getContext(),"消息不能为空", 0).show();
return ;
}
et_msg_info.setText("");
list_message.add(info);
lvadapter.notifyDataSetChanged();//list中数据发生变化时,adapter更新数据
lv_info_contain.setSelection(list_message.size()-1);//选中最后一行
}
});
}
private class ListViewAdapter extends BaseAdapter{
@Override
public int getCount() {
if(list_message == null){
return 0;
}
return list_message.size();
}
@Override
public Object getItem(int position) {
return list_message.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = null;
if(position%2 == 0){//当position 为偶数时,为发出消息
v = inflater.inflate(R.layout.messageto, null);
TextView tv = (TextView) v.findViewById(R.id.tv_show_tomsg);
tv.setText(list_message.get(position));
}else{
v = inflater.inflate(R.layout.messageinfocom, null);
TextView tv = (TextView) v.findViewById(R.id.tv_show_comemsg);
tv.setText(list_message.get(position));
}
return v;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页