说明:使用ListView显示图片和文字信息,点击按钮删除最后一项内容
- 在主布局文件中添加ListView和Button控件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/game_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/remove_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/text"
android:layout_centerHorizontal="true"
android:text="删除一个列表项" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/game_img"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:src="@drawable/ic_launcher_background" />
<TextView
android:id="@+id/game_name"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_toEndOf="@+id/game_img"
android:gravity="start|center_vertical"
android:text="休闲益智"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
- 在MainActivity.java文件中编写逻辑代码
package com.youngpain.listviewdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private Button button;
//填充ListView的图片
private int[] gameImages = {R.drawable.yizhi, R.drawable.celue, R.drawable.tiyu
, R.drawable.qipai, R.drawable.sheji, R.drawable.banyan};
//填充ListView的文字
private String[] gameNames = {"休闲益智", "经营策略", "体育竞速", "棋牌桌游", "动作射击", "角色扮演"};
//图片和文字的集合
private List<Map<String, Object>> gameInfo = new ArrayList<>();
//ListView最后一项的索引
private int itemIndex = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去顶栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
//初始化
init();
//创建Adapter
final SimpleAdapter simpleAdapter = new SimpleAdapter(this, gameInfo, R.layout.game_item
, new String[]{"image", "name"}, new int[]{R.id.game_img, R.id.game_name});
//绑定Adapter
listView.setAdapter(simpleAdapter);
//设置监听事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (itemIndex >= 0) {
//删除最后一项数据
gameInfo.remove(itemIndex);
itemIndex--;
}
simpleAdapter.notifyDataSetChanged();
listView.invalidate();
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//点击子项时获取子项内容并显示
String itemName = (String) gameInfo.get(position).get("name");
Toast.makeText(MainActivity.this, itemName, Toast.LENGTH_SHORT).show();
}
});
}
/**
* 初始化操作
*/
private void init() {
//获取ListView
listView = findViewById(R.id.game_list);
//获取Button
button = findViewById(R.id.remove_item);
//将图片和文字添加到集合中
for (int i = 0; i < gameImages.length; i++) {
Map<String, Object> map = new HashMap<>();
map.put("image", gameImages[i]);
map.put("name", gameNames[i]);
gameInfo.add(map);
}
}
}