【问题标题】:How to add spinner to subtitle - as in the Play Music app for Android?如何在字幕中添加微调器 - 就像在 Android 的 Play 音乐应用程序中一样?
【发布时间】:2013-09-26 05:51:09
【问题描述】:

请参考附图。

setListNavigationCallbacks(mSpinnerAdapter, mNavigationCallback)“我的图书馆” 下添加了一个旋转器,我无法在其下添加子列表。我找不到为 “ALL MUSIC” 添加微调器的方法,不会弄乱操作栏的主标题。

感谢任何帮助!

【问题讨论】:

标签: android android-actionbar android-spinner


【解决方案1】:

它看起来像一个定制的ArrayAdapter 您需要实现getDropDownView 并返回您希望看到的视图。

随意修改代码。这只是一个概念证明。 在您的活动中:

getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    getActionBar().setTitle("");

    getActionBar().setListNavigationCallbacks(new MySpinnerAdapter(this, R.layout.customspinneritem, items), new OnNavigationListener() {

        @Override
        public boolean onNavigationItemSelected(int itemPosition, long itemId) {

            return false;
        }
    });

MySpinnerAdapter:

 public class MySpinnerAdapter extends ArrayAdapter<String>{

// CUSTOM SPINNER ADAPTER
private Context mContext;
public MySpinnerAdapter(Context context, int textViewResourceId,
        String[] objects) {
    super(context, textViewResourceId, objects);

    mContext = context;
    // TODO Auto-generated constructor stub
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    return getCustomView(position, convertView, parent);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    return getCustomView(position, convertView, parent);
}

public View getCustomView(int position, View convertView,ViewGroup parent) {
// TODO Auto-generated method stub
// return super.getView(position, convertView, parent);


LayoutInflater inflater =  
    ( LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.customspinneritem, null);
holder = new ViewHolder();
holder.txt01 = (TextView) convertView.findViewById(R.id.TextView01);
holder.txt02 = (TextView) convertView.findViewById(R.id.TextView02);

convertView.setTag(holder);

} else {

holder = (ViewHolder) convertView.getTag();
}

holder.txt01.setText("My Library");
holder.txt02.setText("ALL MUSIC");

return convertView;
}

class ViewHolder {
    TextView txt01;
    TextView txt02;
}



} // end custom adapter

【讨论】:

  • 如何根据 ListView 中导航抽屉中的选择更改我的库标题。
  • 这并不是一个完整的答案。为您引用的自定义布局包含 xml 会很好。
【解决方案2】:

由于我的声誉,我还不能对答案发表评论,但要以与 Google 音乐应用程序相同的方式实现这一点,我认为您可能希望对 getDropDownView() 使用不同的方法,因为 Google 音乐上的下拉菜单是就像普通的下拉菜单一样。

将 prijupaul 的方法替换为以下是您正在寻找的想法。

像这样:

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

    if (convertView == null) {
        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        convertView = inflater.inflate(R.layout.dropdown_list_item, parent, false);

        holder = new DropDownViewHolder();
        holder.mTitle = (TextView) convertView.findViewById(R.id.textView_dropdown_title);

        convertView.setTag(holder);
    } else {
        holder = (DropDownViewHolder) convertView.getTag();
    }

    // Should have some sort of data set to go off of, we'll assume
    // there is a some array called mData.
    holder.mTitle.setText(mData[position]);

    return convertView;
}

public class DropDownViewHolder {
    TextView mTitle;
}

我想我应该把代码写清楚。

【讨论】:

  • 如何根据导航抽屉列表视图中的选择更改标题。不影响下拉视图的选择。
  • @madan 你找到改标题的办法了吗。
【解决方案3】:

这里是解决方案。我们需要为 getView(...) 创建一个不同的方法,并且还需要跟踪从下拉列表中选择的项目。单击导航抽屉中的项目后,调用一个新方法(在微调器适配器中创建新方法作为更新标题)就像

public void setHeaderTitle(String str) { titleTxt = str; }

从get view调用不同的方法

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

        return getBarView(position);
    }

private View getBarView(int position) {
        if (displayHolder == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView1 = inflater.inflate(R.layout.actionbar_spinner_item, null);
            displayHolder = new ViewHolder();
        }

        displayHolder.txt01 = (TextView) convertView1.findViewById(R.id.actionBarTitle123);
        displayHolder.txt02 = (TextView) convertView1.findViewById(R.id.actionBarSubTitle123);
        displayHolder.txt01.setText(titleTxt);
        displayHolder.txt02.setText(items[curPos]);

        return convertView1;
    }

以及选择下拉项时,请拨打以下方法

public void setHeaderSubTitle(int pos) {

        // update display of sub title
        curPos = pos;
        getBarView(pos);
    }

让我知道还有什么需要的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多