我在網格視圖上有30個圖像(每行3個圖像)。如果用戶選擇第20張圖像並轉到下一個屏幕並返回,那麼我想將焦點放在該位置。我用下面一行代碼,它適用於前8行,但在最後2行不滾動。請幫忙。如何滾動gridview到位置?
gridview.setSelection(position);
gridview.requestFocusFromTouch();
gridview.setSelection(position);
感謝
我在網格視圖上有30個圖像(每行3個圖像)。如果用戶選擇第20張圖像並轉到下一個屏幕並返回,那麼我想將焦點放在該位置。我用下面一行代碼,它適用於前8行,但在最後2行不滾動。請幫忙。如何滾動gridview到位置?
gridview.setSelection(position);
gridview.requestFocusFromTouch();
gridview.setSelection(position);
感謝
原因是因爲您的視圖將在您的適配器中重用。
public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] someprivatevariable;
public ImageAdapter(Context context, String[] mobileValues) {
// Your boring constructor
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
// REUSE VIEW IF NOT NULL CODE
if (convertView == null) {
.....
.....
you generally create your view here
} else {
gridView = (View) convertView;
}
return gridView;
}
}
如果你的觀點是被迫不重用,那麼你可以通過保存這個值
int index = gridview.getFirstVisiblePosition();
scrolltoposition併爲我工作
gridview.smoothScrollToPosition(int index)
你可以嘗試財產以後這樣的。我希望這會爲你工作..
選擇時會發生這樣做保存位置
int index = gridview.getFirstVisiblePosition();
當你來到回到gridview然後你可以試試這個
gridview.smoothScrollToPosition(int index)
液還原值相結合的代碼來自Monali和Sreedev(謝謝Monali和Sreedev):
private int gridviewVerticalPositionWhenThumbnailTapped;
...
// save vertical position of gridview screen, will use to re-find this location if user follows a usre and gridview gets redrawn
gridviewVerticalPositionWhenThumbnailTapped = gridview.getFirstVisiblePosition();
在應用後...
// scroll gridview to vertical position pre user tap follow
gridview.setSelection(gridviewVerticalPositionWhenThumbnailTapped);
感謝您的答覆.. – Monali
@lalitab B,而smoothScrollToPosition(指數)是不工作的ICS.Is有什麼辦法在ICS上使用請在高級告訴我感謝。 –
@Lithith B你知道如何做一個不平滑的滾動嗎?我在網格視圖中有> 1000個元素,smoothScroll需要一段時間。 – Agata