如何更改特定列表视图项或元素的颜色

时间:2014-11-11 15:53:53

标签: android android-layout listview android-listview

我有一个由String数组充气的ListView。在ListView的顶部,我添加了一个按钮,我希望每当用户点击按钮时,4项的颜色会自动更改。

这是我的活动代码:

    public class MainActivity extends Activity
    {
    String[] countryNames = {"Germany", "France", "Italy", "Spain", "Russia", "USA", "Iran", "China", "Inida"};
    Button button;
    ListView listView;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);

        button = (Button) findViewById(R.id.button_changeColor);

        ListViewAdapter adapter = new ListViewAdapter(getApplicationContext());//
        listView = (ListView) findViewById(R.id.listView_1);//
        listView.setAdapter(adapter);//


        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // here i want to change the the color 4th item.
            }
        });


    }


    private class ListViewAdapter extends BaseAdapter
    {
        LayoutInflater layoutInflater;

        public ListViewAdapter(Context context)
        {
            layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount()
        {
            return countryNames.length;
        }

        @Override
        public Object getItem(int position)
        {
            return null;
        }

        @Override
        public long getItemId(int position)
        {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {

            ViewHolder viewHolder;
            if (convertView == null)
            {
                convertView = layoutInflater.inflate(R.layout.listview_template, null);

                viewHolder = new ViewHolder();
                viewHolder.addedTextView = (TextView) convertView.findViewById(R.id.textView_Template);
                convertView.setTag(viewHolder);
            } else
            {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.addedTextView.setText(countryNames[position]);

            return convertView;
        }
    }

    private class ViewHolder
    {
        TextView addedTextView;
    }
}

这里也是我的listview.xml文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <Button
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Change Color"
            android:id="@+id/button_changeColor"/>
    <ListView
            android:layout_width="wrap_content"
            android:layout_height="300dp"
            android:id="@+id/listView_1">
    </ListView>
</LinearLayout>

和finaly listview_template.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView_Template"
            />
</LinearLayout>

所以我的问题是,如何通过点击按钮来更改特定项目的颜色。

2 个答案:

答案 0 :(得分:5)

喜欢那个?

    listView.getChildAt( 4 );
    View item_4 = listView.getChildAt(4);
    item_4.setBackgroundResource( <new_background> );
    ( (TextView) item_4.findViewById( R.id.text1 ) ).setTextColor( <new_font_color> );

如果你需要更改所有项目,你可以使用带有迭代器的cicle或简单如下:

for(int i=0;i<listView.getChildCount();i++) {
  View item = listView.getChildAt(i);
  // some yours code
}

答案 1 :(得分:0)

由于Listview在滚动时重新使用了视图,因此您需要在适配器的getView方法中重新设置颜色。

final ListViewAdapter adapter = new ListViewAdapter(getApplicationContext()) {
    public boolean isHighlighted;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        super.getView(position, convertView, parent);
        if (position == 4 && isHighlighted) {
            convertView.setBackgroundColor(Color.RED);
        }
        return convertView;
    }
}

然后让按钮单击监听器,设置布尔值:

button.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        adapter.isHighlighted = true;
    }
});

同样,您也可以在适配器中创建变量position,并使用它来突出显示特定项目。