问题描述
我想创建一个非常简单的光标自定义光标适配器,以促进对不断变化的点击列项的颜色。使用下面的code
I want to create a very simple cursor custom cursor adapter to facilitate changing the colors of row items on click. Using the following code
private static int save = -1;
public void onListItemClick(ListView parent, View v, int position, long id) {
parent.getChildAt(position).setBackgroundColor(Color.BLUE);
if (save != -1 && save != position){
parent.getChildAt(save).setBackgroundColor(Color.BLACK);
}
save = position;
}
我从这个线程的code http://stackoverflow.com/a/7649880/498449
我会用一个简单的游标适配器,并放置在code在onclick,但由于在ListFragment默认列表重用的意见,为您滚动多个视图显示加以强调。谈到在IRC上,有人建议我创建一个自定义光标适配器。不过,我似乎无法找到最佳实践如何如何做到这一点,并在上面code段将适合。可以大大AP preciate的帮助。
I would have used a simple cursor adapter and placed the code in the onClick, but because the default list in the ListFragment reuses views, as you scroll multiple views are shown to be highlighted. Talking on IRC, it was suggested that I create a custom cursor adapter. However, I can't seem to locate the best practice how how to do this, and where the above code snippet would fit in. Could greatly appreciate the help.
public class AreaCursorAdapter extends CursorAdapter {
private Context context;
public AreaCursorAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView list_item = (TextView)view.findViewById(android.R.id.text1);
list_item.setText(cursor.getString(cursor.getColumnIndex(INDICATOR_NAME)));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
bindView(v, context, cursor);
return v;
}
}
我已经更新光标适配器与一些code我在网上找到。不过,我有两个问题。 1.我使用的游标加载器,所以我没有一个光标对象传递到构造函数。 2.我收到来自Eclipse的一个警告,构造已经去precated。
I've updated the cursor adapter with some code I found online. However, I have two problems. 1. I am using a cursor loader, so I don't have a "cursor" object to pass into the constructor. 2. I'm getting a warning from Eclipse that the constructor has been deprecated.
推荐答案
您应该能够做到这道:
class YourListFragment extends ListFragmentOrSomethingElse {
private AreaCursorAdapter mAdapter;
@Override
public void onCreate() {
mAdapter = new AreaCursorAdapter(this, null);
setListAdapter(mAdapter);
}
@Override
public void onListItemClick(ListView parent, View v, int position, long id) {
mAdapter.setSelectedPosition(position);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
mAdapter.swapCursor(cursor);
// should reset that here maybe
mAdapter.setSelectedPosition(-1);
}
}
public class AreaCursorAdapter extends CursorAdapter {
private Context context;
private int mSelectedPosition;
LayoutInflater mInflater;
public AreaCursorAdapter(Context context, Cursor c) {
// that constructor should be used with loaders.
super(context, c, 0);
mInflater = LayoutInflater.from(context);
}
public void setSelectedPosition(int position) {
mSelectedPosition = position;
// something has changed.
notifyDataSetChanged();
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView list_item = (TextView)view.findViewById(android.R.id.text1);
list_item.setText(cursor.getString(cursor.getColumnIndex(INDICATOR_NAME)));
int position = cursor.getPosition(); // that should be the same position
if (mSelectedPosition == position) {
view.setBackgroundColor(Color.RED);
} else {
view.setBackgroundColor(Color.WHITE);
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
// edit: no need to call bindView here. That's done automatically
return v;
}
}
这篇关于创建自定义简单的游标适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!