我在CustomAdapter类中成功实现了Rounded Corner ListView,如下所示:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
if (v == null) {
viewHolder = new ViewHolder();
v = layoutInflater.inflate(layout, null);
viewHolder.textTitle = (TextView) v.findViewById(R.id.textTitle);
if (position == 0 && arrayList.size() == 1) {
v.setBackgroundResource(R.drawable.top_list_selector);
}
else if (position == 0) {
v.setBackgroundResource(R.drawable.top_list_selector);
}
else if (position == arrayList.size() - 1) {
v.setBackgroundResource(R.drawable.bottom_list_selector);
} else {
v.setBackgroundResource(R.drawable.mid_list_selector);
}
v.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) v.getTag();
}
但我不知道如何在下面的课程中实现相同的内容,我确实理解,我必须使用arrayList
代替countryList
但view
和{{ 1}}?
position
答案 0 :(得分:1)
在1代码示例中,用户创建了一个自定义适配器来显示项目的列表,如
MyCustomeAdapter extends BaseAdapter
在您的代码中,您只是显示已构建的适配器,它不支持Override
getView()
方法,可以在其中进行所有这些更改。
因此,为了完成示例所做的操作,请创建一个类
static class MyListAdapter extends BaseAdapter {
private List<View> views = new ArrayList<View>();
public void addView(View view){
views.add(view);
}
public int getCount() {
return views.size();
}
public Object getItem(int position) {
return views.get(position).getTag();
}
public long getItemId(int position) {
return views.get(position).getId();
}
public View getView(int position, View convertView, ViewGroup parent) {
return views.get(position);
}
}
而不是做
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),
R.layout.adapter_social, countryList);
待办事项
MyListAdapter listAdapter = new MyListAdapter();
LayoutInflater inflater = getLayoutInflater();
int item_layout = 0 ;
for(int i = 0 ; i < values.length;i++) {
//decide which layout to show depending on the item position
if(i==0) {
item_layout = R.layout.top_list_layout ;
} else if(i==values.length-1) {
item_layout = R.layout.bottom_list_layout;
} else {
item_layout = R.layout.mid_list_layout;
}
TextView button = (TextView)inflater.inflate(item_layout, null);
button.setText(values[i]);
button.setTag(new Integer(i));
listAdapter.addView(button);
}
//lv.setSelector(R.color.transparent);
lv.setAdapter(listAdapter);