简体   繁体   中英

Android: Customcan GridView not work well when it set both setOnItemClickListener() and setOnItemLongClickListener() method

there is a custom gridView:

public class MyGridView extends GridView {

    public MyGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }
    public MyGridView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }
    public MyGridView(Context context) {
        super(context);

    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

    }
}

then set setOnItemClickListener() and setOnItemLongClickListener() method for this gridView, in onItemLongClick() I return true . befor invoke setOnItemLongClickListener() method the setOnItemClickListener() work very well,but after invoke setOnItemLongClickListener() method once time ,both setOnItemClickListener() and setOnItemLongClickListener() can not work. No matter what the operation is no response

anybody can help me? thanks very much!

If you set the following listeners in your Fragment or Activity after you instantiate the gridview, you should be able to see the log statements in LogCat when you click or long-click on an item in your gridview. If you have done this, I'd first try the code with a generic gridview (not the custom one) and make sure that works. That also means your adapter needs to be setup correctly so that items are populated in the gridview. These listeners won't do anything if there are no items in the gridview.

If this doesn't solve it, then I'd suggest posting your Fragment/Activity code with the Adapter code and possibly also the XML you are using for your Fragment/Activity layout so we can see where else the problem might be.

I've tested the below code with one of my gridviews and it works just fine.

@Override
public void onResume() {
    super.onResume();

    MyGridView gridView = (MyGridView) getView().findViewById(R.id.gridview);

    ...

    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            Log.v(TAG,"CLICK: ItemClick");              
        }           
    });

    gridView.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                int position, long id) {
            Log.v(TAG,"CLICK: LongClick");
            return false;
        }           
    });

Check this

MyGridView gridView = (MyGridView) getView().findViewById(R.id.gridview);

gridView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        Log.v(TAG,"CLICK: ItemClick");              
    }           
});

gridView.setOnItemLongClickListener(new OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view,
            int position, long id) {
        Log.v(TAG,"CLICK: LongClick");
        return true;
    }           
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM