用listview写一个列表,自定义item样式
遇到的问题,一定要给item的文本设置字体颜色,第一次没设,一直以为程序有bug,结果字体是白色的,好好的被折腾了一番;另外图片的导入也需要注意,有几次不能展示,很奇怪,问题没法复现,多次重新编译后成功,不知道为什么
样式文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">
<ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#4287f5" android:dividerHeight="1dp" /> </LinearLayout>
|
Item
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/item" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal">
<ImageView android:id="@+id/item_iv" android:layout_width="102dp" android:layout_height="75dp" app:srcCompat="@drawable/dog" />
<TextView android:id="@+id/item_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="text" android:textColor="@color/colorAccent" /> </LinearLayout>
|
Activity
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| package com.example.listview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ListView listView; private String[] datas={ "第1条数据", "第2条数据", "第3条数据", "第4条数据"}; private int[] icons={ R.drawable.dog,R.drawable.dog,R.drawable.dog,R.drawable.dog, }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.lv); MyBaseadapter mAdapter=new MyBaseadapter(); listView.setAdapter(mAdapter); }
static class ViewHolder{ ImageView iv; TextView tv; }
class MyBaseadapter extends BaseAdapter{ @Override public int getCount(){ return datas.length; } @Override public Object getItem(int position){ return datas[position]; } @Override public long getItemId(int position){ return position; } @Override public View getView(int position, View convertView, ViewGroup parent){ ViewHolder holder; if (convertView==null){ convertView= LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_item,parent,false); // LayoutInflater.from()从一个Context中获取一个布局填充器 //inflate()将xml转化为view holder=new ViewHolder(); holder.iv=(ImageView)convertView.findViewById(R.id.item_iv); holder.tv=(TextView) convertView.findViewById(R.id.item_tv); convertView.setTag(holder); }else { holder=(ViewHolder) convertView.getTag(); } holder.tv.setText(datas[position]); Log.e("tv",datas[position]); holder.iv.setImageResource(icons[position]); return convertView; } } }
|
界面