下拉导航Android

时间:2014-07-15 00:38:36

标签: android android-studio

我是Android开发人员,我想学习如何在我的应用中实现下拉导航。基本上,当用户在操作栏中选择微调器中的项目时,我希望在屏幕上显示不同的布局。

我在Android Studio中使用Dropdown导航模板创建了一个新活动,但我不知道如何继续。我将如何完成这项工作?

2 个答案:

答案 0 :(得分:0)

要在操作栏微调器中具有与微调器列表中不同的视图,可以使用BaseAdapter或ArrayAdapter并覆盖某些方法:

@Override
  public View getView(int position, View convertView, ViewGroup parent) {
    // Return a view which appears in the action bar.

    return yourCustomView..;
  }

  @Override
  public View getDropDownView(int position, View convertView, ViewGroup parent) {
    // Return a view which appears in the spinner list.

    // Ignoring convertView to make things simpler, considering
    // we have different types of views. If the list is long, think twice!
    return super.getView(position, null, parent);
  }

更多具体细节可以在这里找到:https://stackoverflow.com/a/15293471/1650683

====编辑====

您可以在本文中找到如何在ActionBar中集成下拉列表,按照说明操作,您将完成它。

对于bring to a different layout after selecting an item on the spinner,您应该在用户点击下拉项后设置片段。示例代码:

mOnNavigationListener = new OnNavigationListener() {

  // Get the same strings provided for the drop-down's ArrayAdapter
  String[] strings = getResources().getStringArray(R.array.action_list);

  @Override
  public boolean onNavigationItemSelected(int position, long itemId) {
    // Create new fragment from our own Fragment class
    ListContentFragment newFragment = new ListContentFragment();
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

    // Replace whatever is in the fragment container with this fragment
    // and give the fragment a tag name equal to the string at the position
    // selected
    ft.replace(R.id.fragment_container, newFragment, strings[position]);

    // Apply changes
    ft.commit();
    return true;
  }
};

答案 1 :(得分:0)

使用微调器进行下拉。这可能会对您有所帮助。

 ArrayAdapter<String> adapter = new ArrayAdapter<String>
    (this, android.R.layout.simple_spinner_item, array_name); 
    adapter.setDropDownViewResource(R.layout.spinner_layout);
 final Spinner s1 = (Spinner) findViewById(R.id.spinner1);
 s1.setPrompt("Your Title");
 s1.setAdapter(adapter);
 s1.setOnItemSelectedListener(new OnItemSelectedListener()
    {   
       @Override
       public void onItemSelected(AdapterView<?> arg0, View arg1,
            int arg2, long arg3)
            {   
                 --enter your code here--
       }}
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {    
            }
    });