简体   繁体   中英

how to change the color of the specific listview's item or element

I have a ListView which is inflated by a array of String. On top of the ListView I've added a button, and I want whenever user clicks on the button, the color of the 4 item changes automatically.

here is my Activity Code:

    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;
    }
}

And also here is my listview.xml File:

<?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>

and 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>

So my question is, how to change a specific item's color by clicking on the button.

Like that?

    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> );

If you need to change all items, you can use cicle with iterator or simple For like this:

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

Since the Listview re-uses views when scrolled, you need to re-set the color in the adapter's getView method.

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;
    }
}

And then have the button click listener, set the boolean:

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

In the same way you could also create a variable position inside the adapter and use it to highlight a specific item.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM