简体   繁体   English

Android Spinner onItemSelected未在Fragment中调用

[英]Android Spinner onItemSelected not being called inside Fragment

I have an activity which calls several fragments. 我有一个调用几个片段的活动。 In one of those fragments I am trying to create a dialog spinner programmatically and add it to the menu option (in toolbar). 在其中一个片段中,我试图以编程方式创建一个对话框微调器并将其添加到菜单选项(在工具栏中)。 I manage to make it work (atleast the view is showing), but the onItemSelected it is not getting called. 我设法让它工作(至少是视图显示),但是onItemSelected它没有被调用。

My code: 我的代码:

public class NewsFeed extends Fragment {
private static final String TAG = "tag";
private String tag;
private ArrayAdapter<New> newsadapter;
private ArrayAdapter<Tag> tagsadapter;
private Spinner spinner;

public NewsFeed() {
    setHasOptionsMenu(true);
}

public static NewsFeed newInstance(String tag) {
    NewsFeed fragment = new NewsFeed();
    Bundle args = new Bundle();
    args.putString(TAG, tag);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        tag = getArguments().getString(TAG);
    }
    setHasOptionsMenu(true);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    getActivity().setTitle(tag);
    NavigationView activitynav = (NavigationView) getActivity().findViewById(R.id.nav_view);
    BottomNavigationView activitybuttomnav = (BottomNavigationView) getActivity().findViewById(R.id.navigation);
    activitynav.setCheckedItem(R.id.nav_news);
    activitybuttomnav.getMenu().getItem(1).setChecked(true);
    View v = inflater.inflate(R.layout.fragment_news_feed, container, false);

    //Spinner related code      
    tagsadapter = new TagsAdapter(getActivity(), android.R.layout.simple_spinner_item, (ArrayList<Tag>) ApplicationData.tags);
    spinner = new Spinner(getActivity(), Spinner.MODE_DIALOG);
    tagsadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setPrompt("Filtrar por categoria:");
    spinner.setAdapter(tagsadapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    newsadapter = new NewsAdapter(getActivity().getApplicationContext(), 0, (ArrayList<New>) ApplicationData.news);
    ListView listview = (ListView) v.findViewById(R.id.listview);
    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
            FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
            NewsDisplay nd = NewsDisplay.newInstance(tag, ApplicationData.news.get(position).getNewsUrl());
            ft.addToBackStack(null);
            ft.replace(R.id.fragmentcontent, nd).commit();
        }
    });
    listview.setAdapter(newsadapter);
    return v;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.main, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.filter_category) {
         //For showing the spinner
        spinner.performClick();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

} }

Already tried: 已经尝试过:

To make NewsFeed implementing OnItemSelectedListener. 使NewsFeed实现OnItemSelectedListener。

To create the OnItemSelectedListener object inside SetOnItemSelectedListener. 在SetOnItemSelectedListener中创建OnItemSelectedListener对象。

Could you give me some help? 你能给我一些帮助吗? It might have to do with this being a fragment and the toolbar is in the activity. 它可能与此片段相关,工具栏位于活动中。

Spinner that was created dynamically ( private val popupSpinner: Spinner by lazy { Spinner(this, Spinner.MODE_DIALOG) } ), needs to have an assigned parent view. 动态创建的Spinnerprivate val popupSpinner: Spinner by lazy { Spinner(this, Spinner.MODE_DIALOG) } )需要有一个指定的父视图。 If it does not have it, the OnItemSelected is never called for some reason. 如果它没有它, OnItemSelected永远不会因某种原因被调用。

I wrote this hack to alleviate the problem. 我写了这个黑客来缓解这个问题。 Added to parent view and hid it. 添加到父视图并隐藏它。 Be sure not to run this in onResume() method and similar ones. 一定不要在onResume()方法和类似的方法中运行它。

findViewById<ViewGroup>(android.R.id.content).addView(popupSpinner)
popupSpinner.visibility = View.INVISIBLE

PS. PS。 Some people might think that this question does not have an answer. 有些人可能会认为这个问题没有答案。 But in the comments the author himself found a solution. 但在评论中,作者自己找到了解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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