当前位置:   article > 正文

Android UI--开源项目IndexableListView(字母索引),准备Android面试_android字母索引控件

android字母索引控件

setContentView(R.layout.main);

// 初始化一些数据

mItems = new ArrayList();

mItems.add(“Diary of a Wimpy Kid 6: Cabin Fever”);

mItems.add(“Steve Jobs”);

mItems.add(“Inheritance (The Inheritance Cycle)”);

mItems.add(“11/22/63: A Novel”);

mItems.add(“The Hunger Games”);

mItems.add(“The LEGO Ideas Book”);

mItems.add(“Explosive Eighteen: A Stephanie Plum Novel”);

mItems.add(“Catching Fire (The Second Book of the Hunger Games)”);

mItems.add(“Elder Scrolls V: Skyrim: Prima Official Game Guide”);

mItems.add(“Death Comes to Pemberley”);

mItems.add(“Diary of a Wimpy Kid 6: Cabin Fever”);

mItems.add(“Steve Jobs”);

mItems.add(“Inheritance (The Inheritance Cycle)”);

mItems.add(“11/22/63: A Novel”);

mItems.add(“The Hunger Games”);

mItems.add(“The LEGO Ideas Book”);

mItems.add(“Explosive Eighteen: A Stephanie Plum Novel”);

mItems.add(“Catching Fire (The Second Book of the Hunger Games)”);

mItems.add(“Elder Scrolls V: Skyrim: Prima Official Game Guide”);

mItems.add(“做作”);

mItems.add(“wokao”);

Collections.sort(mItems); // 排序

ContentAdapter adapter = new ContentAdapter(this,

android.R.layout.simple_list_item_1, mItems);

mListView = (IndexableListView) findViewById(R.id.listview);

mListView.setAdapter(adapter);

mListView.setFastScrollEnabled(true); // 设置快速滑动

}

private class ContentAdapter extends ArrayAdapter implements

SectionIndexer {

private String mSections = “#ABCDEFGHIJKLMNOPQRSTUVWXYZ”;

public ContentAdapter(Context context, int textViewResourceId,

List objects) {

super(context, textViewResourceId, objects);

}

@Override

public int getPositionForSection(int section) {

// If there is no item for current section, previous section will be

// selected

// 如果当前部分没有item,则之前的部分将被选择

for (int i = section; i >= 0; i–) {

for (int j = 0; j < getCount(); j++) {

System.out.println(getCount());

if (i == 0) { // #

// For numeric section 数字

for (int k = 0; k <= 9; k++) {// 1…9

// 字符串第一个字符与1~9之间的数字进行匹配

if (StringMatcher.match(

String.valueOf(getItem(j).charAt(0)),

String.valueOf(k)))

return j;

}

} else { // A~Z

if (StringMatcher.match(

String.valueOf(getItem(j).charAt(0)),

String.valueOf(mSections.charAt(i))))

return j;

}

}

}

return 0;

}

@Override

public int getSectionForPosition(int position) {

return 0;

}

@Override

public Object[] getSections() {

String[] sections = new String[mSections.length()];

for (int i = 0; i < mSections.length(); i++)

sections[i] = String.valueOf(mSections.charAt(i));

return sections;

}

}

}

字符串匹配工具类

/*

  • Copyright 2011 woozzu

  • Licensed under the Apache License, Version 2.0 (the “License”);

  • you may not use this file except in compliance with the License.

  • You may obtain a copy of the License at

  • http://www.apache.org/licenses/LICENSE-2.0
    
    • 1
  • Unless required by applicable law or agreed to in writing, software

  • distributed under the License is distributed on an “AS IS” BASIS,

  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

  • See the License for the specific language governing permissions and

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/894781
推荐阅读
相关标签
  

闽ICP备14008679号