更改特定视图属性

更改特定视图属性

本文介绍了Android的列表视图,更改特定视图属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的列表视图,我滚动到它的这样一个特定的位置

on my listview I am scrolling to a particular position of it like this

getListView()smoothScrollToPosition(的scrollPosition + 1);

getListView().smoothScrollToPosition(scrollposition+1);

但我也想突出这一观点。我如何进入这期特别的属性改变这一立场的背景颜色。

but I also want to "highlight" this view. How do I access the attributes of this particular view to change the background color of this position.

下面是列表视图code

Here is the listview code

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/currentText"
    android:padding="10dp" android:textSize="24sp" android:textColor="#000000" android:background="@drawable/bglistitem"
    android:gravity="center_vertical|center_horizontal">
</TextView>

这仅仅是一个带有背景的TextView。我试着这样做:

It is just a textview with a background. I tried doing this:

View highlightView = getListView().getChildAt(scrollposition);
highlightView.setDrawingCacheBackgroundColor(Color.parseColor("#2580b0"));
TextView currentText = (TextView)highlightView.findViewById(R.id.currentText);
currentText.setTextColor(Color.parseColor("#ffffff"));

但它总是崩溃!我能理解为什么 setDrawingCacheBackgroundColor 崩溃,但即使从这一观点得到碰撞TextView的。

but it always crashes! I can understand why setDrawingCacheBackgroundColorcrashes, but even getting the textview from that view crashes.

帮助?

推荐答案

我遇到了这个问题在几个星期前。

I ran into this issue a few weeks ago.

您不应该使用的ListView的getChildAt()函数。如果你使用getChildAt(5),例如,它会拉5日认为,是的可见

You should not use the getChildAt() function of the ListView. If you use getChildAt(5), for example, it will pull the 5th view that is visible.

要解决这一问题,我不得不创建推翻getView(自定义适配器),并设置基于什么我给对面的颜色。

To solve the issue, I had to create a custom adapter that overrode getView() and set the colors based on what I sent across.

  public class RunAdapter extends SimpleAdapter {

public RunAdapter(Context context, List<HashMap<String, String>> items,
        int resource, String[] from, int[] to) {
    super(context, items, resource, from, to);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
    TextView txtView = (TextView) view.findViewById(R.id.lstTurnout);
        if (txtView.getText().toString().contains("BLU")) {
            txtView.setBackgroundColor(Color.rgb(0x00, 0x00, 0xaa));
            txtView.setText(txtView.getText().toString().replace("BLU", ""));
        } else {
            txtView.setBackgroundColor(Color.rgb(0xaa, 0x00, 0x00));
            txtView.setText(txtView.getText().toString().replace("RED", ""));
        }
  }

这不是prettiest方法,但它的工程!
根据什么颜色我想TextView的是,我通过BLU或红色
然后在getView,我检查,看看哪些字符串包含并相应地改变颜色。

It isn't the prettiest method, but it works!Depending on what color I wanted the TextView to be, I passed 'BLU', or 'RED'Then in the getView, I check to see which the string contains and change the color accordingly.

希望我一些帮助,祝您好运!

Hope I was some help, Good Luck!

修改
构造函数每个请求

EditConstructor as per request

这篇关于Android的列表视图,更改特定视图属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 03:41