I am creating a Spinner instance that I bind to my own custom adapter class (derived from BaseAdapter). In it, I create a bunch of custom “cell” LinearLayout-derived instances that I inflate from xml to be used for each item in the spinner.
Everything looks fine as expected, but whenever the items in “drop down” mode are clicked, the drop down dialog does not get dismissed.
I can trap the click notification inside my cell class, but what is the magic sauce I need to tell the spinner that an item was selected and that it should dismiss the drop down?
My cell class is inflated from the following xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/VIEW_LEAF"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView android:id="@+id/STATIC_LEAF_NAME"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="3dip"
android:paddingTop="20dip"
android:paddingBottom="20dip"
android:textSize="22sp"
android:textStyle="bold"
android:text=""
android:layout_gravity="left|center_vertical"
android:textColor="@color/black_enabled_grey_disabled"
/>
</LinearLayout>
My cell class looks like so:
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;
// This class is responsible for rendering the data in the model.
public class DataCell extends LinearLayout implements OnClickListener
{
private final String TAG = getClass().getSimpleName();
public DataCell(Context context)
{
super(context);
m_context = context;
final boolean attachToRoot = true;
LayoutInflater.from(context).inflate(R.layout.data_cell,
this, attachToRoot);
m_labelName = (TextView) findViewById(R.id.STATIC_LEAF_NAME);
m_labelName.setOnClickListener(this);
}
// Update the views with the data corresponding to selection index.
// If dropDown is true, this view is to be displayed in a dropped down
// list.
public void display(int index, ViewGroup parent, Data data, boolean dropDown)
{
m_data = data;
m_labelName.setText(data.getFriendlyName());
setFocusable(true);
}
@Override
public void onClick(View view)
{
ViewParent parent = getParent();
if(parent instanceof Spinner)
{
//((Spinner)parent).setSelection(2);// TODO something here?
}
}
private TextView m_labelName = null;
@SuppressWarnings("unused")
private Context m_context = null;
@SuppressWarnings("unused")
private Data m_data = null;
}
Thanks!
swine
So you were right about the cell stealing the clicks. But there was a very simple solution to all this, just not very obvious. All I had to do is make the TextView inside my DataCell not respond to clicks.
So I removed the m_labelName.onClickListener() call completely, and instead, at that location, put in the following code: