2012-06-11 41 views
16

Tôi đang sử dụng bộ điều hợp cho ListView triển khai SectionIndexer. ListViewfastScrollEnabled đặt thành true trong tệp xml. Mọi thứ hoạt động tốt trên Android 2.2 và 2.3, nhưng khi tôi thử nghiệm ứng dụng của mình trên máy tính bảng có Android 3.0, tại một số thanh cuộn phần biến mất. Ví dụ khi tôi cuộn xuống danh sách, tại các phần tử bắt đầu bằng chữ cái Thanh cuộn A-B có thể nhìn thấy, nhưng đối với chữ cái C-H thì không, và sau đó H lại hiển thị.Thanh cuộn biến mất khi sử dụng SectionIndexer tại các phần cụ thể trên HoneyComb

Bộ điều hợp này được tạo để sắp xếp nội dung theo thứ tự bảng chữ cái trong ListView để có thể sử dụng fastscroll.

Ứng dụng được thiết kế cho API cấp 8, vì vậy tôi không thể sử dụng fastScrollAlwaysVisible.

Đây là một bộ quy tắc chuyển đổi của tôi:

public class AlphabetSimpleAdapter extends SimpleAdapter implements SectionIndexer { 

    private HashMap<String, Integer> charList; 
    private String[] alphabet; 
    public Typeface tfSansMedium; 
    public Context mContext; 
    public int mResource; 
    public int[] mTo; 
    public List<? extends Map<String, ?>> mData; 
    public String mTitleKey; 

    public AlphabetSimpleAdapter(Context context, 
      List<? extends Map<String, ?>> data, int resource, String[] from, 
      int[] to, String titleKey /* key sent in hashmap */) { 
     super(context, data, resource, from, to); 
     mData = data; 
     mTitleKey = titleKey; 
     mContext = context; 
     mResource = resource; 
     mTo = new int[to.length]; 
     for (int i = 0; i < to.length; i ++) 
     { 
      mTo[i] = to[i]; 
     } 
     charList = new HashMap<String, Integer>(); 
     int size = data.size(); 
     tfSansMedium = Typeface.createFromAsset(context.getAssets(), "fonts/VitesseSans-Medium.otf"); 
     for(int i = 0; i < size; i++) { 
         // Parsing first letter of hashmap element 
      String ch = data.get(i).get(titleKey).toString().substring(0, 1); 
      ch = ch.toUpperCase(); 

      if(!charList.containsKey(ch)) { 
       charList.put(ch, i); // Using hashmap to avoid duplicates 
      } 
     } 

     Set<String> sectionLetters = charList.keySet(); // A set of all first letters 

     ArrayList<String> sectionList = new ArrayList<String>(sectionLetters); // Creating arraylist to be able to sort elements 
     Collections.sort(sectionList, Collator.getInstance(new Locale("pl", "PL"))); // Sorting elements 
     alphabet = new String[sectionList.size()]; 
     sectionList.toArray(alphabet); 

    } 

    // Methods required by SectionIndexer 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     if (v == null) { 
      LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = li.inflate(mResource, null); 
     } 
     for (int i = 0; i < mTo.length; i ++) { 
      TextView tv = (TextView)v.findViewById(mTo[i]); 
      if (tv != null) tv.setTypeface(tfSansMedium); 
     } 
     return super.getView(position, v, parent); 
    } 

    @Override 
    public int getPositionForSection(int section) { 
     if(!(section > alphabet.length-1)) { 
      return charList.get(alphabet[section]); 
     } 
     else { 
      return charList.get(alphabet[alphabet.length-1]); 
     } 
    } 

    @Override 
    public int getSectionForPosition(int position) { 
     return charList.get(mData.get(position).get(mTitleKey).toString().substring(0, 1)); 
    } 

    @Override 
    public Object[] getSections() { 
     return alphabet; 
    } 
} 

là một HashMap nơi tôi lưu trữ thư từ với chỉ số xuất hiện cuối cùng của họ để khi tôi có 6 yếu tố bắt đầu bằng chữ "A", giá trị cho khóa " A "là 5 và cứ tiếp tục như vậy.

alphabet là một mảng Chuỗi có tất cả các chữ cái đầu tiên hiện có.

+0

Bạn đã xác định điều này? Tôi đang làm việc trên cùng một vấn đề. Nó hink vấn đề của tôi là với phương pháp getSectionForPosition. – KickingLettuce

+0

Thật không may là không. Đã dành nhiều giờ để tìm ra điều này, nhưng cuối cùng cũng phải để nó như thế. –

+0

Vấn đề này đã được đề cập ở đây: http://stackoverflow.com/a/13470842/1140682 – saschoar

Trả lời

2

Tôi đã gặp phải sự cố tương tự. Tôi chỉ đưa ra phiên bản SDK tối thiểu là 8 và traget là 16 và đã viết mã bên dưới. Và mọi thứ đều ổn.

   int currentapiVersion = android.os.Build.VERSION.SDK_INT; 
       if (currentapiVersion <= android.os.Build.VERSION_CODES.FROYO){ 
        // Do something for froyo and above versions 
        fblist.setFastScrollEnabled(true); 


       } else if(currentapiVersion > android.os.Build.VERSION_CODES.HONEYCOMB){ 
        // do something for phones running an SDK before froyo 
        fblist.setFastScrollEnabled(true); 
        fblist.setFastScrollAlwaysVisible(true); 
       } 
+0

Nó hoạt động, cảm ơn !!! – Cabezas

Các vấn đề liên quan