2013-02-05 32 views
11

默認情況下,所選項目的操作欄列表導航寬度與最寬項目一樣寬。我想在選定的項目中使用「wrap_content」,因爲它在google +,gmail,maps android應用中。操作欄列表導航寬度 - 換行內容

有人知道這麼做很熱嗎?

我試圖覆蓋導航適配器的getView,但它不起作用。看起來這個視圖是用另一個寬度最寬的視圖來包裝的。我也嘗試使用微調控制器的actionbar.setCustom視圖,但微調控制器的行爲是相同的。它使用最寬的項目寬度。

感謝您的所有建議,鏈接和幫助。

Current state

Required state

回答

13

我遇見了同樣的問題。看起來Android會爲所有項目調用getView()方法,最後將寬度設置爲最寬項目的寬度。

因此,這裏是我的解決方案:

  1. 存儲當前選定的部分(在你的例子e.g部分),說selectedSection,在你的代碼。實際上,您必須在NavigationListener的onNavigationItemSelected()方法中執行此操作。

  2. 重寫SpinnerAdapter的getView()方法,始終將其內容設置爲selectedSection。

  3. 在NavigationListener的onNavigationItemSelected()方法中,嘗試在將當前模式設置爲selectedSection後,調用spinnerAdapter的notifyDataSetChanged()方法。

這裏的示例代碼:

final int selectedSection = 0; 
final String sectionLabels[] = {"short sec", "long section", "looooonger section"}; 

final ArrayAdapter<String> arrayAdapter = 
    new arrayAdapter<String>(this, simple_list_item_1) { 
    @Override 
    public int getCount() { 
     // You need to implement here. 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
     convertView = LayoutInflater.from(getContext()).inflate(R.layout.you_entry, null); 
     } 
     TextView textView = (TextView) convertView.findViewById(R.id.you_text); 
     // Set the text to the current section. 
     textView.setText(sectionLabels[selectedSection]); 
    } 

    @Override 
    public View getDropDownView(int position, View convertView, ViewGroup parent) { 
     // You need to implement here. 
    } 

    } 

actionBar.setListNavigationCallbacks(navigationAdpater, 
    new ActionBar.OnNavigationListener() { 

    @Override 
    public boolean onNavigationItemSelected(int itemPosition, long itemId) { 
     // Store the current section. 
     selectedSection = itemPosition; 
     navigationAdpater.notifyDataSetChanged(); 
     return true; 
    } 
    }); 
+1

很好的回答!謝謝。 你也可以替換textView.setText(sectionLabels [selectedSection]); with textView.setText(sectionLabels [actionBar.getSelectedNavigationIndex()]); 所以你不需要選擇部分 – vandzi

+0

嗯,沒錯!我們不需要selectedSection ;-) @vandzi –

+2

有一個更簡單的方法。在getView()調用((AdapterView)parent).getSelectedItemPosition()中獲取當前位置。 –

0

創建一個微調,並添加textviews了這一點。在微調器上,將maxWidth字段設置爲你想要的任何值,並處理這個。另一種選擇是設置

android:layout_width="0" 
android_layout_weight="0.3" 

這將意味着微調器將只佔用1/3的微調寬度。

在TextView的你會設置:

android:singleLine="false" 
android:ellipsize="end" 
+0

感謝。不幸的是它不起作用。我知道如何設置固定寬度。但我想將微調器寬度設置爲選定的項目寬度。 – vandzi

+0

然後,您可以執行OnGlobalLayoutChangeListener並將微調器寬度設置爲selecteditem寬度。 – lokoko

3

你需要重寫你的適配器getView()方法,並設置佈局PARAMS到TextView的包的內容。微調器將自動調整選定項目的大小。

我創建了一個自定義適配器從ArrayAdapter延伸:

public class CustomAdapter extends ArrayAdapter<String> { 

之後,我重寫getView()方法改變當前視圖的LayoutParams:

@Override 
public View getView(final int position, final View convertView, final ViewGroup parent) { 
      View view = convertView; 
      if (null == view) { 
       view = LayoutInflater.from(this.getContext()) 
         .inflate(android.R.layout.simple_list_item1, null); 
       ((TextView) view).setText(getItem(position)); 
      } 
      final LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, 
               LayoutParams.WRAP_CONTENT); 
      view.setLayoutParams(params); 
      return view; 
} 

在你的活動中,創建適配器並設置你微調它:

List<String> stringList = new ArrayList<String>(); 
stringList.add("test1"); 
stringList.add("test12"); 
stringList.add("test123"); 
stringList.add("test1234"); 

final SpinnerAdapter adapter = new CustomAdapter(this.getContext(), 
               android.R.layout.simple_list_item1, 
               android.R.id.text1, 
               stringList); 
mySpinner.setAdapter(adapter); 

當你選擇索姆在列表中,微調器將重新創建視圖,將所選項目重新排序到第一個位置,並將他自己調整爲內容的大小。

+0

不適用於我 – Roman

0

這對我有效。重要的是這個。把下面的代碼放在你的適配器中,並使用selectedItemPosition從對象數組中選擇文本。

int selectedItemPosition = position; 
    if (parent instanceof AdapterView) { 
     selectedItemPosition = ((AdapterView) parent) 
       .getSelectedItemPosition(); 
    } 

示例如下。

public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = getLayoutInflater(); 
    final View spinnerCell; 
    if (convertView == null) { 
     // if it's not recycled, inflate it from layout 
     spinnerCell = inflater.inflate(R.layout.layout_spinner_cell, parent, false); 
    } else { 
     spinnerCell = convertView; 
    } 
    int selectedItemPosition = position; 
    if (parent instanceof AdapterView) { 
     selectedItemPosition = ((AdapterView) parent) 
       .getSelectedItemPosition(); 
    } 

    TextView title = (TextView) spinnerCell.findViewById(R.id.spinnerTitle); 
    title.setText(titles[selectedItemPosition]); 
    return spinnerCell; 
} 

如果你需要一個解釋請點擊此鏈接:http://coding-thoughts.blogspot.in/2013/11/help-my-spinner-is-too-wide.html