我有一个ListView。在适配器内部,当我执行以下操作(下面的代码)时,列表首次显示时,firstVisible
仅等于position
。在滚动过程中,当视图明显位于顶部时,适配器仍无法将其捕获为顶部。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
...
int firstVisible = ((ListView) parent).getFirstVisiblePosition();
if (firstVisible == position) {/
//print some stuff and do work
} else {
//print some other stuff and do other work
}
}
return rowView;
}
所以我的问题是,在滚动过程中如何跟踪特定项目在ListView的顶部?我想从适配器内部执行此操作。
编辑
当有问题的视图到达顶部时:首先,我想将其设置为GONE,以使其不可见。实际上,如果我只能那样做,那就太好了。然后,我想获取其数据以进行一些工作。
最佳答案
好吧,我想我有一个解决方案:
public class MyActivity extends Activity {
ListView listView;
ArrayAdapter<String> adapter;
int firstVisible;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listView);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
processView(v, getItem(position), true);
return v;
}
};
for (int i = 0; i < 30; i++) {
adapter.add("Value " + String.valueOf(i + 1));
}
listView.setAdapter(adapter);
firstVisible = listView.getFirstVisiblePosition();
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem != firstVisible) {
onFirstVisibleChanged(firstVisible, firstVisibleItem);
firstVisible = firstVisibleItem;
}
}
});
}
private void processView(View v, String value, boolean fromGetView) {
//if from get view we don't want to set yet wait till it comes from scrolling
if (value.contains("4") && !fromGetView) {
v.setBackgroundResource(android.R.color.holo_orange_light);
} else {
v.setBackgroundResource(android.R.color.transparent);
}
}
private void onFirstVisibleChanged(int oldIndex, int newIndex) {
View v = getViewForPosition(newIndex);
String val = adapter.getItem(newIndex);
processView(v, val, false);
}
/**
* @param position
* position of item in list
* @return null if position outside bounds
*/
public View getViewForPosition(int position) {
int firstPosition = listView.getFirstVisiblePosition() - listView.getHeaderViewsCount();
int wantedChild = position - firstPosition;
// Say, first visible position is 8, you want position 10, wantedChild will now be 2
// So that means your view is child #2 in the ViewGroup:
if (wantedChild < 0 || wantedChild >= listView.getChildCount()) {
return null;
}
return listView.getChildAt(wantedChild);
}
}
对我来说,最好的解释方法是自己尝试一下。
这是使最多30个字符串“值n”的列表
如果字符串在滚动时包含数字4,我们会将背景色更改为橙色。如果您希望删除视图,我们应该可以这样做:
private void processView(View v, String value, boolean fromGetView) {
//if from get view we don't want to set yet wait till it comes from scrolling
if (value.contains("4") && !fromGetView) {
adapter.remove(value);
} else {
//no action needed
}
}
但是,这可能会使用户感到困惑,因为值4到达顶部时将其删除,而值5取代了它。
关于android - 在滚动过程中检查项目是否位于ListView适配器中的第一个位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18773620/