你好我有一个listview包含很多项目,但只有3项可见,我想在滚动列表后更改中间项目的背景颜色
我的Activity
实现了AbsListView.OnScrollListener
,我有2种覆盖方法,如下所示
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int firstVisibleRow = AccountList.getFirstVisiblePosition();
int lastVisibleRow = AccountList.getLastVisiblePosition();
int middleone = firstVisibleRow+1;
Account test = (Account) AccountList.getItemAtPosition(middleone);
// test is the item in the middle of listview
Log.i("", "" + middleone + "=" + test.getDescription());
}
我创建了Account列表和Account列表的适配器 帐户测试是中间项目
我觉得我很重要 现在我想将背景颜色设置为此行“test”
请任何帮助,希望我的问题清楚 谢谢所有
答案 0 :(得分:1)
首先,谢谢大家
最后我发现我的解决方案是
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
wantedView.setBackgroundResource(R.drawable.background_shape);//or color but set background
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int middlePosition = AccountList.getFirstVisiblePosition()+1;
int firstPosition = AccountList.getFirstVisiblePosition() - AccountList.getHeaderViewsCount();
int middleChild = middlePosition - firstPosition;
if (middleChild < 0 || middleChild >= AccountList.getChildCount()) {
Log.w("TAG", " Unable to get view for desired position, because its not being displayed on screen");
return;
}
middleView = AccountList.getChildAt(middleChild);
middleView.setBackgroundColor(Color.WHITE);
}
祝所有人好运:)
答案 1 :(得分:0)
使[int middleone]成为一个全局变量(在onCreate()之前声明它)
而不是使用此代码:
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
AccountList.get(middleone).setBackgroundColor(YOURCOLOR);
}
答案 2 :(得分:0)
这是完成这个技巧的代码,
list.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView arg0, int arg1) {
// TODO Auto-generated method stub
int pos = list.getFirstVisiblePosition();
LinearLayout layout = (LinearLayout) list.findViewWithTag(pos++);
layout.setBackgroundColor(Color.GREEN);
Log.v("inside", "inside" + layout.toString());
}
@Override
public void onScroll(AbsListView a,int arg1,int arg2,int arg3){
// TODO Auto-generated method stub
}
});
,getView看起来像,
if(convertView==null){
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
convertView = inflater.inflate(R.layout.layout, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.linear.setTag(position);
holder.tv1.setText(s[position]);
holder.tv2.setText(s1[position]);
请注意,我最后遇到了ClassCastException,但这是要走的路。当你找到方法时,请告诉我。