Custom spinner not showing dropdown in ActionBar icon

倖福魔咒の 提交于 2019-12-13 03:09:03

问题


Trying to get my custom Spinner with only an ImageView (to show a list of icon drawables) to work. I thought I had the custom adapter code correct, and the onCreateOptionsMenu() code correct, but no. The icon for the sharing feature shows in the ActionBar, but when I touch it, the Spinner does not dropdown the menu it should. What could be wrong? This goal is unique, because it is not just a custom spinner (easy to find tutorial on) or just an ActionBar icon placement (also easy), but rather combining them both, which is hard to find resources on.

PhotoViewerActivity.java

package org.azure_simbiosys.cutecollection.phototab;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;

import org.azure_simbiosys.R;

public class PhotoViewerActivity extends ActionBarActivity
                                implements AdapterView.OnItemSelectedListener{

    private ArrayAdapter spinnerAdapter;
    int[] iconList = {R.drawable.ic_facebook, R.drawable.ic_twitter, R.drawable.ic_sms};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_viewer);

        // Shows the up carat near app icon in ActionBar
        getSupportActionBar().setDisplayUseLogoEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        ImageView photoView = (ImageView)findViewById(R.id.photo_display);

        // gets the single bitmap image from the array of bytes
        Bundle extras = getIntent().getExtras();
        byte[] byteArray = extras.getByteArray("photo");
        Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

        // sets device orientation based on the image orientation
        if (bm.getWidth() > bm.getHeight()){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        // sets bitmap into your ImageView
        photoView.setImageBitmap(bm);


    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        // Facebook, Twitter, SMS code here

    }

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

    }

    private class ShareSpinnerAdapter extends ArrayAdapter {

        // constructor
        public ShareSpinnerAdapter (Context context, int iconResourceId){

            super(context, iconResourceId);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            View itemView = convertView;

            return getCustomView(position, convertView, parent);
        }

        @Override
        public View getDropDownView(int position, View convertView,ViewGroup parent) {

            return getCustomView(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView, ViewGroup parent)
        {

            LayoutInflater inflater=getLayoutInflater();
            View row=inflater.inflate(R.layout.share_spinner_row, parent, false);

            ImageView icon=(ImageView)row.findViewById(R.id.spinner_icon);
            icon.setImageResource(iconList[position]);

            return row;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_share, menu);

        Spinner shareSpinner = (Spinner) menu.findItem(R.id.action_social_share).getActionView();

        spinnerAdapter = new ShareSpinnerAdapter(this, R.layout.share_spinner_row);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        shareSpinner.setAdapter(spinnerAdapter);

        shareSpinner.setBackground(getResources().getDrawable(R.drawable.ic_action_social_share));

        // add listener
        shareSpinner.setOnItemSelectedListener(this);

        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Makes the UP caret go back to the previous fragment MakeCuteHome
        switch (item.getItemId()) {
            case android.R.id.home:
                android.app.FragmentManager fm = getFragmentManager();
                fm.popBackStack();
                finish();
                return true;
            case R.id.action_social_share:


                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

menu_share.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <!-- Share, should appear as action button -->
    <item android:id="@+id/action_social_share"
        android:icon="@drawable/ic_action_social_share"
        android:title="Share"
        android:actionLayout="@layout/action_share"
        app:showAsAction="always"
        app:actionViewClass="android.widget.Spinner"/>

</menu>

action_share.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/share_spinner"
        android:layout_gravity="center_horizontal"
        android:spinnerMode="dropdown"/>
</LinearLayout>

share_spinner_row

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/spinner_icon"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

回答1:


If you want share icon on action bar work as a spinner... then create child of item tag in menu_share.xml file as..

 <item android:id="@+id/menu"
      android:icon="@drawable/menu"
      android:title="menu"

      android:showAsAction="always" >


      <menu>

    <item
        android:id="@+id/Login"
        android:title="Login"
        android:icon="@drawable/contact" />
    <item
        android:id="@+id/share"
        android:title="Share"
        android:icon="@drawable/share" />
     <item
        android:id="@+id/like"
        android:title="Like"
        android:icon="@drawable/favourite" />
</menu>

    </item>


来源:https://stackoverflow.com/questions/30433501/custom-spinner-not-showing-dropdown-in-actionbar-icon

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