8

私はアンドロイドのアクションバーのスピナーをgoogle currentアプリケーションのようなものにカスタマイズしようとしています。基本的に、 '字幕'だけが同じままで、スピナーから選択したものを '字幕'に反映する必要があります。カスタムスピナーを作成する必要があることを理解しており、getView()getDropDownView()メソッドをオーバーライドする必要があります。しかし、私は非常にこれらのメソッドを適切にオーバーライドする方法についてここで混乱しています。いくつか私は正しい方向に私を軽く振ってください。私は私の質問を明確にしたいと思う。下記のアクションバーのスピナーのカスタマイズ

the image screenshot http://androidcowboy.com/wp-content/uploads/2012/12/google-currents-3a.jpg

私のコードです。

public class CustomSpinnerAdapter extends BaseAdapter { 

private LayoutInflater inflater; 

private final Context context; 
private final String[] dropDown; 
private final String mainText; 
private final String subText; 

public CustomSpinnerAdapter(Context context, 
     String mainText, String subText,String[] dropDown) { 

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

    this.mainText=mainText; 
    this.subText=subText; 
    this.context = context; 
    this.dropDown=dropDown; 
} 

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

@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) { 
    // TODO Auto-generated method stub 

    View actionBarView = inflater.inflate(R.layout.custom_spinner, null); 
    TextView textView = (TextView) actionBarView 
      .findViewById(R.id.custom_spinner_textview); 
    textView.setText(mainText); 
    return actionBarView; 
} 

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

    View dropDownView = inflater.inflate(R.layout.custom_spinner, null); 
    TextView dropDownTextView = (TextView) dropDownView 
      .findViewById(R.id.custom_spinner_dropdown_textview); 

    dropDownTextView.setText(dropDown[position]); 
    return dropDownView; 

} 
} 

答えて

17

私はそれを解決しました。私のアダプタクラスの

リスト:ab_dropdown_view.xmlの

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

<TextView 
    android:id="@+id/ab_basemaps_title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TextView" 
    android:textSize="20sp" 
    android:textColor="@color/White" /> 

<TextView 
    android:id="@+id/ab_basemaps_subtitle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/ab_basemaps_title" 
    android:text="TextView" 
    android:textColor="@color/White" 
    android:textSize="13sp" /> 

</RelativeLayout> 

リスト:ab_main_view.xmlの

public class AdapterBaseMaps extends BaseAdapter { 

Context context; 
int layoutResourceId; 
ArrayList<ObjectLayers> data; 
LayoutInflater inflater; 

public AdapterBaseMaps(Context context, int textViewResourceId, 
     ArrayList<ObjectLayers> data) { 
    // super(a, textViewResourceId, data); 
    this.data = data; 
    inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    this.context = context; 
    this.layoutResourceId = textViewResourceId; 

} 

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

    View actionBarView = inflater.inflate(R.layout.ab_main_view, null); 
    TextView title = (TextView) actionBarView 
      .findViewById(R.id.ab_basemaps_title); 
    TextView subtitle = (TextView) actionBarView 
      .findViewById(R.id.ab_basemaps_subtitle); 
    title.setText(context.getResources() 
      .getString(R.string.label_cartravel)); 
    subtitle.setText(data.get(position).getLayerName()); 
    return actionBarView; 

} 

@Override 
public View getDropDownView(int position, View convertView, ViewGroup parent) { 
    View actionBarDropDownView = inflater.inflate(
      R.layout.ab_dropdown_view, null); 
    TextView dropDownTitle = (TextView) actionBarDropDownView 
      .findViewById(R.id.ab_basemaps_dropdown_title); 

    dropDownTitle.setText(data.get(position).getLayerName()); 

    return actionBarDropDownView; 

} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return data.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; 
} 

} 

リスト

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

<TextView 
    android:id="@+id/ab_basemaps_dropdown_title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:textSize="20sp" 
    android:padding="5dp" 
    android:textColor="@color/White" /> 

</RelativeLayout> 
+0

Objectlayers何ですか? – ajay

2

最初の答えは便利ですが、アダプターのサブクラスは不要です。

@のuser1624587の答えのようにXMLを定義して、単純public boolean onCreateOptionsMenu(Menu menu)で参照:

ArrayAdapter<CharSequence> someAdapter = new 
     ArrayAdapter<CharSequence>(context, R.layout.ab_main_view, 
     android.R.id.text1, getResources().getStringArray(R.array.some_array)); 


    someAdapter.setDropDownViewResource(R.layout.ab_dropdown); 

    MenuItem item = menu.add("SomeTitle").setActionView(R.layout.some_spinner); 
    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); //or whatever 

    someSpinner = (someSpinner) item.getActionView(); 
    someSpinner.setAdapter(someAdapter); 
    ...etc 
関連する問題