[英]android - listview get item view by position
我有带有自定义适配器(基本适配器)的列表视图。 我想按位置从列表视图中获取视图。 我试过mListView.getChildAt(position)
,但它不起作用。 如何按位置获取项目视图?
用这个 :
public View getViewByPosition(int pos, ListView listView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
if (pos < firstListItemPosition || pos > lastListItemPosition ) {
return listView.getAdapter().getView(pos, null, listView);
} else {
final int childIndex = pos - firstListItemPosition;
return listView.getChildAt(childIndex);
}
}
您只能从 ListView 获取可见视图,因为 ListView 中的行视图是可重用的。 如果您使用mListView.getChildAt(0)
您将获得第一个可见视图。 此视图与来自适配器的项目相关联,位于位置mListView.getFirstVisiblePosition()
。
绘制 ListView 后更改外观/任何行视图的首选方法是更改 ListView 从(传递到您的适配器的对象数组)绘制的数据中的某些内容,并确保在您的 getView() 中考虑到这一点函数,然后通过调用重绘 ListView
notifyDataSetChanged();
编辑:虽然有一种方法可以做到这一点,但如果您需要这样做,很可能会做错事。 虽然我能想到的边缘情况很少,但通常使用notifyDataSetChanged()
和其他内置机制是一种方法。
编辑 2:人们常犯的错误之一是试图想出自己的方式来响应用户在 ListView 中单击/选择一行,如对这篇文章的评论之一。 有一种现有的方法可以做到这一点。 就是这样:
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
/* Parameters
parent: The AdapterView where the click happened.
view: The view within the AdapterView that was clicked (this will be a view provided by the adapter)
position: The position of the view in the adapter.
id: The row id of the item that was clicked. */
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//your code here
}
});
ListView有很多内置功能,无需为更简单的情况重新发明轮子。 由于 ListView 扩展了AdapterView ,您可以设置相同的侦听器,例如上面示例中的OnItemClickListener 。
workignHoursListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,View view, int position, long id) {
viewtype yourview=yourListViewId.getChildAt(position).findViewById(R.id.viewid);
}
});
这是 VVB 发布的函数的 Kotlin 版本。 我在 ListView Adapter 中使用它来实现 getView() 中的“当在当前行的最后一个 EditText 上按下 Enter 键时转到下一行第一个 EditText”功能。
在 ListViewAdapter 类中,fun getView() 添加 lastEditText.setOnKeyListner 如下:
lastEditText.setOnKeyListener { v, keyCode, event ->
var setOnKeyListener = false
if (keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_UP) {
try {
val nextRow = getViewByPosition(position + 1, parent as ListView) as LinearLayout
val nextET = nextRow.findViewById(R.id.firstEditText) as EditText
nextET.isFocusableInTouchMode = true
nextET.requestFocus()
} catch (e: Exception) {
// do nothing
}
setOnKeyListener = true
}
setOnKeyListener
}
在 fun getView() 之后添加 fun getViewByPosition() 如下:
private fun getViewByPosition(pos: Int, listView: ListView): View? {
val firstListItemPosition: Int = listView.firstVisiblePosition
val lastListItemPosition: Int = firstListItemPosition + listView.childCount - 1
return if (pos < firstListItemPosition || pos > lastListItemPosition) {
listView.adapter.getView(pos, null, listView)
} else {
val childIndex = pos + listView.headerViewsCount - firstListItemPosition
listView.getChildAt(childIndex)
}
}
Listview lv = (ListView) findViewById(R.id.previewlist);
final BaseAdapter adapter = new PreviewAdapter(this, name, age);
confirm.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
View view = null;
String value;
for (int i = 0; i < adapter.getCount(); i++) {
view = adapter.getView(i, view, lv);
Textview et = (TextView) view.findViewById(R.id.passfare);
value=et.getText().toString();
Toast.makeText(getApplicationContext(), value,
Toast.LENGTH_SHORT).show();
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.