setonItemLongClickListener未将属性设置为自定义列表视图中的右侧位置

时间:2018-04-13 14:25:07

标签: java android listview onitemlongclicklistener

说我的清单中有5个项目。如果我长按一个项目,会出现一个Toast消息,给出我按下的位置是正确的,但是我有一个ImageView,我希望一旦我在那个位置长按一个项目就可以看到它。问题是ImageView总是出现在列表的底部而不是我选择的位置。这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setTitle(Html.fromHtml("<small>" + GalleryActivity.item + "</small>"));

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });




    listOfImages = (ListView) findViewById(R.id.listOfImages);
    customAdaptor = new CustomAdaptor();
    listOfImages.setAdapter(customAdaptor);
    listOfImages.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            Toast.makeText(ImageActivity.this, "You pressed at position " + position, Toast.LENGTH_SHORT).show();

            delete.setClickable(true);
            delete.setVisibility(View.VISIBLE);

            return true;
        }
    });

}
class CustomAdaptor extends BaseAdapter {
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final View view = getLayoutInflater().inflate(R.layout.custom_layout, null);

        imageView = (ImageView) view.findViewById(R.id.customImage);
        delete = (ImageView) view.findViewById(R.id.delete);
        test = (TextView) view.findViewById(R.id.test);

        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        loadImageFromUrl(images.get(position));




        return view;
    }


}

enter image description here

1 个答案:

答案 0 :(得分:2)

对象delete不包含当前位置图像,因为您没有告诉他这样做。 delete包含您对他影响的最后内容 - &gt;列表的最后一张图片。

你需要在听众内部获得正确的图像,然后使其可见

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

        Toast.makeText(ImageActivity.this, "You pressed at position " + position, Toast.LENGTH_SHORT).show();

        delete = view.findViewById(R.id.delete); //select the right image

        delete.setClickable(true);
        delete.setVisibility(View.VISIBLE);
        return true;
    }
});