IndexableGridView.java
public class IndexableGridView extends GridView {
private boolean mIsFastScrollEnabled = false;
private IndexGridScroller mScroller = null;
private GestureDetector mGestureDetector = null;
public IndexableGridView(Context context) {
super(context);
}
public IndexableGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public IndexableGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean isFastScrollEnabled() {
return mIsFastScrollEnabled;
}
@Override
public void setFastScrollEnabled(boolean enabled) {
mIsFastScrollEnabled = enabled;
if (mIsFastScrollEnabled) {
if (mScroller == null)
mScroller = new IndexGridScroller(getContext(), this);
} else {
if (mScroller != null) {
mScroller.hide();
mScroller = null;
}
}
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
// Overlay index bar
if (mScroller != null)
mScroller.draw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// Intercept ListView's touch event
if (mScroller != null && mScroller.onTouchEvent(ev))
return true;
if (mGestureDetector == null) {
mGestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
// If fling happens, index bar shows
if (mScroller != null)
mScroller.show();
return super.onFling(e1, e2, velocityX, velocityY);
}
});
}
mGestureDetector.onTouchEvent(ev);
return super.onTouchEvent(ev);
}
@Override
public void setAdapter(ListAdapter adapter) {
super.setAdapter(adapter);
if (mScroller != null)
mScroller.setAdapter(adapter);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (mScroller != null)
mScroller.onSizeChanged(w, h, oldw, oldh);
}
}
ImageAdapter.java
public class ImageAdapter extends BaseAdapter implements SectionIndexer{
private String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private Context mContext;
List<String> mItems;
// Constructor
public ImageAdapter(Context c, List<String> mItems) {
mContext = c;
this.mItems = mItems;
}
public int getCount() {
return mItems.size();
}
public Object getItem(int position) {
return mItems.get(position);
}
public long getItemId(int position) {
return mItems.indexOf(getItem(position));
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageBitmap(getBitmapFromAsset("imgs", mItems.get(position)));
return imageView;
}
@Override
public Object[] getSections() {
// TODO Auto-generated method stub
String[] sections = new String[mSections.length()];
for (int i = 0; i < mSections.length(); i++)
sections[i] = String.valueOf(mSections.charAt(i));
return sections;
}
@Override
public int getPositionForSection(int sectionIndex) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getSectionForPosition(int position) {
// TODO Auto-generated method stub
for (int i = position; i >= 0; i--) {
for (int j = 0; j < getCount(); j++) {
if (i == 0) {
// For numeric section
for (int k = 0; k <= 9; k++) {
if (StringMatcher.match(String.valueOf(((String) getItem(j)).charAt(0)), String.valueOf(k)))
return j;
}
} else {
if (StringMatcher.match(String.valueOf(((String) getItem(j)).charAt(0)), String.valueOf(mSections.charAt(i))))
return j;
}
}
}
return 0;
}
}
private Bitmap getBitmapFromAsset(String folder, String strName)
{
AssetManager assetManager = getResources().getAssets();
InputStream istr = null;
Bitmap bitmap = null;
try {
istr = assetManager.open(folder+"/"+strName);
bitmap = BitmapFactory.decodeStream(istr);
istr.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
我在gridview上使用了上面的字母索引器代码。与this link相同。 我只是将listview更改为gridview ..现在我的问题是它没有向我显示alphaindexer在右侧。
listview索引器示例:
我想显示如下图像的数据:
答案 0 :(得分:2)
用于排序网格:
使用名称字符串创建pojo
类。然后将pojo
传递给Comparator
。将名称1与2进行比较并返回之前的名称。
对于分组:
基本上你需要一个标题来进行分组。使用此library(Stickygridheaders)。