package com.mesmerize.listviewapp;
/**
* Created by mesmerize on 2017/1/5.
*
* 数据
*/
public class CustomBean {
// 昵称
public String nick;
// 最后一条聊天信息
public String chatMessage;
// 头像
public int headImage;
// 最后聊天的时间
public String time;
public CustomBean(String nick, String chatMessage, int headImage, String time) {
this.nick = nick;
this.chatMessage = chatMessage;
this.headImage = headImage;
this.time = time;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mesmerize.listviewapp.MainActivity">
<ListView
android:id="@+id/lv_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</RelativeLayout>
package com.mesmerize.listviewapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by mesmerize on 2017/1/5.
*
* 自定义 ListView
*/
public class CustomActivity extends Activity {
private List<CustomBean> customBeanList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customBeanList = new ArrayList<>();
ListView mListView = (ListView) findViewById(R.id.lv_listview);
}
}
package com.mesmerize.listviewapp;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by mesmerize on 2017/1/5.
*/
public class CustomAdapter extends BaseAdapter {
private final CustomActivity context;
private final List<CustomBean> list;
public CustomAdapter(CustomActivity customActivity, List<CustomBean> customBeanList) {
this.context = customActivity;
this.list = customBeanList;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = View.inflate(context, R.layout.adapter_custom, null);
ImageView head = (ImageView) view.findViewById(R.id.head);
TextView nick = (TextView) view.findViewById(R.id.nick);
TextView message = (TextView) view.findViewById(R.id.message);
TextView time = (TextView) view.findViewById(R.id.time);
head.setImageResource(list.get(position).headImage);
nick.setText(list.get(position).nick);
message.setText(list.get(position).chatMessage);
time.setText(list.get(position).time);
return view;
}
}
View view = View.inflate(context, R.layout.adapter_custom, null);
<?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">
<ImageView
android:id="@+id/head"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/head_jinx"/>
<TextView
android:id="@+id/nick"
android:layout_toRightOf="@id/head"
android:textSize="18sp"
android:layout_marginLeft="9dp"
android:layout_marginTop="12dp"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="金克斯"/>
<TextView
android:id="@+id/message"
android:layout_marginLeft="7dp"
android:layout_marginTop="15dp"
android:layout_below="@id/nick"
android:layout_toRightOf="@id/head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="啊哈哈哈哈啊哈哈哈啊哈哈哈"/>
<TextView
android:id="@+id/time"
android:text="15:33"
android:layout_alignParentRight="true"
android:layout_marginTop="15dp"
android:layout_marginRight="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
ImageView head = (ImageView) view.findViewById(R.id.head);
TextView nick = (TextView) view.findViewById(R.id.nick);
TextView message = (TextView) view.findViewById(R.id.message);
TextView time = (TextView) view.findViewById(R.id.time);
head.setImageResource(list.get(position).headImage);
nick.setText(list.get(position).nick);
message.setText(list.get(position).chatMessage);
time.setText(list.get(position).time);
CustomAdapter mAdapter = new CustomAdapter(this, customBeanList);
mListView.setAdapter(mAdapter);
/**
* 初始化数据
*/
private void initCustomList(){
CustomBean vn = new CustomBean("薇恩", "走,开黑去,我带你秀起来", R.mipmap.head_vn, "20:23");
customBeanList.add(vn);
CustomBean lks = new CustomBean("拉克丝", "我刚买的大天使元素,来来来,带你上分", R.mipmap.head_lakesi, "17:33");
customBeanList.add(lks);
CustomBean ey = new CustomBean("好运", "啊哈哈,啊哈哈哈哈,哈啊啊哈哈啊", R.mipmap.head_eyun, "16:53");
customBeanList.add(ey);
CustomBean xz = new CustomBean("瞎子", "我用双手成就你的梦想,一库", R.mipmap.head_liqing, "16:15");
customBeanList.add(xz);
CustomBean ylst = new CustomBean("伊利斯坦", "我的滑板鞋时尚时尚最时尚", R.mipmap.head_ylst, "15:25");
customBeanList.add(ylst);
CustomBean irly = new CustomBean("艾瑞莉娅", "不要再削我了,呜呜~~~~(>_<)~~~~", R.mipmap.head_irly, "14:22");
customBeanList.add(irly);
CustomBean ali = new CustomBean("阿狸", "你渴望什么呢?", R.mipmap.head_ali, "13:55");
customBeanList.add(ali);
CustomBean jinx = new CustomBean("金克斯", "我数到三...... 3", R.mipmap.head_jinx, "13:53");
customBeanList.add(jinx);
}
package com.mesmerize.listviewapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by mesmerize on 2017/1/5.
*
* 自定义 ListView
*/
public class CustomActivity extends Activity {
private List<CustomBean> customBeanList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customBeanList = new ArrayList<>();
initCustomList();
ListView mListView = (ListView) findViewById(R.id.lv_listview);
CustomAdapter mAdapter = new CustomAdapter(this, customBeanList);
mListView.setAdapter(mAdapter);
}
/**
* 初始化数据
*/
private void initCustomList(){
CustomBean vn = new CustomBean("薇恩", "走,开黑去,我带你秀起来", R.mipmap.head_vn, "20:23");
customBeanList.add(vn);
CustomBean lks = new CustomBean("拉克丝", "我刚买的大天使元素,来来来,带你上分", R.mipmap.head_lakesi, "17:33");
customBeanList.add(lks);
CustomBean ey = new CustomBean("好运", "啊哈哈,啊哈哈哈哈,哈啊啊哈哈啊", R.mipmap.head_eyun, "16:53");
customBeanList.add(ey);
CustomBean xz = new CustomBean("瞎子", "我用双手成就你的梦想,一库", R.mipmap.head_liqing, "16:15");
customBeanList.add(xz);
CustomBean ylst = new CustomBean("伊利斯坦", "我的滑板鞋时尚时尚最时尚", R.mipmap.head_ylst, "15:25");
customBeanList.add(ylst);
CustomBean irly = new CustomBean("艾瑞莉娅", "不要再削我了,呜呜~~~~(>_<)~~~~", R.mipmap.head_irly, "14:22");
customBeanList.add(irly);
CustomBean ali = new CustomBean("阿狸", "你渴望什么呢?", R.mipmap.head_ali, "13:55");
customBeanList.add(ali);
CustomBean jinx = new CustomBean("金克斯", "我数到三...... 3", R.mipmap.head_jinx, "13:53");
customBeanList.add(jinx);
}
}
public class CustomAdapter extends BaseAdapter {
private final CustomActivity context;
private final List<CustomBean> list;
...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CustomBean item = list.get(position);
View view;
if (convertView == null) {
view = View.inflate(context, R.layout.adapter_custom, null);
} else {
view = convertView;
}
ImageView head = (ImageView) view.findViewById(R.id.head);
TextView nick = (TextView) view.findViewById(R.id.nick);
TextView message = (TextView) view.findViewById(R.id.message);
TextView time = (TextView) view.findViewById(R.id.time);
head.setImageResource(item.headImage);
nick.setText(item.nick);
message.setText(item.chatMessage);
time.setText(item.time);
return view;
}
}
package com.mesmerize.listviewapp;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by mesmerize on 2017/1/5.
*/
public class CustomAdapter extends BaseAdapter {
private final CustomActivity context;
private final List<CustomBean> list;
...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CustomBean item = list.get(position);
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(context, R.layout.adapter_custom, null);
holder = new ViewHolder();
holder.head = (ImageView) convertView.findViewById(R.id.head);
holder.nick = (TextView) convertView.findViewById(R.id.nick);
holder.message = (TextView) convertView.findViewById(R.id.message);
holder.time = (TextView) convertView.findViewById(R.id.time);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.head.setImageResource(item.headImage);
holder.nick.setText(item.nick);
holder.message.setText(item.chatMessage);
holder.time.setText(item.time);
return convertView;
}
class ViewHolder {
ImageView head;
TextView nick,message,time;
}
}
package com.mesmerize.listviewapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* Created by mesmerize on 2017/1/5.
*
* 自定义 ListView
*/
public class CustomActivity extends Activity {
private List<CustomBean> customBeanList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customBeanList = new ArrayList<>();
initCustomList();
ListView mListView = (ListView) findViewById(R.id.lv_listview);
CustomAdapter mAdapter = new CustomAdapter(this, customBeanList);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(CustomActivity.this, "哈哈哈 我是 " + customBeanList.get(position).nick, Toast.LENGTH_SHORT).show();
}
});
}
/**
* 初始化数据
*/
private void initCustomList(){
for (int i = 0; i < 3; i++) {
CustomBean vn = new CustomBean("薇恩", "走,开黑去,我带你秀起来", R.mipmap.head_vn, "20:23");
customBeanList.add(vn);
CustomBean lks = new CustomBean("拉克丝", "我刚买的大天使元素,来来来,带你上分", R.mipmap.head_lakesi, "17:33");
customBeanList.add(lks);
CustomBean ey = new CustomBean("好运", "啊哈哈,啊哈哈哈哈,哈啊啊哈哈啊", R.mipmap.head_eyun, "16:53");
customBeanList.add(ey);
CustomBean xz = new CustomBean("瞎子", "我用双手成就你的梦想,一库", R.mipmap.head_liqing, "16:15");
customBeanList.add(xz);
CustomBean ylst = new CustomBean("伊利斯坦", "我的滑板鞋时尚时尚最时尚", R.mipmap.head_ylst, "15:25");
customBeanList.add(ylst);
CustomBean irly = new CustomBean("艾瑞莉娅", "不要再削我了,呜呜~~~~(>_<)~~~~", R.mipmap.head_irly, "14:22");
customBeanList.add(irly);
CustomBean ali = new CustomBean("阿狸", "你渴望什么呢?", R.mipmap.head_ali, "13:55");
customBeanList.add(ali);
CustomBean jinx = new CustomBean("金克斯", "我数到三...... 3", R.mipmap.head_jinx, "13:53");
customBeanList.add(jinx);
}
}
}
1.25 MB, 下载次数: 44
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |