2

我正在开发一个 Android 应用程序,它从互联网上检索一些图像并将它们放在 ListView 上。单击其中一个图像时,会创建另一个活动,其中包含有关该图像的一些详细信息。问题是我无法跟踪列表上的点击。

活动.java

public class MyActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        // List<FlickerPicture> pictures = ...
        ListView listView = (ListView) findViewById(android.R.id.list);
        listView.setOnItemClickListener(new OnItemClickListener() {
             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                 Log.d("MyActivity", "You clicked on " + arg2);
                 Toast.makeText(getBaseContext(), "You clicked on " + arg2,
                 Toast.LENGTH_LONG).show();
             }
         });
        ArrayAdapter<FlickerPicture> adapter = new FlickrItemAdapter(this, pictures);
        listView.setAdapter(adapter);
    }
}

FlickrItemAdapter

public class FlickrItemAdapter extends ArrayAdapter<FlickerPicture> {
    public FlickrItemAdapter(Activity activity, List<FlickerPicture> pictures) {
        super(activity, 0, pictures);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Activity activity = (Activity) getContext();
        LayoutInflater inflater = activity.getLayoutInflater();
        // Inflate the views from XML
        View rowView = inflater.inflate(R.layout.miniature_layout, null);
        FlickerPicture picture = getItem(position);
        // Load the image and set it on the ImageView
        ImageButton imageButton = (ImageButton) rowView.findViewById(R.id.miniature_picture);
        try {
            imageButton.setImageBitmap(Util.loadBitmap(picture.getMiniatureLink()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return rowView;
    }
}

问题

如何使列表中的图像“可点击”?

4

1 回答 1

1

您使用的是 ImageButton 而不是 ImageView。只需更改miniature_layout.xml您的适配器getView()以使用 ImageView,因为 ImageButton 正在消耗单击事件,从而阻止它到达 ListView。

于 2012-11-10T23:02:35.453 回答