我有一个ListView
,其中包含Fragment
。在onCreateView
部分,我为列表设置了onItemClickListener
。我的目的是突出显示所选项目,但点击ListViewItems
之间的空格是彩色而不是项目本身。
我也考虑使用Selector
,但一直没有成功,IE:view.setBackgroundDrawable(view.getContext().getResources().getDrawable(R.drawable.list_selector));
有没有办法纠正这个?或者是否需要使用Row
引入新的LayoutInflater
?提前谢谢。
片段
public static class FragmentRoutine extends Fragment {
DatabaseHandler db;
private ListView routineListView;
private List<Routine> routines = new ArrayList<Routine>();
ArrayAdapter<Routine> routineAdapter;
Routine longClickedItemRoutines;
private SelectedAdapter selectedAdapter;
ImageButton btnMoveUp,btnMoveDown;
public FragmentRoutine() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.routines,
container, false);
db = new DatabaseHandler(getActivity().getApplicationContext());
routineListView = (ListView) rootView.findViewById(R.id.routineList);
registerForContextMenu(routineListView);
db.closeDB();
if (db.getRoutineCount() != 0)
routines.clear();
routines.addAll(db.getAllRoutines());
populateList();
selectedAdapter = new SelectedAdapter(this.getActivity(),0,routines);
selectedAdapter.setNotifyOnChange(true);
routineListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
longClickedItemRoutines = routines.get(position);
return false;
}
});
routineListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView, View view,
int position, long id) {
for (int h = 0; h < adapterView.getChildCount(); h++)
adapterView.getChildAt(h).setBackgroundColor(Color.TRANSPARENT);
// change the background color of the selected element
view.setBackgroundColor(Color.LTGRAY);
}
});// move up event handler
ImageButton btnMoveUp =(ImageButton) rootView.findViewById(R.id.btnMoveUp);
btnMoveUp.setOnClickListener(new AdapterView.OnClickListener() {
public void onClick(View arg0) {
moveUp();
}
});
// move down event handler
ImageButton btnMoveDown =(ImageButton) rootView.findViewById(R.id.btnMoveDown);
btnMoveDown.setOnClickListener(new AdapterView.OnClickListener() {
public void onClick(View arg0) {
moveDown();
}
});
setHasOptionsMenu(true);
return rootView;
}
答案 0 :(得分:1)
您不应更新setOnItemClickListener
中的视图,而是记录此功能中的位置:
<强>更新强>
mActiveListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
sSelectedIndex = position;
// notify ListView's ArrayAdapter that the data attached has been changed,
// and any View reflecting the data set should refresh itself.
yourArrayAdapter.notifyDataSetChanged();
}
}
然后在getView
方法中更新您的片段列表视图:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_menu, null);
}
if (position == sSelectedIndex) {
convertView.setBackgroundColor(...);
} else {
convertView.setBackgroundColor(...);
}
return convertView;
}
答案 1 :(得分:0)
错误位于routineListView上的setOnItemClickListener中。
更具体地说是view.setBackgroundColor(Color.LTGRAY);
部分
您在此处传递的视图是ListView,而不是单击的列表元素。因此,您要设置整个列表视图的背景颜色而不是单个元素。但是单个元素位于列表视图的顶部,因此您可以看到颜色的唯一位置在列表元素之间。
要纠正此问题,您应该执行以下操作:
routines.get(position).setBackgroundColor(Color.LTGRAY);
这将正确获取点击的列表元素。