public void setDataToList() {
liveTvAdapter = new LiveTvAdapter(this, channelList);
horizonatalListViewForLiveTv.setAdapter(liveTvAdapter);
horizonatalListViewForLiveTv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView;
for (int i = 0; i < parent.getChildCount(); i++) {
if (position == i) {
textView = (TextView) parent.getChildAt(i).findViewById(R.id.live_tv_chanel_text);
textView.setTextColor(Color.parseColor("#63a0b9"));
} else {
textView = (TextView) parent.getChildAt(i).findViewById(R.id.live_tv_chanel_text);
textView.setTextColor(Color.BLACK);
}
}
if (videoView != null) {
videoView = null;
}
setValue(position);
}
});
}
这是我正在做的代码。在这里,我从父母的位置获取textview。在此代码中,当我单击第三个或第四个项目时,项目不会着色,尽管其他项目会变色。
答案 0 :(得分:1)
在适配器的onClick中,您可以使用notifyItemChanged(oldPosition)
https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#notifyItemChanged(int, java.lang.Object)
答案 1 :(得分:0)
首先我在我的适配器中制作了一个方法
public void setSelectedIndex(int ind) {
selectedIndex = ind; // selectedIndex global variable
notifyDataSetChanged();
}
而不是
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolderForLiveTV holderForLiveTV;
if (selectedIndex != -1 && position == selectedIndex) {
holderForLiveTV.channelName.setTextColor(Color.parseColor("#63a0b9"));
} else {
holderForLiveTV.channelName.setTextColor(Color.BLACK);
}
return convertView;
}
然后我像这样设置所选项目的位置: -
liveTvAdapter.setSelectedIndex(position);