我有一个自定义游标适配器,它从数据库中获取日期并填充列表,其 onlistItemClick 监听器我显示一个附加到该行的视图,然后再次单击该视图隐藏该视图,
一切都很好,但我有两个问题,
1) - 我有10行,它显示10行,但是当我记录位置时,它只给我那些适合屏幕的位置。
2) - 当我点击一行时,会出现附加到该行的视图,但当我向下滚动时,我发现另一个视图在屏幕下可见,
这是我的适配器,
public class CustomCursorAdapter extends CursorAdapter {
private LayoutInflater mInflater;
public CustomCursorAdapter(Context context, Cursor cursor, int flags) {
super(context, cursor, flags);
mInflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, final Context context, final Cursor cursor) {
TextView textView = (TextView) view.findViewById(R.id.txtview);
textView.setText(cursor.getString(cursor.getColumnIndex("text")));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
//in that "list_layout.xml" i made a view that has to show and hide on click listener
View rowView = mInflater.inflate(R.layout.list_layout, parent, false);
if(cursor.getPosition()%2 != 0)
{
rowView.setBackgroundResource(R.drawable.list_selector);
}
return rowView;
}
}
我正在工作几个小时才弄明白这个问题,但是徒劳无功。
请帮助任何照顾者。
答案 0 :(得分:0)
对问题1的回答:Android会在给定时间初始化适合屏幕的行,而不是一次性出现性能问题。
回答问题2:尝试提供这样的else子句:
if(cursor.getPosition()%2 != 0)
{
rowView.setBackgroundResource(R.drawable.list_selector);
}
else
{
rowView.setBackgroundResource(R.deawable.whatever); // your view for even number rows
}
答案 1 :(得分:0)
尝试编写getView方法。
例如:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = mInflater
.inflate(R.layout.list_layout, parent, false);
if (getCursor().getPosition() % 2 != 0) {
convertView.setBackgroundResource(R.drawable.list_selector);
}else{
convertView.setBackgroundResource(<-Something->);
}
return convertView;
}
修改
@Override
public void bindView(View view, final Context context, final Cursor cursor) {
TextView textView = (TextView) view.findViewById(R.id.txtview);
textView.setText(cursor.getString(cursor.getColumnIndex("text")));
if(cursor.getPosition()%2 != 0)
{
view.setBackgroundResource(R.drawable.list_selector);
}
}