开发者

Weird graphical bug with GridView in android

开发者 https://www.devze.com 2023-03-13 03:37 出处:网络
What I have: A gridview with a buttonadapter. Here\'s the code of adapter public class ButtonAdapter extends BaseAdapter {

What I have: A gridview with a buttonadapter. Here's the code of adapter

public class ButtonAdapter extends BaseAdapter {  

     static List<Button> button = new ArrayList<Button>();
    private Context mContext;  

    // Gets the context so it can be used later  
    public ButtonAdapter(Context c) {  
     mContext = c;  

    }  

    public int getCount() {  
     return my.package.names.length;  
    }  

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

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

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

     if (convertView == null) {  

         button.add(new Button(mContext));
         button.get(position).setLayoutParams(new GridView.LayoutParams(85, 85));  
         button.get(position).setPadding(8, 8, 8, 8);  
         bu开发者_开发问答tton.get(position).setOnClickListener(new MyOnClickListener(position)); 
      }  
     else {  

     }    
 button.get(position).setText(my.package.names[position]);  // names is an array of strings  
     button.get(position).setTextColor(Color.BLACK);  
     button.get(position).setBackgroundResource(*Drawable here*);    
     return button.get(position);  
    }  

   }  

The problem: When touching the button on the screen and moving finger to uperright or uperleft or downleft or downright corner my button resizes and becomes as this (imagege below):

Weird graphical bug with GridView in android

OnClickListener class, pretty simple

public class MyOnClickListener implements OnClickListener  
{  
 private final int position;  
  int i;
 public MyOnClickListener(int position)  
 {  
  this.position = position;  
 }  

 public void onClick(View v)  
 {  

  // Preform a function based on the position  
for(i=0; i<9; i++){
    if(this.position == i){
    mypackagemyclass.flipper.setDisplayedChild(i); //viewflipper
     mypackagemyclass.slider.open(); //sliding drawer

}
 }  
}}  

I tried the same with an imageadapter, same problem. In emulator and on real device. I've searched everywhere and it seems noone else had this problem before. What can I do with this?


I wouldn't store your buttons in that button list. (Static there is suspicious too).

Android caches views for you in lists and grids and reuses those that have scrolled off-screen, which will be more efficient if your grid gets big.

I'd suggest that your getView() should look more like this (untested):

public View getView(int position, View convertView, ViewGroup parent) {  
    Button button;
    if (convertView == null) {
        // create button
        button = new Button(mContext);
        button.setLayoutParams(new GridView.LayoutParams(85, 85));  
        button.setPadding(8, 8, 8, 8);  
        button.setTextColor(Color.BLACK);  
    }
    else {
        // reuse button
        button = (Button)convertView;
    }
    // (re)initialise button for this position
    button.setOnClickListener(new MyOnClickListener(position)); 
    button.setText(my.package.names[position]);  // names is an array of strings  
    button.setBackgroundResource(*Drawable here*);    
    return button;
} 

(and delete your button list).

I can't say for certain that it will fix your problem, but it should put you on the same page as everybody else :-)

Or could your problem be in the *Drawable here* code, which you haven't shown us?

0

精彩评论

暂无评论...
验证码 换一张
取 消