效果图:
实现思路:
1.创建ListView布局,以及展示数据的视图
2.准备数据
3.创建自定义适配器(将准备好的数据绑定到要展示的视图文件中)
4.为ListView组件设置创建好的适配器
1.视图模块
ListView布局:
<android.support.constraint.ConstraintLayout 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”
tools:context=".MainActivity">
<ListView
android:id="@+id/list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
展示数据的视图:
<ImageView
android:id="@+id/iv_listviewitem_image"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:padding="10dp"
android:scaleType="fitXY"
android:src="@drawable/book1" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="6"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/tv_listviewitme_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="狂人摄影日记"
android:textColor="@color/red"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="书本作者:"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_listviewitme_author"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="阿刘" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="书本价格:"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_listviewitme_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="$123"
android:textColor="@color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text=" 出版社:"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_listviewitme_publish"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="电子出版社"
android:textColor="@color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="书本简介:"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_listviewitme_remark"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:ellipsize="end"
android:maxLines="2"
android:text="很冷的一个冬天很冷的一个冬天很冷的一个冬天很冷的一个冬天很冷的一个冬天很冷的一个冬天很冷的一个冬天很冷的一个冬天很冷的一个冬天很冷的一个冬天"
android:textColor="@color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/bt_listviewitme_btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:src="@drawable/btn_shopping" />
<ImageButton
android:id="@+id/bt_listviewitme_btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:src="@drawable/btn_accounts" />
</LinearLayout>
</LinearLayout>
2.数据模块(这里是进行数据模拟,实际数据是来自数据库) 实体类:
package com.study.t212_09;
public class Book {
private Integer id;
private String title;
private String author;
private Float price;
private String publish;
private String remark;
private int image;
public Book() {
super();
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
数据准备:
package com.study.t212_09;
import java.util.ArrayList;
import java.util.List;
public class BookDao {
private int[] bookImages = new int[] { R.drawable.book1, R.drawable.book2,
R.drawable.book3, R.drawable.book4, R.drawable.book5,
R.drawable.book6, R.drawable.book7, R.drawable.book8,
R.drawable.book9, R.drawable.book10 };
public List<Book> list() {
List<Book> list = new ArrayList<Book>();
for (int i = 1; i <= 1000; i++) {
Book b = new Book();
b.setId(i);
b.setTitle("t" + i);
b.setAuthor("a" + i);
b.setPrice(0.0f + i);
b.setPublish("p" + i);
b.setRemark("r" + i);
b.setImage(bookImages[i % bookImages.length]);
list.add(b);
}
return list;
}
}
3.实现
package com.study.t212_09;
import android.content.Context;
import android.support.v7.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;
import java.util.List;
import java.util.zip.Inflater;
public class MainActivity extends AppCompatActivity {
private List data;
private MyBaseAdapt mba;//自定义适配器
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = findViewById(R.id.list_view);
//1.数据准备
data = new BookDao().list();
//2.创建自定义适配器
mba = new MyBaseAdapt((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE));
//3.为listView设置适配器
lv.setAdapter(mba);
}
public class MyBaseAdapt extends BaseAdapter{
public class ViewHolder{
ImageView iv_listviewitem_image;
TextView tv_listviewitme_title;
TextView tv_listviewitme_author;
TextView tv_listviewitme_price;
TextView tv_listviewitme_publish;
TextView tv_listviewitme_remark;
}
private LayoutInflater inflater;
public MyBaseAdapt(LayoutInflater inflater) {
this.inflater = inflater;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int i) {
return data.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
if (v == null){
v = inflater.inflate(R.layout.listview_item,null);
ViewHolder vh = new ViewHolder();
vh.iv_listviewitem_image = v.findViewById(R.id.iv_listviewitem_image);
vh.tv_listviewitme_title = v.findViewById(R.id.tv_listviewitme_title);
vh.tv_listviewitme_author = v.findViewById(R.id.tv_listviewitme_author);
vh.tv_listviewitme_price = v.findViewById(R.id.tv_listviewitme_price);
vh.tv_listviewitme_publish = v.findViewById(R.id.tv_listviewitme_publish);
vh.tv_listviewitme_remark = v.findViewById(R.id.tv_listviewitme_remark);
v.setTag(vh);
}
ViewHolder vh = (ViewHolder) v.getTag();
Book book = data.get(i);
vh.iv_listviewitem_image.setImageResource(book.getImage());
vh.tv_listviewitme_title.setText(book.getTitle());
vh.tv_listviewitme_author.setText(book.getAuthor());
vh.tv_listviewitme_price.setText(book.getPrice()+"");
vh.tv_listviewitme_publish.setText(book.getPublish());
vh.tv_listviewitme_remark.setText(book.getRemark());
return v;
}
}
}
————————————————
版权声明:本文为CSDN博主「NRT」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/li1470846/article/details/82724616