在Android的动作条下拉搜索栏? [英] Drop down seekbar in Android actionbar?

查看:171
本文介绍了在Android的动作条下拉搜索栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把一个下拉/下拉搜索栏的动作条。我觉得像这样的 OnCreateOptionsMenu()将工作:

I'm trying to put a dropdown/pulldown seekbar in the actionbar. I thought something like this in OnCreateOptionsMenu() would work:

    SubMenu subMenu = menu.addSubMenu("DropTest");

    MenuItem seekBarDrop = subMenu2.add("SeekBar");
    SeekBar seekBar = new SeekBar(this);
    seekBarDrop.setActionView(seekBar);
    seekBarDrop.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

    MenuItem subMenuItem = subMenu2.getItem();
    subMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

但失败草草收场。我也试着破解它变成一个微调适配器(使用IcsSpinner为actionbarsherlock):

But it fails miserably. I've also tried hacking it into a spinner adapter (using an IcsSpinner for actionbarsherlock):

    ArrayAdapter<CharSequence> seekAdapter = 
            new ArrayAdapter<CharSequence>(context,
            R.layout.spinner_subtitled_octave, 
            android.R.id.text1, new String[]{""});

    seekAdapter.setDropDownViewResource(R.layout.seekbar);

    MenuItem itemX = menu.add("SeekBar").setActionView(R.layout.spinner_ics);
    itemX.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

    IcsSpinner spinBar = (IcsSpinner) itemX.getActionView();
    spinBar.setAdapter(seekAdapter);

    /*
     * This next part doesn't crash, but it also doesn't work
     */

    LinearLayout sbLayout = (LinearLayout)
            seekAdapter.getDropDownView(0, null, null);

    SeekBar seekBar = (SeekBar) sbLayout.findViewById(R.id.v_seekbar);
    seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            Log.d(TAG, "" + progress); // nope
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {  }
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {  }
    });

使用 seekbar.xml

<?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="wrap_content"
android:orientation="horizontal" >
<TextView 
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/highlight"
/>
<SeekBar
android:id="@+id/v_seekbar"
android:minWidth="100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/spacer" />

</LinearLayout>

和spinner_seek.xml:

And spinner_seek.xml:

<?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="wrap_content"
android:orientation="horizontal" >

<TextView
android:id="@+id/TextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SEEKBAR"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/main" />

<TextView
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/highlight" />

</LinearLayout>

这至少得到一个搜索栏,以下拉菜单中,但我无法弄清楚如何获得一个参考吧。

It at least gets a seekbar to dropdown, but I can't figure out how to get a reference to it.

任何人都知道如何去最后一英里?

Anybody know how to go the last mile?

编辑:我注意到编写的PopupMenu 的存在后,但它只是API 11+(我的项目是10+)或HoloEverywhere。希望能做到这一点没有超出ABS的库。

I noticed after writing that PopupMenu exists, but it's only API 11+ (my project is 10+) or with HoloEverywhere. Hoping to do this without any libraries beyond ABS.

推荐答案

这是你在找什么:

Is this what you are looking for:

我使用BaseAdapter做出这样的:

I made this by using BaseAdapter:

MainActivity:

MainActivity:

public class MainActivity extends Activity implements OnNavigationListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActionBar bar = getActionBar();
    ArrayList<String> list = new ArrayList<String>();
    list.add("bar 0 ");
    list.add("bar 2 ");
    list.add("bar 3 ");
    list.add("bar 4 ");
    Adapter adapter = new Adapter();

    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    bar.setListNavigationCallbacks(adapter.getAdapter(this, list, "Controls"), this);
    adapter.setSeekBarListener( new SeekBarListener(){

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser, int positionInList) {
            Log.i("", "onProgressChanged " + progress + " position in list" + positionInList);

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar, int positionInList) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar, int positionInList) {
            // TODO Auto-generated method stub

        }

    });
}


@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    // TODO Auto-generated method stub
    return false;
}

}

Adapter.class:

Adapter.class:

public class Adapter {
private SeekBarListener mListener;

public interface SeekBarListener{
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser, int positionInList);
    public void onStartTrackingTouch(SeekBar seekBar, int positionInList);
    public void onStopTrackingTouch(SeekBar seekBar, int positionInList);
}

public listAdapter getAdapter(Context context, ArrayList<String> list, String title){
    return new listAdapter(context, list, title);
}

public void setSeekBarListener(SeekBarListener listener){
    mListener = listener;
}

public class listAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private onSeekbarChange mSeekListener;
    private ArrayList<String> itemsList;
    private String title;

    public listAdapter(Context context, ArrayList<String> list, String title){
        mInflater = LayoutInflater.from(context);
        if(mSeekListener == null){
            mSeekListener = new onSeekbarChange();
        }
        this.itemsList = list;
        this.title = title;
    }

    @Override
    public int getCount() {
        return itemsList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder2 holder;

        if(convertView == null){
            holder = new ViewHolder2();
            convertView = mInflater.inflate(R.layout.baseadapter_layout, null);
            holder.text_title = (TextView)convertView.findViewById(R.id.textView);
            convertView.setTag(R.layout.baseadapter_layout, holder);
        } else {
            holder = (ViewHolder2)convertView.getTag(R.layout.baseadapter_layout);
        }
        holder.text_title.setText(title);
        return convertView;
    }


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

        if(convertView == null){
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.baseadapter_dropdown_layout, null);
            holder.text = (TextView)convertView.findViewById(R.id.textView1);
            holder.seekbar = (SeekBar)convertView.findViewById(R.id.seekBar1);
            convertView.setTag(R.layout.baseadapter_dropdown_layout, holder);
        } else {
            holder = (ViewHolder)convertView.getTag(R.layout.baseadapter_dropdown_layout);
        }
        holder.text.setText(itemsList.get(position));
        holder.seekbar.setOnSeekBarChangeListener(mSeekListener);
        holder.seekbar.setTag(position);
        return convertView;

    }

}

static class ViewHolder {
    TextView text;
    SeekBar seekbar;
}

static class ViewHolder2 {
    TextView text_title;
}


public class onSeekbarChange implements OnSeekBarChangeListener{

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        int position = (Integer) seekBar.getTag();
        if(mListener != null){
            mListener.onProgressChanged(seekBar, progress, fromUser, position);
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        int position = (Integer) seekBar.getTag();
        if(mListener != null){
            mListener.onStartTrackingTouch(seekBar, position);
        }
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        int position = (Integer) seekBar.getTag();
        if(mListener != null){
            mListener.onStopTrackingTouch(seekBar, position);
        }
    }

}

}

baseadapter_layout.xml:

baseadapter_layout.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="25dp"
    android:layout_marginTop="18dp"
    android:textColor="#FFFFFF"
    android:text="Controls" />

baseadapter_dropdown_layout.xml:

baseadapter_dropdown_layout.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="15dp"
    android:textColor="#FFFFFF"
    android:text="TextView" />

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="11dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_toRightOf="@+id/textView1" />

这篇关于在Android的动作条下拉搜索栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆