【Android】ListView控件的用法

正文索引 [隐藏]

Android的ListView控件是一个最常用但是却又最难用的控件,下面的链接是ListView简单用法的一些素材
ListView图片素材

前端XML界面

在ListView简单用法中的界面非常的简单,用线性布局,上面一个ListView,下面一个文本框+按钮实现按按钮增加数据,so easy.
LISTVIEWEAMPLE

在MainActivity.java文件里面添加数据源

private String[] data = {"Apple","Banana","Orange","Watemelon","Pear","Grape","Pinepple",
"Strawberray","Cherry","Mango","Apple","Banana","Orange", "Watemelon",
"Pear","Grape","Pinepple","Strawberray","Cherry","Mango"};

然后在onCreate里面调用
        ArrayAdapter<String>adapter =new ArrayAdapter<String>(
MainActivity. this, android.R. layout. simple list item_1, data); ListView listView=(ListView) findViewById(R. id. lv);
listView. setAdapter(adapter);

运行后发现里面的数据都变成了自己添加的文本了.

定制条目的样式

1.新建Fruit.java,里面代码如下

public class Fruit {
private String name; //条目内容
private int imagedId; //条目图片的IP
private boolean isDelMode; //后面要用到的长按删除/取消
public Fruit(String name,int imagedId){
this.name = name;
this.imagedId = imagedId;
}
public String getName() {
return name;
}
public int getImagedId() {
return imagedId;
}
public boolean isDelMode() {
return isDelMode;
}
public void setDelMode(boolean DelMode) {
isDelMode = DelMode;
}
}

2.新建fruit_item.xml,代码如下
<androidx.constraintlayout.widget.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"
android:orientation="horizontal">
<ImageView
android:id="@+id/imgFruit"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:src="@drawable/apple_pic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvFruitName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="some fruit name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@id/imgFruit"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imgDel"
android:layout_width="64dp"
android:layout_height="64dp"
app:layout_constraintBottom_toBottomOf="@+id/imgCancel"
app:layout_constraintEnd_toStartOf="@+id/imgCancel"
app:layout_constraintTop_toTopOf="@+id/imgCancel"
app:srcCompat="@drawable/del" />
<ImageView
android:id="@+id/imgCancel"
android:layout_width="64dp"
android:layout_height="64dp"
app:layout_constraintBottom_toBottomOf="@+id/imgFruit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/imgFruit"
app:srcCompat="@drawable/cancel" />
<androidx.constraintlayout.widget.Group
android:id="@+id/group"
android:layout_width="wrap_content"
app:constraint_referenced_ids="imgCancel,imgDel"
android:visibility="invisible"
android:layout_height="wrap_content" />
</androidx.constraintlayout.widget.ConstraintLayout>

3.新建自己的适配器继承自ArrayAdapter,代码如下
public class FruitAdapter extends ArrayAdapter {
public FruitAdapter(Context context, int fruit_item, List objects) {
super(context, 0, objects);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Fruit fruit = getItem(position); // 获取当前项的Fruit实例
View view; //窗口
ViewHolder viewHolder;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.fruit_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.fruitImage = (ImageView) view.findViewById (R.id.imgFruit);
viewHolder.fruitName = (TextView) view.findViewById (R.id.tvFruitName);
viewHolder.fruitDel = (ImageView)view.findViewById(R.id.imgDel);
viewHolder.fruitCancel = (ImageView)view.findViewById(R.id.imgCancel);
viewHolder.group = (Group)view.findViewById(R.id.group);
view.setTag(viewHolder); // 将ViewHolder存储在View中
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag(); // 重新获取ViewHolder
}
viewHolder.fruitImage.setImageResource(fruit.getImagedId());
viewHolder.fruitName.setText(fruit.getName());
if(fruit.isDelMode()){
viewHolder.group.setVisibility(View.VISIBLE);//可见
}else{
viewHolder.group.setVisibility(View.INVISIBLE);//不可见
}
return view;
}
class ViewHolder {
ImageView fruitImage;
TextView fruitName;
ImageView fruitDel;
ImageView fruitCancel;
Group group;
}
}

重写数据源

1.在MainActivity里面写initFruit()初始化数据源函数,代码如下

private void initFruit(){
for (int i=0;i<2;i++)
{
Fruit apple = new Fruit("Apple", R.drawable.apple_pic);
fruitList.add(apple);
Fruit banana = new Fruit("Banana", R.drawable.banana_pic);
fruitList.add(banana);
Fruit orange = new Fruit("Orange", R.drawable.orange_pic);
fruitList.add(orange);
Fruit watermelon = new Fruit("Watermelon", R.drawable.watermelon_pic);
fruitList.add(watermelon);
Fruit pear = new Fruit("Pear", R.drawable.pear_pic);
fruitList.add(pear);
Fruit grape = new Fruit("Grape", R.drawable.grape_pic);
fruitList.add(grape);
Fruit pineapple = new Fruit("Pineapple", R.drawable.pineapple_pic);
fruitList.add(pineapple);
Fruit strawberry = new Fruit("Strawberry", R.drawable.strawberry_pic);
fruitList.add(strawberry);
Fruit cherry = new Fruit("Cherry", R.drawable.cherry_pic);
fruitList.add(cherry);
Fruit mango = new Fruit("Mango", R.drawable.mango_pic);
fruitList.add(mango);
}
}

然后到onCreate里面调用inteFruit()初始化数据源函数,当然还要注释/删掉那个data数据源
2.定义自己的适配器的全局变量
 private List<Fruit> fruitList = new ArrayList<Fruit>();
private FruitAdapter adapter;

3.注释/删掉之前那个适配器的代码,调用自己的适配器的方法,代码如下,
adapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList);
final ListView listView = (ListView)findViewById(R.id.lv);
listView.setAdapter(adapter);

添加数据

final EditText etFruitName = (EditText)findViewById(R.id.edFruitName);
Button btnAdd = (Button)findViewById(R.id.addBtn);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String strAddTxt = etFruitName.getText().toString();
if(strAddTxt.isEmpty()) {
Log.i("DataEmpty", "数据为空");
}
else {
Fruit newFruit = new Fruit(strAddTxt,R.drawable.fruitlogo);
//fruitList.add(newFruit);
//adapter.notifyDataSetChanged();//通知适配器数据源变了
adapter.add(newFruit); //适配器会自动调用notifyDataSetChanged()
etFruitName.setText(""); //点完按钮后自动清空文本框
listView.setSelection(adapter.getCount()); //添加数据后自动滚到最后一行
}
}
});

长按显示删除/取消图片

1.编写长按条目事件代码,代码如下

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
Fruit fruit = fruitList.get(position);
fruit.setDelMode(true); //显示
adapter.notifyDataSetChanged();//数据源变化
return false;
}
});

2.去FruitAdapter适配器里面写删除/取消事件,因为删除/取消图片都在适配器里面,所以去FruitAdapter.java里面getView()写事件代码
//删除图片事件
viewHolder.fruitDel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FruitAdapter.this.remove(fruit); //移除当前水果
}
});
//取消
viewHolder.fruitCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fruit.setDelMode(false); //不可见
notifyDataSetChanged(); //通知适配器数据源发生改变
}
});

这样样子,基本上已经完成了,一些小细节可以自己增加,ListView难用就难用在需要用适配器
如果适配器不是很理解的话是比较难学会的.

blank