我正在使用导航抽屉菜单,其中列表视图被夸大。我基本上使用Eclipse提供的内置导航抽屉。
以下代码段显示列表视图。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
mDrawerListView.setAdapter(new ArrayAdapter<String>(
getActionBar().getThemedContext(),
android.R.layout.simple_list_item_1,
android.R.id.text1,
new String[]{
getString(R.string.title_section1),
getString(R.string.title_section2),
getString(R.string.title_section3),getString(R.string.title_section4),
getString(R.string.title_section5),
getString(R.string.title_section6)}));
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}
我想更改列表项的文本颜色。 到目前为止,我已经尝试创建单独的xml文件,然后甚至尝试使用适配器替换 - “android.R.id.text1”与“R.layout.my_layout”或“R.id.mytext_id”但是改变没有生效。 建议表示赞赏。
答案 0 :(得分:0)
正如我在上面的评论中所说,处理此问题的一个选项是继承ArrayAdapter或BaseAdapter。下面是一个例子(我选择子类BaseAdapter):
static class ViewHolder {
TextView tv;
}
private class CustomAdapter extends BaseAdapter{
private ArrayList<String> items;
private Context context;
private int selectedPosition;
public CustomAdapter(Context context, ArrayList<String> items){
this.items = items;
this.context = context;
}
@Override
public int getCount() {
return items != null ? items.size() : 0;
}
@Override
public Object getItem(int i) {
return items != null ? items.get(i) : null;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup container) {
ViewHolder holder = null;
if(convertView == null){
// You should create your own custom layout for the listview cell.
convertView = LayoutInflater.from(context).inflate(R.layout.your_custom_row_layout);
holder = new ViewHolder();
holder.tv = convertView.findViewById(R.id.your_textview);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
String item = items.get(position);
if(position == selectedPosition){
// Here you set the selected text color
holder.tv.setTextColor(Color.RED);
}else{
holder.tv.setTextColor(Color.BLACK);
}
return convertView;
}
}
然后,在Activity中,创建CustomAdapter的实例并在ListView上设置它。