I try to use ListView with two ImageVies. First is some photo and second delete photo button.
I try to handle wich item I click but always get same id that different from both of ImageViews. I also try to do this in Adapter class, but found that is not whery nice solution.
May be you can say, where am I wrong?
List update/init part
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 item
<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>
Custom List Adapter
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;
}
}