如果ListView只能出现字,那就太单调拉。现在我们来实现ListView的定制。
主要步骤:
建类
建Adapter
在主页面使用adapter
接下来,敲代码,假设我们有一个房子类:
package com.example.listviewapplication; import android.widget.ImageView; public class Room { private String txt; private int img; public Room() { } public Room(String txt, int img) { this.txt = txt; this.img = img; } public String getTxt() { return txt; } public void setTxt(String txt) { this.txt = txt; } public int getImg() { return img; } public void setImg(int img) { this.img = img; } }
接着,是RoomAdapter:
package com.example.listviewapplication; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import java.util.List; public class RoomAdapter extends ArrayAdapter<Room> { private int resourceId; public RoomAdapter(@NonNull Context context, int resource, @NonNull List<Room> objects) { super(context, resource, objects); resourceId = resource; } @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { Room room = getItem(position);//获取当前项的room实例 View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false); ImageView roomImg = view.findViewById(R.id.img); TextView roomTxt = view.findViewById(R.id.txt); roomImg.setImageResource(room.getImg()); roomTxt.setText(room.getTxt()); return view; } }
接着主页面实现:
package com.example.listviewapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<Room> roomList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initRoom(); RoomAdapter adapter = new RoomAdapter(MainActivity.this,R.layout.simple_item,roomList); ListView listView = (ListView)findViewById(R.id.list_view); listView.setAdapter(adapter); } private void initRoom() { for(int i =0;i<2;i++){ Room room1 = new Room("room1", R.mipmap.try_1); roomList.add(room1); Room room2 = new Room("room2", R.mipmap.try_2); roomList.add(room2); Room room3 = new Room("room3", R.mipmap.try_3); roomList.add(room3); Room room4 = new Room("room4", R.mipmap.try_4); roomList.add(room4); } } }
layout_item:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_margin="10dp" android:layout_height="wrap_content"> <ImageView android:id="@+id/img" android:layout_width="100dp" android:layout_height="120dp" android:layout_margin="10dp" android:src="@mipmap/try_1"/> <TextView android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Room" android:textColor="#000000" android:textSize="20dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="100dp"/> </RelativeLayout> </LinearLayout>