我尝试将ListView与两个ImageV一起使用。首先是一些照片和第二个删除照片按钮。
我尝试处理我点击的项目,但总是获得与两个ImageView不同的id。 我也尝试在Adapter类中做到这一点,但发现这不是whery不错的解决方案。
可能你可以说,我哪里错了?
列出更新/初始化部分
public void updateList(final ArrayList<String> arrayList) {
CustomList adapter = new
CustomList(FullInfoActivity.this, arrayList);
HorizontalListView list = (HorizontalListView) findViewById(R.id.HorizontalListView);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.e("Parent", String.valueOf(parent.getId()) +" " +R.id.img + " "+R.id.image_delete + " "+parent.getItemAtPosition(position));
switch (parent.getId()) {
case R.id.img:
Toast.makeText(getApplicationContext(), "111111111", Toast.LENGTH_SHORT).show();
break;
case R.id.image_delete:
Toast.makeText(getApplicationContext(), "222222222", Toast.LENGTH_SHORT).show();
break;
}
ListView项目
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/img"
android:layout_width="75dp"
android:layout_height="75dp"
android:scaleType="centerCrop"
android:visibility="invisible"
android:layout_marginRight="8dp"
android:clickable="true"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:contentDescription="image" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/img"
android:layout_alignStart="@+id/img"
android:layout_alignRight="@+id/img"
android:layout_alignEnd="@+id/img"
android:visibility="visible" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="@+id/image_delete"
android:src="@drawable/ic_action_cancel_light"
android:layout_alignTop="@+id/img"
android:layout_alignRight="@+id/img"
android:layout_alignEnd="@+id/img"
android:background="#7f989e9a"
android:clickable="true"
android:contentDescription="Cancel" />
</RelativeLayout>
自定义列表适配器
public class CustomList extends ArrayAdapter<String> {
private final Activity context;
private final ArrayList<String> arrayList;
public CustomList(Activity context, ArrayList<String> arrayList) {
super(context, R.layout.list_single, arrayList);
this.context = context;
this.arrayList = arrayList;
}
@Override
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_single, null, true);
final ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
final ProgressBar pb = (ProgressBar) rowView.findViewById(R.id.progressBar);
final Animation show = AnimationUtils.loadAnimation(context, R.anim.appear_animation);
ImageView delete = (ImageView) rowView.findViewById(R.id.image_delete);
final Timer myTimer = new Timer(); // Создаем таймер
myTimer.schedule(new TimerTask() { // Определяем задачу
@Override
public void run() {
File f = new File(arrayList.get(position));
if (f.exists()) {
final Bitmap myBitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
if (myBitmap != null) {
myTimer.cancel();
context.runOnUiThread(new Runnable() {
@Override
public void run() {
pb.setVisibility(View.GONE);
imageView.setImageBitmap(Bitmap.createScaledBitmap(myBitmap, 120, 120, false));
imageView.setVisibility(View.VISIBLE);
imageView.startAnimation(show);
}
});
}
}
}
}, 100, 100);
return rowView;
}
}
答案 0 :(得分:1)
获取与两个ImageView不同的id。
因为你有行ID视图,它与他们孩子的视图不同。所以在getView
使用下面的代码:
ImageView delete = (ImageView) rowView.findViewById(R.id.image_delete);
delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// do your work
}
});