我想永久更改ListView中TextView(列表项)的颜色?
我想要新闻,如果我打开所有项目都会突出显示当我点击一个项目时,颜色会变为某种暗淡的外观并且它会永久粘贴。
但是当我滚动ListView时,Color会更改为其他List Item 在下面的图像我刚刚点击“关于杜尔加”我再次回到这个屏幕,我滚动ListView然后其他项目也在影响。即使我没有点击。
请发送任何代码。 [![在此处输入图像说明] [1]] [1]
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
// Toast.makeText(getApplicationContext(),view.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
defaultSet.add("no data");
setOfClickedItems.add(id + "");
// parent.setBackgroundColor(Color.LTGRAY);
final SharedPreferences prefs = getSharedPreferences("disablePref", MODE_PRIVATE);
final SharedPreferences.Editor editor = prefs.edit();
Set<String> disableList = prefs.getStringSet("disable_option", defaultSet);
Iterator<String> iterator = disableList.iterator();
while (iterator.hasNext()) {
setOfClickedItems.add(iterator.next());
}
editor.putStringSet("disable_option", setOfClickedItems);
editor.commit();
Log.e("putshared",setOfClickedItems.size()+ "set size" + setOfClickedItems);
Intent newsIntent = new Intent(MainActivity.this, NewsDescriptionActivity.class);
newsIntent.putExtra("title", ((TextView) view.findViewById(R.id.news_heading)).getText().toString());
newsIntent.putExtra("image_url", newsItems.get(position).getImgUrl());
newsIntent.putExtra("summary", newsItems.get(position).getSummary());
newsIntent.putExtra("desc", newsItems.get(position).getDescription());
startActivity(newsIntent);
}
});
我的适配器
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
defaultSet.add("default");
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
font = Typeface.createFromAsset(context.getAssets(), "fonts/fontawesome-webfont.ttf");
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_news_reader, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.news_heading);
holder.imageView = (ImageView) convertView.findViewById(R.id.news_image);
holder.buttonView = (TextView) convertView.findViewById(R.id.forward_button);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
News rowItem = (News) getItem(position);
String img_url = rowItem.getImgUrl();
URL newurl = null;
Bitmap mIcon_val = null;
try {
newurl = new URL("http://10.2.195.165:9000" + img_url);
Log.e("NEW URL", "++++++++++" + newurl);
mIcon_val = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
Log.e("BitMap", "++++++++++" + mIcon_val);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
final SharedPreferences prefs = context.getSharedPreferences("disablePref",Context.MODE_PRIVATE);
Set<String> disableList = prefs.getStringSet("disable_option",defaultSet);
Log.e("setsize",disableList+"items"+disableList.size());
Iterator<String> iterator = disableList.iterator();
while (iterator.hasNext()) {
String iteratorValue = iterator.next();
Log.e("inside iterator",""+iteratorValue+"view position"+position);
if((position+"").equalsIgnoreCase(iteratorValue)) {
Log.e("inside if",""+iteratorValue+"matched position"+position);
// convertView.setBackgroundColor(Color.LTGRAY);
holder.txtTitle.setTextColor(Color.GRAY);
}
}
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageBitmap(mIcon_val);
holder.buttonView.setTypeface(font);
return convertView;
}
答案 0 :(得分:0)
getView()会回收您的视图。您在getView中将项目的文本颜色更改为灰色,并且该视图将被回收。因此,即使您未点击的项目文本也会变为灰色。
在你的getView()中,你只定义了单击文本时应该具有的颜色。但是,如果未单击“视图”,则忘记定义颜色应该是什么。
将while循环中的代码更改为this(同时添加boolean wasClicked):
boolean wasClicked = false; //false by default
while (iterator.hasNext()) {
String iteratorValue = iterator.next();
Log.e("inside iterator",""+iteratorValue+"view position"+position);
if((position+"").equalsIgnoreCase(iteratorValue)) {
wasClicked = true; //item was clicked
}
}
//now let's change the text color based on if the item was clicked or not
if(wasClicked){ //set color appropriate for clicked items
holder.txtTitle.setTextColor(Color.GRAY);
}else{ //item wasn't clicked
holder.txtTitle.setTextColor(Color.BLACK); //or whatever color you want
}
答案 1 :(得分:0)
你基本上需要在你的适配器中存储每个列表项的状态(即点击它),然后在适配器getView(position ...)
中你必须调整视图的背景颜色。
您的随机行为源于重用视图(它们维持不同getView(position ...)
之间的状态。)
答案 2 :(得分:0)
您必须按照此步骤执行此操作
1: - 转到item_news_reader.xml文件 2: - 您在同一个xml文件中给出文本字段和图像的文件,您可以通过此行设置文本颜色
android:textColor="#000000"
或以编程方式试用
extView.setTextColor(Color.parseColor("#bdbdbd"));