带有自定义下拉视图的微调器不会触发onItemSelected()

带有自定义下拉视图的微调器不会触发onItemSelected(),第1张

概述我有一个使用自定义适配器的Spinner,其中getDropDownView()被覆盖.自定义下拉视图中的每个项目都由一个TextView和一个Button组成.但是,当我运行代码时,微调器下拉项显示得很好,但是单击它们却无济于事.旋转器下拉菜单保持打开状态,并且未触发spinner.onItemSelected().drop_down

我有一个使用自定义适配器的Spinner,其中getDropDownVIEw()被覆盖.自定义下拉视图中的每个项目都由一个TextVIEw和一个button组成.

但是,当我运行代码时,微调器下拉项显示得很好,但是单击它们却无济于事.旋转器下拉菜单保持打开状态,并且未触发spinner.onItemSelected().

drop_down_item.xml

<relativeLayout     androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content">    <TextVIEw        androID:ID="@+ID/dropdown_text"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:layout_alignParentleft="true"        androID:singleline="true" />    <button        androID:ID="@+ID/dropdown_button"        androID:layout_height="match_parent"        androID:layout_wIDth="wrap_content"        androID:layout_alignParentRight="true"        androID:text="Remove"/></relativeLayout>

自定义适配器代码

public VIEw getDropDownVIEw(final int position, VIEw convertVIEw, VIEwGroup parent) {    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    VIEw rowVIEw = inflater.inflate(R.layout.drop_down_item, parent, false);    TextVIEw textVIEw = (TextVIEw) rowVIEw.findVIEwByID(R.ID.dropdown_text);    textVIEw.setText(mValues.get(position));            button buttonVIEw = (button) rowVIEw.findVIEwByID(R.ID.dropdown_button));   return rowVIEw; }

我使用以下代码创建微调器和适配器:

spinner = (Spinner) findVIEwByID(R.ID.my_spinner);MyAdapter adapter = new MyAdapter(getVIEwContext(), R.layout.spinner_item, values);adapter.setDropDownVIEwResource(R.layout.drop_down_item);spinner.setAdapter(adapter);...spinner.setonItemSelectedListener(new AdapterVIEw.OnItemSelectedListener() {    @OverrIDe    public voID onItemSelected(AdapterVIEw<?> parentVIEw, VIEw selectedItemVIEw, int position, long ID) {        // Do something here - but this never runs    }});

所以我不知道为什么不再调用onItemSelected()?

我想知道是否需要在下拉的TextVIEw上放置一个点击侦听器,然后应该使用spinner.setSelection(pos)触发onItemSelected()?

解决方法:

事件基本上是一个接口,Activity通过单击Spinner的DropDown视图的linearLayout来实现该接口以接收回呼.

public class MyArrayAdapter extends BaseAdapter {String[] values;int Customresource;Context context;Events events;public MyArrayAdapter(Context baseContext, int customspinnervIEw,        String[] stringArray, Events events) {    values = stringArray;    context = baseContext;    this.events = events;    Customresource = customspinnervIEw;}@OverrIDepublic int getCount() {    // Todo auto-generated method stub    return values.length;}@OverrIDepublic Object getItem(int position) {    if (position < values.length)        return values[position];    else {        return null;    }}@OverrIDepublic VIEw getVIEw(final int position, final VIEw convertVIEw,        VIEwGroup parent) {    VIEw rowVIEw = convertVIEw;    LayoutInflater inflater = (LayoutInflater) context            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);    if (rowVIEw == null) {        rowVIEw = inflater.inflate(Customresource, parent, false);    }    TextVIEw textVIEw = (TextVIEw) rowVIEw.findVIEwByID(R.ID.dropdown_text);    textVIEw.setText(values[position]);    button button = (button) rowVIEw.findVIEwByID(R.ID.button_text);    return rowVIEw;}@OverrIDepublic VIEw getDropDownVIEw(final int position, VIEw convertVIEw,        VIEwGroup parent) {    LayoutInflater inflater = (LayoutInflater) context            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);    VIEw rowVIEw = convertVIEw;`enter code here`    if (rowVIEw == null) {        rowVIEw = inflater.inflate(Customresource, parent, false);    }    final linearLayout parentrelative = (linearLayout) rowVIEw            .findVIEwByID(R.ID.parent);    final TextVIEw textVIEw = (TextVIEw) rowVIEw            .findVIEwByID(R.ID.dropdown_text);    textVIEw.setText(values[position]);    button button = (button) rowVIEw.findVIEwByID(R.ID.button_text);    rowVIEw.setonClickListener(new OnClickListener() {        @OverrIDe        public voID onClick(VIEw v) {            events.onItemSelectedLister(                    (AdapterVIEw<?>) parentrelative.getParent(),                    parentrelative, position, (long) 0);        }    });    // button buttonVIEw = (button)    // rowVIEw.findVIEwByID(R.ID.dropdown_button);    return rowVIEw;}

事件接口它是Activity所实现的接口,以从适配器接收回调.

import androID.vIEw.VIEw;import androID.Widget.AdapterVIEw;public interface Events {public voID onItemSelectedLister(AdapterVIEw<?> parent, VIEw vIEw,        int position, long ID);}

活动实施.

onItemSelected实现是您可以执行任务的地方.

import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import androID.annotation.TargetAPI;import androID.app.Activity;import androID.os.Build;import androID.os.Bundle;import androID.vIEw.Menu;import androID.vIEw.MenuItem;import androID.vIEw.VIEw;import androID.Widget.AdapterVIEw;import androID.Widget.Spinner;import com.example.adapter.MyArrayAdapter;public class MainActivity extends Activity implements Events {Spinner spinner;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    spinner = (Spinner) findVIEwByID(R.ID.spinner);    spinner.setAdapter(new MyArrayAdapter(getBaseContext(),            R.layout.customspinnervIEw, getResources().getStringArray(                    R.array.values), this));}@OverrIDepublic voID onItemSelectedLister(AdapterVIEw<?> parent, VIEw vIEw,        final int position, long ID) {        //perform your Task.......    Method method;    try {        method = Spinner.class.getDeclaredMethod("onDetachedFromWindow");        method.setAccessible(true);        try {            method.invoke(spinner);        } catch (illegalaccessexception | IllegalArgumentException                | InvocationTargetException e) {            // Todo auto-generated catch block            e.printstacktrace();        }    } catch (NoSuchMethodException e) {        // Todo auto-generated catch block        e.printstacktrace();    }    spinner.post(new Runnable() {        @OverrIDe        public voID run() {            spinner.setSelection(position);            spinner.setSelected(true);            ((MyArrayAdapter) spinner.getAdapter()).notifyDataSetChanged();        }    });}}

setContentVIEw的活动xml文件

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:tools="http://schemas.androID.com/tools"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:paddingBottom="@dimen/activity_vertical_margin"androID:paddingleft="@dimen/activity_horizontal_margin"androID:paddingRight="@dimen/activity_horizontal_margin"androID:paddingtop="@dimen/activity_vertical_margin"tools:context="com.example.customspinner.MainActivity" ><Spinner    androID:ID="@+ID/spinner"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content" ></Spinner></relativeLayout>

Spinner VIEw,它作为布局文件传递到Adapter.

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="match_parent"androID:layout_height="wrap_content"androID:background="#99000000"androID:ID="@+ID/parent"androID:orIEntation="horizontal"><TextVIEw    androID:ID="@+ID/dropdown_text"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"/><button      androID:ID="@+ID/button_text"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:text="remove"/></linearLayout>

该代码可以完美运行:).我可以完美运行代码.

总结

以上是内存溢出为你收集整理的带有自定义下拉视图的微调器不会触发onItemSelected()全部内容,希望文章能够帮你解决带有自定义下拉视图的微调器不会触发onItemSelected()所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1090661.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存