如何根据位置更改listView项目的颜色
Posted
技术标签:
【中文标题】如何根据位置更改listView项目的颜色【英文标题】:How to change color of listView item depending on position 【发布时间】:2020-12-03 15:11:29 【问题描述】:如何根据位置更改 listView 项目的颜色?我不能解决这个问题大约 4 天。也许有人可以帮助我。
我有 customView 类:
public class CustomView extends View
private String[] hoursList = getResources().getStringArray(R.array.hours); ;
private String[] minutesList = getResources().getStringArray(R.array.minutes);;
private String[] dayOfTimeList = getResources().getStringArray(R.array.timeOfDay);
private ListView listView;
private HourAdapter hourAdapter;
private MinuteAdapter minuteAdapter;
private DayOfTimeAdapter dayOfTimeAdapter;
public static int middlePosition;
public CustomView(Context context)
super(context);
public CustomView(Context context, ViewGroup viewGroup)
super(context);
inflate(context, R.layout.custom_test, viewGroup);
listView = viewGroup.findViewById(R.id.hours_list);
hourAdapter = new HourAdapter(context, hoursList);
listView.setAdapter(hourAdapter);
listView.setOnScrollListener(new AbsListView.OnScrollListener()
@Override
public void onScrollStateChanged(AbsListView view, int scrollState)
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
middlePosition = firstVisibleItem+1;
);
和适配器类:
public class HourAdapter extends BaseAdapter
private LayoutInflater lInflater;
private String[] hoursValueList;
public HourAdapter(Context context, String[] hoursValueList
lInflater = LayoutInflater.from(context);
this.hoursValueList = hoursValueList;
@Override
public View getView(int position, View convertView,ViewGroup parent)
View view = convertView;
if (view == null)
view = lInflater.inflate(R.layout.row, parent, false);
TextView textHours = view.findViewById(R.id.textRow);
textHours.setText(hoursValueList[position]);
if (position == CustomView.middlePosition)
view.setBackgroundResource(R.drawable.selected_color);
return view;
What I need: when any element is in the middle position it has another color.
但是使用我现在拥有的代码,它不起作用。每个第二个元素都有颜色, 但我需要将任何位于中间位置的元素着色
【问题讨论】:
您遇到了什么样的问题? 我得到了图片中的结果。元素未绘制。 图片我看不到 对不起,我加了。 【参考方案1】:试试下面的代码:
CustomAdapter.java:
public class CustomAdapter extends BaseAdapter
private LayoutInflater lInflater;
private String[] valueList;
public CustomAdapter(Context context, String[] valueList)
lInflater = LayoutInflater.from(context);
this.valueList = valueList;
@Override
public int getCount()
return valueList.length;
@Override
public Object getItem(int position)
return valueList[position];
@Override
public long getItemId(int position)
return position;
@Override
public View getView(int position, View convertView, ViewGroup parent)
View view = convertView;
if (view == null)
view = lInflater.inflate(R.layout.row, parent, false);
TextView text = view.findViewById(R.id.textRow);
text.setText(valueList[position]);
return view;
CustomView.java:
public class CustomView extends View
private String[] hoursList;
private String[] minutesList;
private String[] dayOfTimeList;
private ListView hoursListView;
private ListView minutesListView;
private ListView dayOfTimeListView;
private CustomAdapter hoursAdapter;
private CustomAdapter minutesAdapter;
private CustomAdapter dayOfTimeAdapter;
AbsListView.OnScrollListener scrollListener = new AbsListView.OnScrollListener()
@Override
public void onScrollStateChanged(AbsListView view, int scrollState)
if (scrollState == SCROLL_STATE_IDLE)
View firstView = view.getChildAt(0);
int offset = view.getTop() - firstView.getTop();
if (offset > (firstView.getHeight() / 2))
view.smoothScrollToPosition(view.getLastVisiblePosition());
else
view.smoothScrollToPosition(view.getFirstVisiblePosition());
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
for (int i = 0; i < view.getChildCount(); i++)
View childView = view.getChildAt(i);
if (i == 1) childView.setBackgroundColor(Color.BLUE); else childView.setBackgroundColor(Color.WHITE);
;
public CustomView(Context context)
super(context);
public CustomView(Context context, ViewGroup viewGroup)
super(context);
inflate(context, R.layout.custom_test, viewGroup);
hoursList = new String[26];
hoursList[0] = " ";
hoursList[25] = " ";
for (int i = 1; i < hoursList.length - 1; i++) hoursList[i] = String.valueOf(i);
minutesList = new String[62];
minutesList[0] = " ";
minutesList[61] = " ";
for (int i = 1; i < minutesList.length - 1; i++) minutesList[i] = String.valueOf(i);
dayOfTimeList = new String[4];
dayOfTimeList[0] = " ";
dayOfTimeList[1] = "AM";
dayOfTimeList[2] = "PM";
dayOfTimeList[3] = " ";
hoursListView = viewGroup.findViewById(R.id.hours_list);
hoursAdapter = new CustomAdapter(context, hoursList);
hoursListView.setAdapter(hoursAdapter);
minutesListView = viewGroup.findViewById(R.id.minutes_list);
minutesAdapter = new CustomAdapter(context, minutesList);
minutesListView.setAdapter(minutesAdapter);
dayOfTimeListView = viewGroup.findViewById(R.id.dayOfTime_list);
dayOfTimeAdapter = new CustomAdapter(context, dayOfTimeList);
dayOfTimeListView.setAdapter(dayOfTimeAdapter);
hoursListView.setOnScrollListener(scrollListener);
minutesListView.setOnScrollListener(scrollListener);
dayOfTimeListView.setOnScrollListener(scrollListener);
要为第二个元素着色,需要重置其他元素的颜色。请注意,如果最初显示 3 行,则在滚动期间/之后可能会有 4 行(2 部分显示和 2 完全显示)。第二个元素可能偏离中间位置。
【讨论】:
以上是关于如何根据位置更改listView项目的颜色的主要内容,如果未能解决你的问题,请参考以下文章
Flutter:更改 ListView.builder 中选定按钮的背景颜色