Android ListView 之 自定义Adapter 实现列表条目功能,以及 OnItemClickListener(点击事件) OnScrollListener(滑动事件)事件监听
目录
Android ListView 之 自定义Adapter 实现列表条目功能,以及 OnItemClickListener(点击事件) OnScrollListener(滑动事件)事件监听
一、简单介绍
Android 开发中的一些基础操作,使用整理,便于后期使用。
本节介绍,ListView 的使用,并监听相关事件。
二、实现原理
1、布局文件中增加 ListView
2、准备数据 List 数据
3、构造适配器 Adapter,可以设置自己需要的 Adapter
4、通过 id 找到 ListView 的实例
5、ListView 设置适配器
6、设置监听 ListView 中Item 等相关事件
三、效果预览
四、实现步骤
1、新建一个工程
2、选择 Empty Activity,点击 Next
3、取一个名字,完成工程新建
4、新建三个脚本,一个作为 Item 数据类型,一个是 ListView 的适配器Adapter,最后在MainActivity,初始化数据
5、构建两个布局文件,一个是ListView容器,一个是 ListView item 的格式 布局
6、运行效果,一个默认的String Item布局效果,一个是 是自定义的布局和Adapter,并设置了监听事件
五、关键代码
1、MainActivity.java
(这里水果的图片都使用的是Android 自带的图片,你可以酌情添加自己的图片)
package com.pf.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
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 implements AdapterView.OnItemClickListener, AbsListView.OnScrollListener {
// 日志标签
private final String TAG = "ListView";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//SetStringListView();
SetCustomAdaperListView();
}
//region 字符串数据的 ListView
//数据列表
private String[] data = { "Apple", "Banana", "Orange", "Watermelon",
"Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango" };
/**
* 设置字符串的 ListView
*/
void SetStringListView(){
// 初始化 ListView 的 Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, data);
// 获取 ListView 布局
ListView listView = findViewById(R.id.list_view);
// 把 Adapter 设置到 listView 中
listView.setAdapter(adapter);
}
//endregion
//region 自定义 Adapter 的 ListView
// 数据列表
private List<Fruit> fruitList = new ArrayList<Fruit>();
/**
* 自定义 adapter 的 ListView 功能实现
*/
void SetCustomAdaperListView(){
InitFruits();
// 初始化 ListView 的 Adapter
FruitAdapter fruitAdapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList);
ListView listView = (ListView)findViewById(R.id.list_view);
listView.setAdapter(fruitAdapter);
listView.setOnItemClickListener(this);
listView.setOnScrollListener(this);
}
/**
*初始化水果数据
*/
void InitFruits(){
Fruit apple = new Fruit("Apple", R.drawable.ic_launcher_background);
fruitList.add(apple);
Fruit banana = new Fruit("Banana", R.drawable.ic_launcher_background);
fruitList.add(banana);
Fruit orange = new Fruit("Orange", R.drawable.ic_launcher_background);
fruitList.add(orange);
Fruit watermelon = new Fruit("Watermelon", R.drawable.ic_launcher_background);
fruitList.add(watermelon);
Fruit pear = new Fruit("Pear", R.drawable.ic_launcher_background);
fruitList.add(pear);
Fruit grape = new Fruit("Grape", R.drawable.ic_launcher_background);
fruitList.add(grape);
Fruit pineapple = new Fruit("Pineapple", R.drawable.ic_launcher_background);
fruitList.add(pineapple);
Fruit strawberry = new Fruit("Strawberry", R.drawable.ic_launcher_background);
fruitList.add(strawberry);
Fruit cherry = new Fruit("Cherry", R.drawable.ic_launcher_background);
fruitList.add(cherry);
Fruit mango = new Fruit("Mango", R.drawable.ic_launcher_background);
fruitList.add(mango);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Fruit fruit = fruitList.get(position);
Toast.makeText(MainActivity.this,"Fruit:"+fruit.getName(),Toast.LENGTH_SHORT).show();
Log.i(TAG, "onItemClick: fruit :" +fruit.getName());
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState){
case SCROLL_STATE_FLING:
Log.i(TAG, "用户手指离开屏幕后,因惯性继续滑动");
Toast.makeText(MainActivity.this,"用户手指离开屏幕后,因惯性继续滑动",Toast.LENGTH_SHORT).show();
break;
case SCROLL_STATE_IDLE:
Log.i(TAG,"已经停止滑动");
Toast.makeText(MainActivity.this,"已经停止滑动",Toast.LENGTH_SHORT).show();
break;
case SCROLL_STATE_TOUCH_SCROLL:
Log.i(TAG, "手指未离开屏幕,屏幕继续滑动");
Toast.makeText(MainActivity.this,"手指未离开屏幕,屏幕继续滑动",Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
//endregion
}
2、FruitAdapter.java
package com.pf.myapplication;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
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 java.util.List;
/**
* 自定义 Adapter
*/
public class FruitAdapter extends ArrayAdapter {
// layout 资源id
private final int resourceId;
// 数据
List<Fruit> fruitList ;
/**
* 构造函数,设置 layout 资源Id 和 数据
* @param context
* @param textViewResourceId
* @param objects
*/
public FruitAdapter( Context context, int textViewResourceId, List<Fruit> objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
fruitList = objects;
}
/**
* 把数据展示到ListView 的自定义布局中
* @param position
* @param convertView
* @param parent
* @return
*/
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Fruit fruit = (Fruit) getItem(position);
View view = LayoutInflater.from(getContext()).inflate(resourceId,null);
ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
return view;
}
/**
* 获得 Item
* @param position
* @return
*/
@Nullable
@Override
public Object getItem(int position) {
return fruitList.get(position);
}
/**
* 获得 ItemId
* @param position
* @return
*/
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
}
3、Fruit.java
package com.pf.myapplication;
/**
* 自定义的水果类
*/
public class Fruit {
private String name;
private int imageId;
/**
* 构造函数 设置数据
* @param name
* @param imageId
*/
public Fruit(String name, int imageId){
this.name = name;
this.imageId = imageId;
}
/**
* 获取名称
* @return
*/
public String getName(){
return name;
}
/**
* 获取图片ID
* @return
*/
public int getImageId(){
return imageId;
}
}
4、activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="-16dp" />
</android.support.constraint.ConstraintLayout>
5、fruit_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fruit_image"/>
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"/>
</LinearLayout>