在ItemClick上获取错误的id



我尝试将ListView与两个ImageVie一起使用。第一个是一些照片,第二个是删除照片按钮。

我试图处理我点击的哪个项目,但总是得到与两个ImageView不同的相同id。我也尝试在Adapter类中这样做,但发现这不是一个很好的解决方案。

也许你可以说,我错在哪里了?

列表更新/初始化部分

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;
    }
}

获取与两个ImageViews不同的相同id。

因为你有row-id视图,它与他们孩子的视图不同。因此在您的getView中使用以下代码:

ImageView delete = (ImageView) rowView.findViewById(R.id.image_delete);
delete.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // do your work
    }
});

相关内容

  • 没有找到相关文章

最新更新