0

我正在尝试在上面有品牌名称的操作栏中实现一个微调器。如果可能,使用 ActionBar setListNavigationCallbacks 方法

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setListNavigationCallbacks(mSpinnerAdapter, null);

安卓

谁能给我一个如何做到这一点的想法?

我会在这里放一些代码,但我不知道从哪里开始,因为我还没有找到相关信息。

编辑:使用 V4.0

4

1 回答 1

0

好吧,我自己设法做到了,并发布答案以防万一有人想要答案。

如果有人想指出一种更优雅的方式,我很乐意听到。不会除了我自己的答案几天,以防有人能想出更好的答案。

//In OnCreate method on my activity I've got
    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setTitle("");
        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);      
        final SpinnerAdapter madap = new EntArrayAdapter(this,R.layout.ent_ddl_actionbar, new ArrayList<EntArrayAdapter.Data>(),allents);
        //ActionBar.OnNavigationListener nav will normaly be in here instead of null
        actionBar.setListNavigationCallbacks(madap, null);
}

我的 EntAdapter 看起来像这样:

public class EntArrayAdapter extends ArrayAdapter<com.SimpleEID.widget.EntArrayAdapter.Data> {
private Context _context;
private List<Data> _objects;

public class Data implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1

    public Data( Ent e) {           
        this.Text = e.getName();
        this.DDText = e.getName();
        this.curEnt = e;
    }
    public Data()
    {
        this.DDText = "No Avalible Ent";
        this.curEnt = null;
    }


    public String Text;
    public String DDText;
    public Integer ID;
    public Ent curEnt;
}

private int textSize=14;
private int ddLSize=20;

public EntArrayAdapter(final Context context,final int textViewResourceId, List<Data> objects,ArrayList<Ent> entList) {
    super(context, textViewResourceId, objects);  

    this._context = context;
    this._objects = objects;                

    for(int i = 0; i< entList.size() ;i++)
    {           
        Ent e = entList.get(i);
        this.add(new Data(e));
    }
            //Empty case just adding blank
    if(entList.size() == 0)
    {
        this.add(new Data());
    }
}

//This part is the Dropdown part of the view.So in here I use layout simple spinner
//As this only needs to show a word
@Override
public View getDropDownView(int position, View convertView,
        ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(_context);
        convertView = inflater.inflate(
                android.R.layout.simple_spinner_item, parent, false);
    }

    String dropdownText = _objects.get(position).DDText;

    TextView tv = (TextView) convertView
            .findViewById(android.R.id.text1);
    tv.setText(dropdownText);

            // extra settings to my text on dropdown
    tv.setHeight(100);
    tv.setPadding(40, 20, 0,0);
    tv.setTextSize(ddLSize);    
    return convertView;
}
    // This getView is the view that shows before you click to change into a dropdown
    // Also the part that shows at the top in the image case it is "BRAND NAME Ent1>"
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(_context);
                    //Chosen my own layout for this part which is basically 2 textViews inside of a LinearLayout
        convertView = inflater.inflate(
                R.layout.ent_ddl_actionbar, parent, false);
    }


    TextView tv = (TextView) convertView
            .findViewById(R.id.text_spinner_1);
    tv.setText(_objects.get(position).Text);
    tv.setTextColor(Color.BLUE);
    tv.setTextSize(textSize);// Controls the Non List size
    return convertView;
}
}

我的布局

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

<TextView 
    android:id="@+id/bar_title1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/default_brandName"
    android:textSize="16sp"/>

<TextView
android:id="@+id/text_spinner_1"
android:layout_height="fill_parent"
android:layout_width="100dip"
android:text="test"
/>

</LinearLayout>

希望这可以帮助

于 2013-10-24T21:16:04.950 回答