尝试在listView子项上设置setOnClickListener,以便在点击时更改列表项背景,所以我自定义了SimpleCursorAdapter,如下所示。它可以工作,但也会更改屏幕外的其他列表项颜色(比单击一个更低一步,例如:if点击项目父位置为0它也改变了位置7的背景,以同样的方式为3到10,)。我也有一个工作正常的setOnItemClickListener。不能做我做错了什么?提前谢谢。
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import com.bbt.alright.imeetup.R;
public class ProgramsCA extends SimpleCursorAdapter{
public ProgramsCA(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_program, parent, false);
return view;
}
@Override
public void bindView(View view, final Context context, Cursor cursor) {
super.bindView(view, context, cursor);
final ImageView iv = (ImageView) view.findViewById(R.id.imageViewCalV);
final RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.fTheProgram);
iv.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
View parentRow = (View) view.getParent();
ListView listView = (ListView) parentRow.getParent();
final int position = listView.getPositionForView(parentRow);
iv.setBackgroundColor(Color.TRANSPARENT);
rl.setBackgroundColor(Color.parseColor("#D98BC34A"));
Toast.makeText(context, "Clicked "+position , Toast.LENGTH_LONG).show();
}
});
}
}
这是我的清单:
public class EventsList extends Fragment {
private SQLiteHandler db;
private ListView lv;
// Store instance variables
private String title;
private int page;
// newInstance constructor for creating fragment with arguments
public static EventsList newInstance(int page, String title) {
EventsList fragmentFirst = new EventsList();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("someTitle", title);
fragmentFirst.setArguments(args);
return fragmentFirst;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
page = getArguments().getInt("someInt", 0);
title = getArguments().getString("someTitle");
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
db = new SQLiteHandler(getActivity().getApplicationContext());
View view = inflater.inflate(R.layout.frag_event_list,container,false);
lv = (ListView) view.findViewById(R.id.programs_list);
makeMyList();
return view;
}
public void makeMyList() {
Cursor cursor = db.getEventsByDate(title);
String[] source = new String[]
{
AppConfig.EVENT_NAME,
AppConfig.EVENT_TIME,
};
int[] toView = new int[]
{
R.id.event_title,
R.id.event_time,
};
ProgramsCA scA = new ProgramsCA
(
getActivity(),
R.layout.list_program,
cursor,
source,
toView,
1
);
ListView myList = lv;
myList.setAdapter(scA);
clickMyListItem(myList);
}
public void clickMyListItem(ListView lv)
{
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Cursor cursor = (Cursor) adapterView.getItemAtPosition(i);
int iCol_Name = cursor.getColumnIndex(AppConfig.EVENT_SERVER_ID);
int sid = cursor.getInt(iCol_Name);
Intent intent = new Intent(getActivity().getBaseContext(),
ItemActivity.class);
intent.putExtra("icon", (int) R.drawable.ic_action_event);
intent.putExtra("title", "Program Detail");
intent.putExtra("s_id", (int) sid);
getActivity().startActivity(intent);
}
});
}
public void myClickHandler(View v)
{
ListView lvItems = lv;
for (int i=0; i < lvItems.getChildCount(); i++)
{
lvItems.getChildAt(i).setBackgroundColor(Color.BLUE);
}
//get the row the clicked button is in
RelativeLayout vwParentRow = (RelativeLayout)v.getParent();
int c = Color.CYAN;
vwParentRow.setBackgroundColor(c);
vwParentRow.refreshDrawableState();
}
}
答案 0 :(得分:0)
private selectedItem = -1;
然后在getView上添加:
if(position == selectedItem)
//set selected background color
else
//set normal background color
的onClick:
selectedItem = position;
notifiyDataSetChanged();
答案 1 :(得分:0)
尝试一下:
private int selectedItemSid = -1;
@Override
public void bindView(View view, final Context context, Cursor cursor) {
super.bindView(view, context, cursor);
final ImageView iv = (ImageView) view.findViewById(R.id.imageViewCalV);
final RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.fTheProgram);
int iCol_Name = cursor.getColumnIndex(AppConfig.EVENT_SERVER_ID);
final int sid = cursor.getInt(iCol_Name);
if (sid == selectedItemSid) {
iv.setBackgroundColor(Color.TRANSPARENT);
rl.setBackgroundColor(Color.parseColor("#D98BC34A"));
} else {//set normal background color,assume white
iv.setBackgroundColor(Color.WHITE);
rl.setBackgroundColor(Color.WHITE));
}
iv.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
selectedItemSid = sid;
notifiyDataSetChanged();
}
});
}