Gmail Android App like ActionView/Spinner (NAVIGATION_MODE_LIST)

廉价感情. 提交于 2019-12-06 10:43:13
Prashant Gami

If someone else is looking for the solution to this problem here it is,

Here is sample code which is written with the help of link

use following code to create your adapter and join it to ActionBar List Navigation

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);

Extend BaseAdapter or ArrayAdapter and implement SpinnerAdapter

In your adapter override getDropdownView which is responsible for individual item view in dropdown and override getView which is responsible for the view appearing in the ActionBar

`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;
    }

}`

Here is the layout file for spinner item 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" />

You may be able to not provide the current entry in the navigation list, and just display it in the custom SpinnerAdapter in getView() method.

I just wrote a post with full source code, but my example uses static typed-arrays - you could change it to use a custom NavigationListItem class (or whatever you want to call it) and build a dynamic list per activity so that doesn't include the current one. You will need to be careful though as the spinner will try to select the first entry upon start, but you can display what you want in getView() as opposed to using the entry provided by position.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!