本文介绍了在GrivView的BaseAdapter绝对位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想写 BaseAdapter
我的 GrivView
但在
public View getView(int position, View convertView, ViewGroup parent) { ... }
在位置
而呈现下一个内容是:
the position
while rendering next elements is:
visible elements
---------------
| 1 2 3 |
| 4 5 6 | <--- screen
| 7 8 9 |
---------------
nonvisible elements
| 1 2 3 | <-- below visible area
| 4 5 6 |
在这里真正是:
where real is:
visible elements
---------------
| 1 2 3 |
| 4 5 6 |
| 7 8 9 |
---------------
nonvisible elements
| 10 11 12 |
| 13 14 15 |
我知道这是正确的行为(它计数唯一可见的元素),但如何让项目在列表中的实际位置?要不怎么知道我应该回到它的布局?
I'm aware it's correct behaviour (it counts only visible elements), but how to get the real position of item on list? Or how to know which layout should I return?
修改:但是返回的数量更比随机我发现,即使是在屏幕上
EDIT: However the returned number are much more random than I showed, even on screen.
推荐答案
使用 BaseAdapter
酷似谷歌Documentatnion 显示效果还算不错。
Using BaseAdapter
exactly like Google Documentatnion shows works fine.
我的问题是:
-
不会使用ViewHolder
NOT using ViewHolder
或试图在viewHolder持有ID
or trying to hold id in viewHolder
我应该使用 ViewHolder
如下:
public View getView(int position, View convertView, ViewGroup parent) {
// create holder
ViewHolder holder; // check if layout exists
if (convertView == null) { // if it's not recycled, initialize some
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = (LinearLayout) inflater.inflate(R.layout.card_prev,
null);
// Create layout elements
ImageView image = (ImageView) convertView
.findViewById(R.id.imageViewThumbnail);
TextView desc = (TextView) convertView
.findViewById(R.id.textViewDescription);
// Place layout elements in holder
holder = new ViewHolder();
holder.id = position;
holder.preview = image;
holder.text = desc;
convertView.setTag(holder);
} else {
// Get existing holder
holder = (ViewHolder) convertView.getTag();
}
// Update layout here using layout elements from holder
String imageName = "image_" + (position + 1);
final int imageId = mContext.getResources().getIdentifier(imageName,
"drawable", "com.myapp");
holder.text.setText("" + (position+1));
holder.preview.setImageResource(imageId);
holder.preview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent fullScreenIntent = new Intent(v.getContext(),
FullScreenImage.class);
fullScreenIntent.putExtra("imageRes", imageId);
MainMenuActivity.currentShownActivity
.startActivity(fullScreenIntent);
}
});
return convertView;
}
这篇关于在GrivView的BaseAdapter绝对位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!