AndroidListView如何实现仿微信聊天界面
本篇内容主要讲解“Android ListView如何实现仿微信聊天界面”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android ListView如何实现仿微信聊天界面”吧!
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:国际域名空间、网络空间、营销软件、网站建设、阳原网站维护、网站推广。
效果图:
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://myzitong.com/article/posihi.html