2014-09-03 58 views
0

我找不到解決我的問題。我有一個與佈局的網格視圖。這些佈局包含一個imageview和一個textview。我需要的是在觸摸某個項目時爲圖像視圖設置動畫。我如何檢測gridview中的項目keydown事件?Android GridView項目感動聽衆

這裏是我的適配器:

public class GridViewAdapter extends BaseAdapter { 
    private ArrayList<SingleButton> buttons; 
    private MainActivity mainActivity; 

    public GridViewAdapter(MainActivity mainActivity) { 
     this.mainActivity = mainActivity; 
     this.buttons = new ArrayList<SingleButton>(); 

     Resources res = mainActivity.getApplicationContext().getResources(); 
     String[] temp = res.getStringArray(R.array.buttons); 
     int[] buttonImages = { R.drawable.pic1, R.drawable.pic2, 
       R.drawable.pic3, R.drawable.pic4, R.drawable.pic5, 
       R.drawable.pic6, R.drawable.pic7, R.drawable.pic8, 
       R.drawable.pic9 }; 

     for (int count = 0; count < 9; ++count) { 
      this.buttons 
        .add(new SingleButton(buttonImages[count], temp[count])); 
     } 
    } 

    @Override 
    public int getCount() { 
     return this.buttons.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return this.buttons.get(position); 
    } 

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

    @SuppressLint("ViewHolder") 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView; 
     ViewHolder holder = null; 

     if (row == null) { 
      LayoutInflater layoutInflater = (LayoutInflater) mainActivity 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = layoutInflater.inflate(R.layout.single_button, parent, false); 
      holder = new ViewHolder(row); 
      row.setTag(holder); 
     } else { 
      holder = (ViewHolder) row.getTag(); 
     } 

     holder.buttonImage.setImageResource(buttons.get(position).image); 
     holder.buttonText.setText(buttons.get(position).buttonText); 

     return row; 
    } 

    class ViewHolder { 
     ImageView buttonImage; 
     TextView buttonText; 

     public ViewHolder(View v) { 
      this.buttonImage = (ImageView) v.findViewById(R.id.imageViewButton); 
      this.buttonText = (TextView) v.findViewById(R.id.textViewButton); 
     } 
    } 

    class SingleButton { 
     int image; 
     String buttonText; 

     SingleButton(int image, String buttonText) { 
      this.image = image; 
      this.buttonText = buttonText; 
     } 
    } 
+0

分享您的適配器代碼。您需要更改爲getView – 2014-09-03 08:56:31

+0

我編輯了我的帖子:) – definera 2014-09-03 08:58:19

回答

0

您需要添加View.OnTouchListener父視圖,在你的圖像。這裏父視圖是你的網格行。

它可以通過使用setOnTouchListener(View.OnTouchListener)完成。以下是代碼,您需要用下面的代碼替換ViewHolder類。

class ViewHolder implements View.OnTouchListener { 
    ImageView buttonImage; 
    TextView buttonText; 

    public ViewHolder(View v) { 
     this.buttonImage = (ImageView) v.findViewById(R.id.imageViewButton); 
     this.buttonText = (TextView) v.findViewById(R.id.textViewButton); 
     v.setOnTouchListener(this); 
    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     if (MotionEvent.ACTION_DOWN == event.getAction()) { 
      // Here you can put animation for your image view 
      // this.buttonImage is the target image view. 
     } 
     return true; 
    } 
} 
+0

是的,它的工作,但onItemClick()沒有調用。我需要知道哪個項目已被點擊。 – definera 2014-09-03 10:43:02

+0

將你的代碼分享到你調用這個適配器並在onItemClick()上使用。 – 2014-09-03 11:26:45