一、简介: ListView,列表视图,直接继承了AbsListView,是一个以垂直方式在项目中显示View视图的列表。ListView的数据项,来自一个继承了ListAdapter接口的适配器。 二、新建一个包listview并新建ListViewActivity.java活动: 三、在Andr ......
一、简介:
listview,列表视图,直接继承了abslistview,是一个以垂直方式在项目中显示view视图的列表。listview的数据项,来自一个继承了listadapter接口的适配器。
二、新建一个包listview并新建listviewactivity.java活动:
1 2 3 4 5 6 7 8
|
public class listviewactivity extends appcompatactivity {
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_list_view); } }
|
三、在androidmanifest.xml中声名activity:
1
|
<activity android:name=".listview.listviewactivity"></activity>
|
四、建立activity_list_view.xml布局:
1 2 3 4 5 6
|
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
</linearlayout>
|
五、在activity_main.xml中新建一个按钮:
1 2 3 4 5 6
|
<button android:id="@+id/btn_listview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="listview" android:textallcaps="false"/>
|
六、在mainactivity.java中声名控件:
1
|
private button mbtnlistview;
|
七、在mainactivity.java中找到控件:
1
|
mbtnlistview=findviewbyid(r.id.btn_listview);
|
八、设置点击事件:
1 2 3 4 5 6 7 8
|
mbtnlistview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { //跳转到listview演示页面 intent intent=new intent(mainactivity.this,listviewactivity.class); startactivity(intent); } });
|
九、在activity_list_view.xml布局中写代码:
1 2 3 4 5 6 7 8 9 10 11 12
|
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
<listview android:id="@+id/lv_1" android:layout_width="match_parent" android:layout_height="wrap_content">
</listview> </linearlayout>
|
十、新建layout_list_item.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:paddingleft="15dp" android:paddingright="15dp" android:paddingtop="10dp" android:paddingbottom="10dp">
<imageview android:id="@+id/iv" android:layout_width="100dp" android:layout_height="100dp" android:scaletype="centercrop" android:background="#000"/>
<linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingleft="10dp">
<textview android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello" android:textsize="20sp" android:textcolor="#000"/>
<textview android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2018-11-27" android:textsize="18sp" android:textcolor="#808080" android:layout_margintop="10dp"/>
<textview android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是内容" android:textsize="18sp" android:textcolor="#808080" android:layout_margintop="10dp"/>
</linearlayout> </linearlayout>
|
十一、在包listview中新建mylistadapter.java继承自baseadapter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
public class mylistadapter extends baseadapter {
private context mcontext; private layoutinflater mlayoutinflater;
public mylistadapter(context context){ this.mcontext=context; mlayoutinflater=layoutinflater.from(context); }
@override public int getcount() { return 10; }
@override public object getitem(int i) { return null; }
@override public long getitemid(int i) { return 0; }
static class viewholder{ public imageview imageview; public textview tvtitle,tvtime,tvcontent; }
@override public view getview(int position, view convertview, viewgroup parent) { viewholder holder=null; if(convertview==null){ convertview=mlayoutinflater.inflate(r.layout.layout_list_item,null); holder=new viewholder(); holder.imageview=convertview.findviewbyid(r.id.iv); holder.tvtitle=convertview.findviewbyid(r.id.tv_title); holder.tvtime=convertview.findviewbyid(r.id.tv_time); holder.tvcontent=convertview.findviewbyid(r.id.tv_content); convertview.settag(holder); }else{ holder=(viewholder)convertview.gettag(); } //给控件赋值 holder.tvtitle.settext("这是标题"); holder.tvtime.settext("2018-11-28"); holder.tvcontent.settext("这是内容"); glide.with(mcontext).load("https://www.baidu.com/img/bd_logo1.png?where=super").into(holder.imageview); return convertview; } }
|
十二、在listviewactivity.java中写代码:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
public class listviewactivity extends appcompatactivity {
private listview mlv1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_list_view);
mlv1=findviewbyid(r.id.lv_1); mlv1.setadapter(new mylistadapter(listviewactivity.this)); } }
|
运行结果:
十三、在drawable下新建一个list_item:
1 2 3 4 5 6 7
|
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@color/coloraccent"/> <item android:state_pressed="true" android:drawable="@color/coloraccent"/> <item android:state_focused="true" android:drawable="@color/coloraccent"/> <item android:drawable="@color/colorwhite"/> </selector>
|
十四、在activity_list_view.xml下配置:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
<listview android:id="@+id/lv_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:listselector="@drawable/list_item">
</listview> </linearlayout>
|
运行截图:
十五、在listviewactivity.java设置点击和长按事件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
mlv1.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int position, long l) { toast.maketext(listviewactivity.this,"点击 pos:"+position,toast.length_short).show(); } });
mlv1.setonitemlongclicklistener(new adapterview.onitemlongclicklistener() { @override public boolean onitemlongclick(adapterview<?> adapterview, view view, int position, long l) { toast.maketext(listviewactivity.this,"长按 pos:"+position,toast.length_short).show(); return true;//松开后不会显示点击事件 } });
|
运行截图: