【问题标题】:Gmail Android App like ActionView/Spinner (NAVIGATION_MODE_LIST)Gmail Android 应用,例如 ActionView/Spinner (NAVIGATION_MODE_LIST)
【发布时间】:2013-03-25 02:20:21
【问题描述】:

我正在尝试实现类似 gmail Android 应用程序的 NAVIGATION_MODE_LIST。 我的主要问题是从微调器列表中隐藏当前选定的项目。 因此,例如,如 中所示,如果您选择已发送,那么它只会在微调器中显示其他元素。

我的理解是它是一个自定义的 ActionView,而不是使用带有自定义适配器的 NAVIGATION_MODE_LIST。

【问题讨论】:

  • 在这个问题link找到了部分解决方案

标签: android-actionbar actionbarsherlock


【解决方案1】:

如果其他人正在寻找这个问题的解决方案,那就是,

这是在link的帮助下编写的示例代码

使用以下代码创建您的适配器并将其加入 ActionBar 列表导航

ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

    itemArr = getResources().getStringArray(R.array.array_spinner_items);
    items = toArrayList(itemArr, null);

    navigationAdapter = new CustomAdapter(this, R.layout.navigation_item_layout, items);
    actionBar.setListNavigationCallbacks(navigationAdapter, this);
    actionBar.setDisplayShowTitleEnabled(false);

扩展BaseAdapterArrayAdapterimplement SpinnerAdapter

在您的适配器中覆盖 getDropdownView,它负责下拉列表中的单个项目视图 并覆盖负责出现在 ActionBar 中的视图的 getView

`public class CustomAdapter extends ArrayAdapter implements SpinnerAdapter {

    Context context;
    int textViewResourceId;
    ArrayList<String> arrayList;

    public CustomAdapter(Context context, int textViewResourceId,  ArrayList<String> arrayList) {
        super(context, textViewResourceId, arrayList);

        this.context = context;
        this.textViewResourceId = textViewResourceId;
        this.arrayList = arrayList;

    }

    @Override
     public View getDropDownView(int position, View convertView, ViewGroup parent){
       if (convertView == null)
       {
         LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         //convertView = vi.inflate(android.R.layout.simple_spinner_dropdown_item, null);
         convertView = vi.inflate(R.layout.navigation_item_layout, null);
       }

       TextView textView = (TextView) convertView.findViewById(R.id.navigation_item);
       textView.setText(arrayList.get(position).toString());//after changing from ArrayList<String> to ArrayList<Object>

       if (position  == curitem) { 
          textView.setHeight(0);
      }
      else{
        textView.setHeight(60);
      }

       return convertView;
     }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.navigation_item_layout, null);
        }
        TextView textview = (TextView) convertView.findViewById(R.id.navigation_item);
        textview.setText(itemArr[position].toUpperCase());
        textview.setTextColor(Color.RED);
        return convertView;
    }

}`

这里是微调项navigation_tem_layout.xml的布局文件

<?xml version="1.0" encoding="utf-8"?>
<TextView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/navigation_item" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:padding="10dp" />

【讨论】:

    【解决方案2】:

    您可能无法在导航列表中提供当前条目,而只是在 getView() 方法中的自定义 SpinnerAdapter 中显示它。

    我刚刚写了一篇包含完整源代码的帖子,但我的示例使用静态类型数组 - 您可以将其更改为使用自定义 NavigationListItem 类(或任何您想要调用的类)并为每个活动构建一个动态列表,以便不包括当前的。您需要小心,因为微调器会在启动时尝试选择第一个条目,但您可以在 getView() 中显示您想要的内容,而不是使用 position 提供的条目。

    dandar3.blogspot.com/2013/03/actionbarsherlock-custom-list-navigation.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-18
      • 2011-06-22
      • 2019-10-24
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      相关资源
      最近更新 更多