【问题标题】:longClickable on gallery item画廊项目上的 longClickable
【发布时间】:2013-07-11 16:55:16
【问题描述】:

我想要实现的是让用户长按图库视图中的图像以将他们带到网站。

到目前为止我所拥有的就是这个,但似乎无法解决......

 .......
 public int getCount() {
    return imageIDs.length;
 .....
 public View getView(int position, View convertView, ViewGroup parent){
    ImageView imageView;
    if (convertView == null){
        imageView = new ImageView(context);
        imageView.setImageResource(imageIDs[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);


    }else{
        imageView = (ImageView) convertView;
    }
    imageView.setBackgroundResource(itemBackground);
    return imageView;

        imageView.setOnLongClickListener(imageIDs[position]){

            boolean onLongClick(int position,View v) {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse ("http://www.google.com"));
            startActivity(intent);
            return true;
        }
    {
    }
}

 }

收到此错误

 The method setOnLongClickListener(View.OnLongClickListener) in the type View is not applicable  for the arguments 
 (Integer)

任何帮助将不胜感激!

【问题讨论】:

    标签: java android user-interface android-widget


    【解决方案1】:

    该错误会告诉您确切的问题所在。 imageIDs[position] 返回一个 int,而 setOnLongClickListener 采用 OnLongClickListener。比如:

    imageView.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse ("http://www.google.com"));
            startActivity(intent);
            return true;
        }
    });
    

    注意:将 OnItemLongClickListener 切换为 OnLongClickListener

    【讨论】:

    • 感谢您的回复,但仍然没有运气,仍然给我同样的错误。
    • 我想我可以尝试做的是在文本中包含网站地址但屏蔽它。当用户点击文本中的“示例”时,它会将他们带到google.com,换句话说,隐藏 uri 并仅显示“示例”。问题是如何做到这一点?
    • @Keithk 我将答案更改为使用 OnLongClickListener 而不是 OnItemLongClickListener。复制了错误的代码。试试看它是否有效
    猜你喜欢
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多