Android列表组件ListView⼦项的点击事件与⼦项中组件的点击
事件
点击ListView⼦项时进⾏相关的逻辑操作,为ListView组件添加⼦项点击事件:
//设置ListView⼦项的点击事件
testLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ListViewDemoActivity.this, "点击的⼦项ID = " + position, Toast.LENGTH_SHORT).show();
}
});
ListView⼦项中组件的点击事件,在适配器中添加,这样不会与ListView⼦项的点击事件产⽣冲突问题:
//设置内容⽂本组件的点击事件
@Override
public void onClick(View v) {
Toast.makeText(context, (position), Toast.LENGTH_LONG).show();
}
});
完整代码:
package net.oschina.git.zhaikun.androiddeveloped.activitys;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import net.oschina.git.zhaikun.androiddeveloped.R;
import net.oschina.git.zhaikun.androiddeveloped.adapter.ListViewDemoAdapter;
import java.util.ArrayList;
import java.util.List;
/**
* Created by zhaikun68 on 2018/3/5.
* <p>
* ListView演⽰Demo
*/
public class ListViewDemoActivity extends AppCompatActivity implements View.OnClickListener, View.OnTouchListener, AbsListView.OnScrollListener { private static final String TAG = "ListViewDemoActivity";//⽇志打印标记
private ListView testLv;//ListView组件
private Button updateDataBtn;//动态加载数据组件
private List<String> dataList = new ArrayList<>();//存储数据
private ListViewDemoAdapter listViewDemoAdapter;//ListView的数据适配器
private int firstVisibleItem = 0;//记录ListView滑动过程中,当前可视区域第⼀个⼦项的ID
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
setContentView(R.layout.activity_listview_demo);
initView();//初始化组件
initData();//初始化数据
}
/**
* 初始化组件
*/
private void initView() {
testLv = (ListView) findViewById(st_lv);
updateDataBtn = (Button) findViewById(R.id.update_data_btn);
updateDataBtn.setOnClickListener(this);//动态加载数据组件的点击事件
testLv.setOnTouchListener(this);//设置ListView组件的触摸事件
testLv.setOnScrollListener(this);//设置ListView组件的滑动监听事件
//设置ListView⼦项的点击事件
testLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ListViewDemoActivity.this, "点击的⼦项ID = " + position, Toast.LENGTH_SHORT).show(); }
});
}
/**
* 初始化数据
*/
private void initData() {
//初始化10项数据
for (int i = 1; i <= 20; i++) {
dataList.add("显⽰内容" + i);
}
//设置ListView的适配器
listViewDemoAdapter = new ListViewDemoAdapter(this, dataList);
testLv.setAdapter(listViewDemoAdapter);
//testLv.setSelection(4);
//数据为空时显⽰的提⽰信息
testLv.setEmptyView(findViewById(pty_tv));
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.update_data_btn://动态加载列表数据
dataList.add("动态加载的数据项");
//通知ListView更改数据源
if (listViewDemoAdapter != null) {
testLv.setSelection(dataList.size() - 1);//设置显⽰列表的最后⼀项
} else {
listViewDemoAdapter = new ListViewDemoAdapter(this, dataList);
testLv.setAdapter(listViewDemoAdapter);
testLv.setSelection(dataList.size() - 1);
}
break;
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (Action()) {
case MotionEvent.ACTION_DOWN://触摸屏幕时,只执⾏⼀次
Log.i(TAG, "执⾏触摸时的操作");
break;
case MotionEvent.ACTION_MOVE://触摸到屏幕时,反复执⾏
Log.i(TAG, "执⾏移动时的操作");
break;
case MotionEvent.ACTION_UP://离开屏幕时,只执⾏⼀次
Log.i(TAG, "执⾏触摸离开时的操作");
break;
}
return false;
}
@Override
excel listview控件public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case SCROLL_STATE_IDLE://停⽌滑动
Log.w(TAG, "停⽌滑动");
break;
case SCROLL_STATE_TOUCH_SCROLL://正在滑动
Log.w(TAG, "正在滑动");
break;
case SCROLL_STATE_FLING://滑动ListView离开后,由于惯性继续滑动
Log.w(TAG, "滑动ListView离开后,由于惯性继续滑动");
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { /*
ListView滑动时,⼀直回调该⽅法
firstVisibleItem:当前能看到的第⼀个⼦项的ID(从0开始)
visibleItemCount:当前能看到的⼦项的总数,包含未完整显⽰的⼦项,⽐如只显⽰了⼀⼩部分的⼦项 totalItemCount:所有⼦项的总数
*/
Log.e(TAG, "当前能看到的第⼀个⼦项的ID(从0开始)= " + firstVisibleItem);
Log.e(TAG, "当前能看到的⼦项的总数 = " + visibleItemCount);
Log.e(TAG, "所有⼦项的总数 = " + totalItemCount);
//可⽤于底部加载更多数据的判断逻辑
if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount > 0) {
Log.e(TAG, "滑动到ListView的最后⼀个⼦项");
}
//判断ListView的滑动⽅向
if (this.firstVisibleItem == firstVisibleItem) {
Log.e(TAG, "未发⽣滑动");
} else if (this.firstVisibleItem > firstVisibleItem) {
Log.e(TAG, "发⽣下滑");
} else {
Log.e(TAG, "发⽣上滑");
}
this.firstVisibleItem = firstVisibleItem;
}
}
布局⽂件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="schemas.android/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/update_data_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:text="动态加载数据"/>
<ListView
android:id="@+id/test_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/update_data_btn"
android:divider="@color/colorPrimaryDark"
android:dividerHeight="3dp"
android:scrollbars="none"/>
<TextView
android:id="@+id/empty_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@color/blue"
android:padding="20dp"
android:text="ListView数据为空提醒"
android:textColor="#000000"
android:textSize="16sp"/>
</RelativeLayout>
列表数据适配器:
package net.oschina.git.zhaikun.androiddeveloped.adapter;
t.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import net.oschina.git.zhaikun.androiddeveloped.R;
import java.util.List;
/
**
* Created by zhaikun68 on 2018/3/5.
* <p>
* ListView演⽰Demo中的数据适配器
*/
public class ListViewDemoAdapter extends BaseAdapter {
private Context context;//上下⽂对象
private List<String> dataList;//ListView显⽰的数据
/**
* 构造器
* 构造器
*
* @param context 上下⽂对象
* @param dataList 数据
*/
public ListViewDemoAdapter(Context context, List<String> dataList) {
this.dataList = dataList;
}
@Override
public int getCount() {
return dataList == null ? 0 : dataList.size();
}
@Override
public Object getItem(int position) {
(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
//判断是否有缓存
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_demo, null); viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
//得到缓存的布局
viewHolder = (ViewHolder) Tag();
}
//设置图⽚
viewHolder.pictureImg.setImageResource(R.mipmap.ic_launcher);
//设置内容
//设置内容⽂本组件的点击事件
@Override
public void onClick(View v) {
Toast.makeText(context, (position), Toast.LENGTH_LONG).show(); }
});
return convertView;
}
/**
* ViewHolder类
*/
private final class ViewHolder {
ImageView pictureImg;//图⽚
TextView contentTv;//内容
/**
* 构造器
*
* @param view 视图组件(ListView的⼦项视图)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论