我必须对列表视图执行单击,在onItem单击中,我选择了视图的位图。当我手动单击它是工作时,但是当我以编程方式执行单击时,视图的高度等于 0。这是我的代码片段
listMedic.performItemClick(
getViewByPosition(position), position, adapter.getItemId(position));
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
ListElement elem = (ListElement) listMedic.getItemAtPosition(position);
try {
QuantityPopUp quantityPopUp = new QuantityPopUp(context);
quantityPopUp.setImageAndPosition(getBitMapOfView(v), v, this, getResources().getString(R.string.valid_q_text_bt));
quantityPopUp.setQuantity(Float.parseFloat(elem.getqDonn().replace(',', '.')));
quantityPopUp.setListElementAdapter((ListElementAdapter) listMedic.getAdapter());
quantityPopUp.setIndex(position);
contentLayout.addView(quantityPopUp);
popUpI = quantityPopUp;
darkenView();
} catch (NullPointerException e) {
}
}
private Bitmap getBitMapOfView(View v){
//récupérer la vue en tant que bitmap
v.setDrawingCacheEnabled(true);
v.buildDrawingCache();
return Bitmap.createBitmap(v.getDrawingCache(true));
}
public View getViewByPosition(int pos) {
final int firstListItemPosition = listMedic.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listMedic.getChildCount() - 1;
if (pos < firstListItemPosition || pos > lastListItemPosition ) {
return listMedic.getAdapter().getView(pos, null, listMedic);
} else {
final int childIndex = pos - firstListItemPosition;
return listMedic.getChildAt(childIndex);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_element_layout, parent, false);
}
// récupérer l'item par sa position dans la vue
ListElement listElement = getItem(position);
//récupérer les éléments à remplir
LinearLayout listElementLayout = (LinearLayout) convertView.findViewById(R.id.listElementLayout);
TextView denomMedic = (TextView) convertView.findViewById(R.id.denomMedic);
TextView denom2Medic = (TextView) convertView.findViewById(R.id.denom2Medic);
TextView qPres = (TextView) convertView.findViewById(R.id.qPres);
TextView qDonn = (TextView) convertView.findViewById(R.id.qDonn);
ImageView compIcon = (ImageView) convertView.findViewById(R.id.compIcon);
//définir un touch listener pour afficher l'historique
listElementLayout.setTag(position);
//remplir les elements avec les informations
denomMedic.setText(listElement.getDenomMedic());
denom2Medic.setText(listElement.getDenom2Medic());
qPres.setText(listElement.getqPres());
qDonn.setText(listElement.getqDonn());
compIcon.setImageDrawable(listElement.getIconBac());
//retourner l'élément construit
return convertView;
}
问题的根本原因是,在某些情况下,您试图从从未布局和测量过的View
中获取位图。当您通过直接调用getView()
在getViewByPosition()
中获取视图时,会发生这种情况。除非您有充分的理由,否则不建议从此类列表中获取视图。
要解决您的问题,您有两种选择:
1(修复您的getViewByPosition()
以返回已经测量并准备绘制到位图的视图(这个答案可以帮助:将视图转换为位图而不在Android中显示它?
2( 仅对已绘制的视图调用getBitMapOfView()
。
我在多兰·雅科夫列夫-戈拉尼的帮助下找到了答案。我实际上添加了一个带有睡眠时间的 Thread((。我不知道这是否是最好的答案,但它有效。
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(500);
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
listMedic.performItemClick(
getViewByPosition(position), position, adapter.getItemId(position));
} catch (NullPointerException e) {
e.printStackTrace();
}
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();