2012-06-07 36 views
0

這可能很簡單,但我找不到解決方案。問題是在onitemlongclicklistener方法中爲gridview項目設置背景顏色。在android中設置bgcolor for gridview項目的問題

我有兩個線性佈局,一個包含的TextView和GridView和另一包含一些按鈕(I設置該佈局等上的屏幕的頂部的菜單欄)。我使用setVisibility(View.GONE)從屏幕上隱藏了第二個,當我在itemview上單擊gridview項目時,第二個將使用setVisibility(View.VISIBLE)同時顯示,同時我想更改點擊的bgcolor /選定的項目。

一切工作正常,但每當第二個佈局是可見的,也無論何時滾動GridView控件,點擊的/選擇的博客上消失。

我試了很多方法,但我無法解決這個問題。請看看我的編碼,並告訴我我做錯了什麼。

gv.setOnItemLongClickListener(new OnItemLongClickListener() { 
      public boolean onItemLongClick(AdapterView<?> parent, View strings, 
          int position, long id) { 
       final Option o = dir.get(position); 
      //gv.performItemClick(gv, position,gv.getItemIdAtPosition(position)); 

       /* if(pos!=-1) 
       { 
       gv.getChildAt(pos).setBackgroundColor(0xffffffff); 

       }*/ 
       strings.setSelected(true); 
      // TextView tx=(TextView)strings.findViewById(R.id.grid_item_label); 
      // tx.findViewById(id).setBackgroundColor(0xff0000ff); 
      /*if(!o.getData().equalsIgnoreCase("o")){ 


       //gv.getChildAt(position).setSelected(true);  
       fill(new File(new File(o.getPath()).getParent()),position); 

      }*/ 
      /*try 
      { 
       gv.getSelectedView().setBackgroundColor(0xffcccccc); 
      } 
      catch(Exception e){ 
       Toast.makeText(FffsdActivity.this, "ok\n"+e,Toast.LENGTH_SHORT).show(); 

      }*/ 

      /*for(int i=0;i<nofifo;i++) 

       { 
       try{ 
       if(gv.getChildAt(i).isSelected()) 
       { 
         gv.getChildAt(i).setBackgroundColor(0xffbbbbbb); 
         // Toast.makeText(FffsdActivity.this, "yes ok\n"+i,Toast.LENGTH_SHORT).show(); 

       } 
       else{ 
          gv.getChildAt(i).setBackgroundColor(0xffffffff); 
         //Toast.makeText(FffsdActivity.this, "no ok\n"+i,Toast.LENGTH_SHORT).show(); 

       } 
       } 
       catch(Exception e) 
       { 
       Toast.makeText(FffsdActivity.this, "i wanna e :"+e+"\n"+i,Toast.LENGTH_SHORT).show(); 

       } 
       try{ 
       gv.getChildAt(i).setSelected(false); 
        //Toast.makeText(FffsdActivity.this, "gv ok\n"+i,Toast.LENGTH_SHORT).show(); 

       }catch(Exception e){ 
         Toast.makeText(FffsdActivity.this, "i wanna setselected e :"+e+"\n"+i,Toast.LENGTH_SHORT).show(); 

       } 

       }*/ 


      // Toast.makeText(FffsdActivity.this, "i wanna c :"+gv.getChildAt(position).isSelected(),Toast.LENGTH_LONG).show(); 
      //gv.getSelectedView().setBackgroundColor(0xffcccccc);   
      pos=position; 
      if(o.getData().equalsIgnoreCase("parent directory")) 
      { 
       fill(new File(o.getPath()),-1); 
      } 
      else if(o.getData().equalsIgnoreCase("folder")) 
      { 
       llfftools.setVisibility(View.VISIBLE); 
       TranslateAnimation slide = new TranslateAnimation(0, 0,-llfftools.getHeight(),0); 
       slide.setDuration(100); 
       slide.setFillAfter(true); 
       llfftools.startAnimation(slide); 
       //fill(new File(new File(o.getPath()).getParent())); 

       onFolderClick(o); 
       //gv.getChildAt(position).setBackgroundColor(0xffaaaaaa); 


      } 

和我baseadapter是:

public class ImageAdapter extends BaseAdapter { 
private Context context; 
private final List<Option> mobileValues; 



public ImageAdapter(Context context,List<Option> fofivalues) { 
    this.context = context; 
    this.mobileValues = fofivalues; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 

    LayoutInflater inflater = (LayoutInflater) context 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View gridView; 
    //if (convertView == null) { 

     gridView = new View(context); 

     // get layout from mobile.xml 
     gridView = inflater.inflate(R.layout.mobile, null); 



    /*} else { 
     gridView = (View) convertView; 
    }*/ 
    // set value into textview 

       TextView textView = (TextView) gridView.findViewById(R.id.grid_item_label); 
       //to set the max no.of chararcters in textview 
       String iname=(mobileValues.get(position).getName().length()>10)?mobileValues.get(position).getName().substring(0, 9)+"..":mobileValues.get(position).getName(); 
       if(mobileValues.get(position).getPosition()==position&&mobileValues.get(position).getPosition()!=-1) 
        { 
        textView.setText(iname); 
        textView.setTextColor(0xff0000ff); 
        } 
       else{ 
       textView.setText(iname); 
       } 

       // set image based on selected text 
       final ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image); 
       /*gridView.setOnLongClickListener(new OnLongClickListener() { 

        public boolean onLongClick(View v) { 
         gridView.setBackgroundColor(0xffaaaaaa); 
         // TODO Auto-generated method stub 
         return false; 
        } 
       });*/ 
       String mobile = mobileValues.get(position).getData(); 
       if(mobile=="o") 
       { 
       //do nothing 
        imageView.setImageResource(R.drawable.white); 
       } 

       else if(mobile=="Parent Directory") 
       { 
        imageView.setImageResource(R.drawable.parent); 
       } 
       else if (mobile=="Folder") 
       { 
        imageView.setImageResource(R.drawable.folderg); 
       } 
       else{ 
        imageView.setImageResource(R.drawable.picicon); 
       } 



    return gridView; 
} 

public int getCount() { 
    return mobileValues.size(); 
} 

public Object getItem(int position) { 
    return null; 
} 

public long getItemId(int position) { 
    return 0; 
} 

    } 

在此先感謝,如果你想要的東西更多,請讓我知道...

回答

0

在你的XML設置了android:背景屬性的xml是這樣的:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="@drawable/home_btn_sessions_pressed" 
    android:state_focused="true" 
    android:state_pressed="true" /> 
<item android:drawable="@drawable/home_btn_sessions_pressed" 
    android:state_focused="false" 
    android:state_pressed="true" /> 
<item android:drawable="@drawable/home_btn_sessions_selected" android:state_focused="true" /> 
<item android:drawable="@drawable/home_btn_sessions_default" 
    android:state_focused="false" 
    android:state_pressed="false" /> </selector> 

不需要在這裏編寫Java代碼。

+0

嗨安塔爾感謝您的回覆,我已經嘗試過這一個了,但不工作。 –

+0

嗨,我發現我的問題的解決方案,並完美地工作。 –

+0

這是真正幫助我的頁面:http://stackoverflow.com/questions/7530087/how-to-set-a-background-drawable-on-a-clicked-gridview-item-in-android –