AndroidListView怎么实现微信聊天界面
这篇文章主要介绍“Android ListView怎么实现微信聊天界面”,在日常操作中,相信很多人在Android ListView怎么实现微信聊天界面问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Android ListView怎么实现微信聊天界面”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
成都创新互联是一家专业提供龙游企业网站建设,专注与做网站、成都做网站、H5技术、小程序制作等业务。10年已为龙游众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。
效果图如下
1.首先页面总布局(ListView + LinearLayout(TextView+Button))
2.为ListView定制Adapter
public class MsgAdapter extends ArrayAdapter{ private int resourceID; public MsgAdapter(Context context, int resource, List 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布局
4.ListView加载Adapter
public class MainActivity extends Activity { private ListView listView; private MsgAdapter msgAdapter; private ListmsgList = new ArrayList (); 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();//当有新消息时刷新 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); } }
到此,关于“Android ListView怎么实现微信聊天界面”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!
网站题目:AndroidListView怎么实现微信聊天界面
地址分享:http://ybzwz.com/article/iijoss.html