长按show button按下android gridview中的网格项

时间:2014-05-30 11:45:16

标签: android gridview hide

我在这里使用以下代码。现在我的网格显示长按下载按钮。如果我长按另一个项目它显示按钮。但问题是前一个按钮在那里。我想隐藏旧的。(隐藏旧的下载按钮新的长按另一个网格项目。)

我该怎么办?请检查一下并给我一个实现这部分的想法吗?

   bookGrid.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                final int position, long id) {                  
            downloadImg=(ImageView) view.findViewById(R.id.download);       
            shareImg=(ImageView) view.findViewById(R.id.share);     
            downloadImg.setVisibility(View.VISIBLE);        
            shareImg.setVisibility(View.VISIBLE);       

    }}      

提前完成。

4 个答案:

答案 0 :(得分:0)

这可能有助于同样的活动。

grid1.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                final int position, long id) {                  
            downloadImg1=(ImageView) view.findViewById(R.id.download1);       
            shareImg1=(ImageView) view.findViewById(R.id.share1);     
            downloadImg1.setVisibility(View.VISIBLE);        
            shareImg1.setVisibility(View.VISIBLE); 
            downloadImg2=(ImageView) view.findViewById(R.id.download2);       
            shareImg2=(ImageView) view.findViewById(R.id.share2);     
            downloadImg2.setVisibility(View.GONE);        
            shareImg2.setVisibility(View.GONE); 


    }}
grid2.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view,
                    final int position, long id) {                  
                downloadImg1=(ImageView) view.findViewById(R.id.download1);       
                shareImg1=(ImageView) view.findViewById(R.id.share1);     
                downloadImg1.setVisibility(View.GONE);        
                shareImg1.setVisibility(View.GONE);
                downloadImg2=(ImageView) view.findViewById(R.id.download2);       
                shareImg2=(ImageView) view.findViewById(R.id.share2);     
                downloadImg2.setVisibility(View.VISIBLE);        
                shareImg2.setVisibility(View.VISIBLE);

        }}

答案 1 :(得分:0)

如果您使用自定义适配器。您可以在CustomAdapter方法中创建setButtonVisible(int position)。并在getView中检查此设置位置并在视图中显示或隐藏Button。你的方法onClick kwill是这样的:

public boolean onItemLongClick(AdapterView<?> parent, View view,
    final int position, long id) {                  
    adapter.setButtonVisible(position);
    adapter.notifyDataSetChanged();
}

答案 2 :(得分:0)

你可以:

  1. 使用最后一个按钮创建一个字段或最终变量。在onItemLongClick中检查它是否为null然后使其不可见。在onItemLongClick的末尾用当前按钮填充字段变量。 在您的情况下(使用提供的代码,我建议下载和shareImg是可以单击的所有视图共有的最终变量):

    bookGrid.setOnItemLongClickListener(new OnItemLongClickListener(){

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view,
            final int position, long id) {                  
        if (downloading != null) {
            downloadImg.setVisibility(View.INVISIBLE);
        }
        if (shareImg != null) {
            shareImg.setVisibility(View.INVISIBLE);
        }
    
        downloadImg=(ImageView) view.findViewById(R.id.download);       
        shareImg=(ImageView) view.findViewById(R.id.share);     
        downloadImg.setVisibility(View.VISIBLE);        
        shareImg.setVisibility(View.VISIBLE);       
    

    }}

  2. 一段时间后,按钮会消失。但在这种情况下,变量应该是方法范围。例如。 :

        final ImageView downloadImg=(ImageView) view.findViewById(R.id.download);       
        shareImg=(ImageView) view.findViewById(R.id.share);     
        downloadImg.setVisibility(View.VISIBLE);        
        shareImg.setVisibility(View.VISIBLE);    
        downloadImg.postDelayed(new Runnable(){
            @Override
            public void run() {
                downloadImg.setVisibility(View.INVISIBLE);
            }
       }, 1000);
    

答案 3 :(得分:0)

您可以记住以前长按的项目:

private View previousItem;

bookGrid.setOnItemLongClickListener(new OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view,
            final int position, long id) {                  
        if (previousItem != null) {
            downloadImg=(ImageView) previousItem.findViewById(R.id.download);       
            shareImg=(ImageView) previousItem.findViewById(R.id.share);     
            downloadImg.setVisibility(View.GONE);        
            shareImg.setVisibility(View.GONE);       
        }
        downloadImg=(ImageView) view.findViewById(R.id.download);
        shareImg=(ImageView) view.findViewById(R.id.share);
        downloadImg.setVisibility(View.VISIBLE);
        shareImg.setVisibility(View.VISIBLE);
        previousItem = view;
    }
}