1.布局添加Listview
2.找到listview
3.创建一个Adapter适配器继承BaseAdapter,封装4个方法,其中getcount,getview必须封装
getcount:告诉listview要显示的条目数
getview:告诉listview每个条目显示的内容。
4.创建Adapter的一个对象,设置给listview。
listview.setAdapter(ListAdapter adapter);
TextView view = null;
if(convertView != null){//判断converView是否为空,不为空重新使用
view = (TextView) convertView;
}else{
view = new TextView(mContext);//创建一个textView对象
}
return view;
public class MainActivity extends AppCompatActivity {
//1,声明控件LISTVIEW
private ListView listView1;
private ListView listView2;
private ListView listView3;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//2,获取控件的id
listView1 = (ListView)findViewById(R.id.list_item1);
listView2 = (ListView)findViewById(R.id.list_item2);
listView3 = (ListView)findViewById(R.id.list_item3);
mContext = this;
//3,进行绑定
MyAdapter myAdapter = new MyAdapter();
listView3.setAdapter(myAdapter);
listView2.setAdapter(myAdapter);
listView1.setAdapter(myAdapter);
}
//创建适配器类实现接口
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return 50;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
//声明一个textview对象
TextView view0 = null;
//进行判断是否能过复用
if(view != null){
view0 = (TextView)view;
}else {
view0 = new TextView(mContext);
}
view0.setTextSize(40);
Random random = new Random();
int num = random.nextInt(100);
if(num<20){
view0.setText("桃");
view0.setTextColor(Color.parseColor("#ff00ff"));
}else if(num<40){
view0.setText("梨");
view0.setTextColor(Color.YELLOW);
}else if(num<60){
view0.setText("枣");
view0.setTextColor(Color.RED);
}else if(num<80){
view0.setText("橘");
view0.setTextColor(Color.parseColor("#d4824f"));
}else{
view0.setText("杏");
view0.setTextColor(Color.parseColor("#00ff00"));
}
return view0;
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.wenkai.tigerlistview.MainActivity">
<ListView
android:id="@+id/list_item1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ListView
android:id="@+id/list_item2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ListView
android:id="@+id/list_item3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有