Android学习-列表视图ListView
Posted ycmqaq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android学习-列表视图ListView相关的知识,希望对你有一定的参考价值。
一、简介:
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;//松开后不会显示点击事件 } });
|
运行截图:
以上是关于Android学习-列表视图ListView的主要内容,如果未能解决你的问题,请参考以下文章
Xamarin 中 ListView 的不同视图形式 Android 和 UWP
一起学Android之ListView
Android 更复杂的 ListView 教程 [关闭]
Android listview edittext过滤空间按钮?
android - listview 按位置获取项目视图
我如何从firebase检索数据到android listview顶部[重复]