如何在默认选择的列表视图中制作项目?我知道在以前的stackoverflow帖子中已经回答了这个问题,但是我不能完全一样地使这项工作生效。我使用了论坛中指定的多种组合,但没有用。
这是我的代码:
// create the list view
ListView listView = (ListView) view.findViewById(R.id.list);
// create adapter
TimeAdapter adapter = new TimeAdapter(getContext(), stringArray, 3);
// set adapter to the list view
listView.setAdapter(adapter);
listView.setItemChecked(3, true);
listView.setSelection(3);
listView.performItemClick(listView.getSelectedView(), 3, 0);
我的XML布局
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.4"
android:choiceMode="singleChoice"
android:listSelector="@color/colorAccent" />
[更新] 我试图根据您在getView中的建议进行修改,但仍然没有帮助:(这次,突出显示了很多行,而不仅仅是一行!
public class TimeAdapter extends ArrayAdapter<String>{
private Context mContext;
private ArrayList<String> mArrayString;
private int mSelection;
public TimeAdapter(Context context, ArrayList<String> arrayList, int selection) {
super(context, 0);
mArrayString = arrayList;
mContext = context;
mSelection = selection;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.time_item, parent, false);
}
String time = mArrayString.get(position);
TextView mTimeTextView = listItemView.findViewById(R.id.time_text);
mTimeTextView.setText(time);
if (mSelection == position){
listItemView.setSelected(true);
listItemView.setBackgroundColor(mContext.
getColor(R.color.colorAccent));
}
return listItemView;
}
@Override
public String getItem(int position) {
return mArrayString.get(position);
}
@Override
public int getCount() {
return mArrayString.size();
}
}
答案 0 :(得分:-1)
您可以尝试以下方法:
// create the list view
final ListView listView = (ListView) findViewById(R.id.list);
// create adapter
final TimeAdapter adapter = new TimeAdapter(this, stringArray, 3);
// set adapter to the list view
listView.setAdapter(adapter);
listView.setSelection(3);
listView.setItemChecked(3, false);
listView.performItemClick(listView.getSelectedView(), 3, 0);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ListViewDemo.this, "position is :"+position, Toast.LENGTH_SHORT).show();
adapter.setSelection(position);
}
});
public class TimeAdapter extends ArrayAdapter<String> {
private static final int NOT_SELECTED = -1;
private Context mContext;
private ArrayList<String> mArrayString;
private int mSelection;
public TimeAdapter(Context context, ArrayList<String> arrayList, int selection) {
super(context, 0);
mArrayString = arrayList;
mContext = context;
mSelection = selection;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
String time = mArrayString.get(position);
TextView mTimeTextView = listItemView.findViewById(R.id.text);
mTimeTextView.setText(time);
/* if (mSelection == position) {
convertView.setSelected(true);
mTimeTextView.setTextColor(getResources().getColor(R.color.md_amber_800));
mTimeTextView.setBackground(new ColorDrawable(getResources().getColor(R.color.accent)));
}*/
if (position == mSelection) {
// your color for selected item
listItemView.setBackgroundColor(getResources().getColor(R.color.primary));
} else {
// your color for non-selected item
listItemView.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
return listItemView;
}
@Override
public String getItem(int position) {
return mArrayString.get(position);
}
@Override
public int getCount() {
return mArrayString.size();
}
// if called with the same position multiple lines it works as toggle
public void setSelection(int position) {
if (mSelection == position) {
mSelection = NOT_SELECTED;
} else {
mSelection = position;
}
notifyDataSetChanged();
}
}