我有一个ListView
,我想用 SectionIndexer 实现 fastscroll,ImageView
每行项目有 3 个项目,如下所示:
https://www.dropbox.com/s/th83hznhqzr01ds/cap2.png?dl=0
我想为每个图像视图 1、2、3 实现 onclicklistener 但是当我使用
imageview1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO here
}
});
它不起作用 -onClickListener
没有被抓住,我不知道为什么。
这是我的适配器代码:
public class LazyImageLoadAdapter extends BaseAdapter implements SectionIndexer{
private Context _context;
private TopActivity _parentActivity;
private static LayoutInflater inflater=null;
public CoverBookLoader _coverBookLoader;
private ArrayList<RowBook> _arrListRowBooks;
private int sizeView;
public LazyImageLoadAdapter(Context context, ArrayList<RowBook> arrListRowBooks) {
this._context = context;
this._parentActivity = (TopActivity) context;
this.sizeView = _parentActivity.getScreenWidth();
this._arrListRowBooks = arrListRowBooks;
inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
_coverBookLoader = new CoverBookLoader(_context);
}
public int getCount() {
// return data.length;
return _arrListRowBooks.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public LinearLayout llBook1;
public ImageView ivBookCover1;
public ProgressBar pbLoadingBookCover1;
public LinearLayout llBook2;
public ImageView ivBookCover2;
public ProgressBar pbLoadingBookCover2;
public LinearLayout llBook3;
public ImageView ivBookCover3;
public ProgressBar pbLoadingBookCover3;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holderBook;
LayoutParams lpSizeBook = new LayoutParams(sizeView / 3, sizeView * 3 / 5);
if(convertView==null){
vi = inflater.inflate(R.layout.listview_row, parent, false);
holderBook = new ViewHolder();
holderBook.llBook1 = (LinearLayout) vi.findViewById(R.id.llBook1);
holderBook.llBook1.setLayoutParams(lpSizeBook);
holderBook.ivBookCover1 = (ImageView) vi.findViewById(R.id.ivBookCover1);
holderBook.pbLoadingBookCover1 = (ProgressBar) vi.findViewById(R.id.pbLoadingCoverImageBook1);
holderBook.llBook2 = (LinearLayout) vi.findViewById(R.id.llBook2);
holderBook.llBook2.setLayoutParams(lpSizeBook);
holderBook.ivBookCover2 = (ImageView) vi.findViewById(R.id.ivBookCover2);
holderBook.pbLoadingBookCover2 = (ProgressBar) vi.findViewById(R.id.pbLoadingCoverImageBook2);
holderBook.llBook3 = (LinearLayout) vi.findViewById(R.id.llBook3);
holderBook.llBook3.setLayoutParams(lpSizeBook);
holderBook.ivBookCover3 = (ImageView) vi.findViewById(R.id.ivBookCover3);
holderBook.pbLoadingBookCover3 = (ProgressBar) vi.findViewById(R.id.pbLoadingCoverImageBook3);
vi.setTag( holderBook );
}
else {
holderBook=(ViewHolder)vi.getTag();
}
LinearLayout llBook1 = holderBook.llBook1;
llBook1.setLayoutParams(lpSizeBook);
ImageView ivBookCover1 = holderBook.ivBookCover1;
ProgressBar pbLoadingBookCover1 = holderBook.pbLoadingBookCover1;
LinearLayout llBook2 = holderBook.llBook2;
llBook2.setLayoutParams(lpSizeBook);
ImageView ivBookCover2 = holderBook.ivBookCover2;
ProgressBar pbLoadingBookCover2 = holderBook.pbLoadingBookCover2;
LinearLayout llBook3 = holderBook.llBook3;
llBook3.setLayoutParams(lpSizeBook);
ImageView ivBookCover3 = holderBook.ivBookCover3;
ProgressBar pbLoadingBookCover3 = holderBook.pbLoadingBookCover3;
RowBook rowBook = _arrListRowBooks.get(position);
if (rowBook.getSize() < 3) {
llBook3.setVisibility(View.VISIBLE);
}
if (rowBook.getSize() < 2) {
llBook2.setVisibility(View.VISIBLE);
}
if (rowBook != null) {
for (int i = 0; i < rowBook.getSize(); i++) {
switch (i) {
case 0:
addBook(rowBook.getBookAt(i), ivBookCover1, pbLoadingBookCover1);
break;
case 1:
addBook(rowBook.getBookAt(i), ivBookCover2, pbLoadingBookCover2);
break;
case 2:
addBook(rowBook.getBookAt(i), ivBookCover3, pbLoadingBookCover3);
break;
}
}
}
return vi;
}
private void addBook(Books book , ImageView ivBookCover, ProgressBar pbLoadingCoverImageBook){
ivBookCover.setImageResource(R.drawable.blank_book);
ivBookCover.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO here
}
});
JSONObject objBookTag = new JSONObject();
try {
objBookTag.put(Define.KEY_JSONOBJ_BOOK_ID, book.getId());
objBookTag.put(Define.KEY_JSONOBJ_BOOK_URL_THUMBNAIL, book.urlThumbnail);
ivBookCover.setTag(objBookTag);
} catch (JSONException e) {
e.printStackTrace();
ivBookCover.setTag("{\""+Define.KEY_JSONOBJ_BOOK_ID+"\":\"" + book.getId()+"\", \""+Define.KEY_JSONOBJ_BOOK_URL_THUMBNAIL+"\":\""+book.urlThumbnail+"\"");
}
_coverBookLoader.DisplayImage(book.urlThumbnail, ivBookCover, book.getId(), pbLoadingCoverImageBook);
}
private String[] mSections;
public void initSection(String[] sections){
mSections = sections;
}
@Override
public Object[] getSections() {
Log.d("ListView", "Get sections");
if (mSections != null) {
String[] sectionsArr = new String[mSections.length];
for (int i=0; i < mSections.length; i++) {
sectionsArr[i] = "" + mSections[i];
}
return sectionsArr;
}
return null;
}
@Override
public int getPositionForSection(int sectionIndex) {
if (mSections != null) {
Log.d("ListView", "Get position for section");
for (int i=0; i < this.getCount(); i++) {
if (mSections[sectionIndex].equals("" + i)) {
return i;
}
}
}
return 0;
}
@Override
public int getSectionForPosition(int position) {
Log.d("ListView", "Get section");
return 0;
}
}