Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending AdapterView

i'm trying to make (for learning purposes) my own implementation of a simple AdapterView where items comes from an basic Adapter (ImageAdapter from sdk samples).

Actual code is like this:

    public class MyAdapterView extends AdapterView<ImageAdapter> implements AdapterView.OnItemClickListener{
    private ImageAdapter mAdapter;
    public MyAdapterView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initThings();
    }
    private void initThings(){
        setOnItemClickListener(this);
    }
    @Override
    public ImageAdapter getAdapter() {
        // TODO Auto-generated method stub
        return mAdapter;
    }
    @Override
    public void setAdapter(ImageAdapter adapter) {
        // TODO Auto-generated method stub
        mAdapter=adapter;
        requestLayout();
    }
    View obtainView(int position) {
        View child = mAdapter.getView(position, null, this);
        return child;
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
     super.onLayout(changed, l, t, r, b);
        for(int i=0;i<mAdapter.getCount();i++){
            View child = obtainView(i);
            child.layout(10, 70*i, 70, 70);
            addViewInLayout(child, i, null, true);
        }
        this.invalidate();
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        Log.d("MYEXAMPLES","Clicked an item!");
    }
}

This isn't a coding masterpiece, it just displays a pseudo-listview with pictures. I know i could've used ListView, GridView, Spinner, etc. but i'm relative new to android and i'm trying to figure out some things on it.

Well, the question here is: Why is my onItemClick not firing?

Using the same ImageAdapter with a GridView, everything works ok, but when i use with above class, i get nothing. Inside AdapterView.java there is code for those click, longclick, etc events... so why can't i just fire them? Maybe i'm misunderstanding basic things on how AdapterView works? Should I extend other base classes instead? And why?

Hoping to find more experienced guidance on here, thanks in advance.

like image 559
Ander Webbs Avatar asked Apr 01 '10 21:04

Ander Webbs


People also ask

What is AdapterView in android studio?

AdapterView is a ViewGroup that displays items loaded into an adapter. The most common type of adapter comes from an array-based data source.

Can be used to provide views for an AdapterView?

An AdapterView is a group of widgets (aka view) components in Android that include the ListView, Spinner, and GridView. In general, these are the widgets that provide the selecting capability in the user interface (read about AdapterView widgets here).

How do I get AdapterView in activity?

You need to initialize instance for the Adapter and set to the userlist instead of just passing the unassigned adapter variable. Show activity on this post. Show activity on this post. OnItemClickListener is AdapterView.

How many types of adapters are there in Android?

Android provides several subclasses of Adapter that are useful for retrieving different kinds of data and building views for an AdapterView ( i.e. ListView or GridView). The common adapters are ArrayAdapter,Base Adapter, CursorAdapter, SimpleCursorAdapter,SpinnerAdapter and WrapperListAdapter.


1 Answers

If you take a look into AdapterView's sources, you'll see that the OnItemClickListener gets invoked in a method, called performItemClick:

public boolean performItemClick(View view, int position, long id) {
    if (mOnItemClickListener != null) {
        // ...
        mOnItemClickListener.onItemClick(this, view, position, id);
        return true;
    }
    return false;
}

However, if you search the AdapterView source for where this method is used, you'll see that it isn't called anywhere!

Indeed, if you check the source of the Gallery for example, you'll see that the performItemClick is called within the handling of the onSingleTapUp or onKeyUp events.

If you want to use that, you have to detect when a user clicks somewhere and call performItemClick on your own.

like image 143
Shade Avatar answered Sep 20 '22 21:09

Shade