3

How can I change view of item in drop down navigation list in action bar on click? After click, I want that displayed item shows another view (in layout it is set as invisible). I've got onNavigationItemSelected method from ActionBar.OnNavigationListener but it does not pass view of the item clicked but just position of item in navigation spinner. Or in other words, why can I not get reference to selected view from actionbar spinner? (like I can get view with listview onItemClick from OnItemClickListener())

4

3 回答 3

2

ActionBarSpinner资源 id 是:android:id/action_bar_spinner

获取Spinner资源id:

int resId = getResources().getIdentifier("action_bar_spinner", "id", "android");

Spinner使用资源 id获取对小部件的引用:

final Spinner spinner = (Spinner) getWindow().getDecorView().findViewById(resId);

现在您将能够访问所选视图:

CheckedTextView v = (CheckedTextView) spinner.getSelectedView();

您可以立即在以下位置修改所选视图OnNavigationListener

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getFragmentManager().beginTransaction()
    .replace(android.R.id.content, new MyFragment())
    .commit();

    SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this,
            R.array.action_list, android.R.layout.simple_spinner_dropdown_item);

    getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

    int resId = getResources().getIdentifier("action_bar_spinner", "id", "android");
    final Spinner spinner = (Spinner) getWindow().getDecorView().findViewById(resId);

    this.getActionBar().setListNavigationCallbacks(mSpinnerAdapter, new ActionBar.OnNavigationListener() {

        @Override
        public boolean onNavigationItemSelected(int itemPosition,
                long itemId) {
            CheckedTextView v = (CheckedTextView) spinner.getSelectedView();
            // Modify selected view.
            return true;
        }

    });
}
于 2014-08-19T17:33:05.770 回答
0

我只是想弄清楚这一点并设法追踪它。

用于setSelectedNavigationItem执行此操作。

于 2012-06-30T15:36:28.260 回答
0

使用您的 navigationListener,您可以尝试以下操作:

OnNavigationListener navigationListener = new OnNavigationListener() {

  @Override
  public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    View convertView = null;
    View view = adapter.getView(itemPosition, convertView, null);
    TextView myTextView = (TextView) view
        .findViewById(R.id.my_textView);
    Toast.makeText(getBaseContext(), myTextView.getText(),
        Toast.LENGTH_LONG).show();

    return true;
  }

然后,您将通过适配器检索该项目。我现在唯一的问题是修改检索到的参考内容。我似乎无法让它工作。

我希望它有帮助

于 2012-09-11T13:14:19.200 回答