4

这是我认为准确的 buttonAdapter 类:

package com.example.test;

import android.content.Context; 
import android.graphics.Color; 
import android.view.View; 
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button; 
import android.widget.GridView;

public class ButtonAdapter extends BaseAdapter { 
    private Context mContext; 
    public String [] fName = { "File 1", "File 2", "Roflcopters"};

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

    // Total number of things contained within the adapter 
    public int getCount () { return 8; }

    // Require for structure, not really used in my code. 
    public Object getItem (int position) {  return null; }

    // Require for structure, not really used in my code. Can be used to get the id of an item in the adapter for manual control. 
    public long getItemId (int position) { return position; }

    public View getView (int position, View convertView, ViewGroup parent){     
        Button btn;         
        if (convertView == null) {      // if it's not recycled, initialize some attributes      
            btn = new Button (mContext);
            btn.setLayoutParams (new GridView.LayoutParams (190, 190));
            btn.setPadding (1, 1, 1, 1);    
        } else {        
            btn = (Button) convertView;
        }       
      // btn.setText(filesnames[position]);   
      // filenames is an array of strings    
      //btn.setTextColor (Color.WHITE);
      //btn.setBackgroundResource (R.drawable.sample_2);  
      //btn.setBackgroundColor (Color.BLACK);     
      btn.setHighlightColor(Color.GREEN);    
      btn.setId (position);

      return btn; 
    } 
}

这是我的家庭课。我无法让 onItemClick 工作。我在这里做错了什么:

package com.example.test;

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View;
import android.view.View.OnClickListener; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.GridView; 
import android.widget.Toast;

public class home extends Activity implements OnItemClickListener {

    public final static String EXTRA_MESSAGE1 = "com.example.text.MESSAGE";

    public void onCreate (Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);     
        setContentView (R.layout.activity_home);    

        GridView gridview = (GridView) findViewById (R.id.gridview);
        gridview.setAdapter (new ButtonAdapter (this));     

        /*gridview.setOnItemClickListener (new OnItemClickListener () {
           public void onItemClick (AdapterView <?> parent, View v, int position, long id) {          
            Toast.makeText (home.this, "" + position, Toast.LENGTH_LONG).show ();
            Intent intent = new Intent (this, alarm.class);       
            String message = "Position:" + position;            
            intent.putExtra(EXTRA_MESSAGE1, message);        
            startActivity (intent);         
            }     
        });   
     * */    
     }

    @Override 
    public void onItemClick (AdapterView <?> parent, View v, int position, long id) {  
        Intent intent = new Intent (this, alarm.class);
        String message = "Position:" + position;        
        intent.putExtra(EXTRA_MESSAGE1, message);        
        startActivity (intent);  
    }
}

onItemClick 不起作用,注释的“setOnItemClickListener”也不起作用,当它没有被注释掉并且“onItemClick”被注释时。我究竟做错了什么?

4

4 回答 4

5

如果GridView, ListView有像 BUtton 这样的可点击控件,则onItemClick不会触发。
您需要在适配器的方法中Button Click listener实现 。getView

喜欢

public View getView(int position, View convertView, ViewGroup parent) {
    Button btn;
    if (convertView == null) { // if it's not recycled, initialize some
                                // attributes btn = new Button (mContext);
        btn.setLayoutParams(new GridView.LayoutParams(190, 190));
        btn.setPadding(1, 1, 1, 1);
    } else {
        btn = (Button) convertView;
    } // btn.setText(filesnames[position]); // filenames is an array of
        // strings //btn.setTextColor (Color.WHITE);
    // btn.setBackgroundResource (R.drawable.sample_2);
    // btn.setBackgroundColor (Color.BLACK);
    btn.setHighlightColor(Color.GREEN);
    btn.setId(position);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Handle the click here

        }
    });
    return btn;

}
于 2012-08-13T03:22:31.313 回答
3

You can add this line in the root layout of GridView items:

android:descendantFocusability="blocksDescendants"

Then onItemClickListener.onItemClick() will not fire when you tap on subviews which OnClickListener has been defined for them separately.

于 2015-07-27T06:25:53.020 回答
0

我已经测试过 Set Button.onClickListener() (在 API 15 中)不会解决问题。

因此,如果 GridView 包含可点击的视图,它将不会触发 onItemClick。

您可以使用 ImageView 代替 Button。

于 2013-02-05T02:31:47.247 回答
-1

当我试图在充满按钮的地方实现时,我遇到了同样的onitemclick问题gridview。因为按钮是偷每个空间的焦点gridview,你必须给按钮充气android:focusable="false"。但是,该按钮几乎占据了网格内的整个空间,因此您必须单击按钮的最边缘才能触发onitemclick回调。我建议您可以设置onclick或使用图像并将其设计为按钮。

于 2014-03-13T08:46:57.280 回答