Listview更改项目按钮上的文本颜色单击

时间:2016-12-27 12:30:58

标签: android listview row

我有一个带有自定义适配器的列表视图,当我点击第一个项目时,列表视图下方的按钮(“下一个”)可见,第一个行项目文本颜色发生变化。 单击按钮下一步我想更改下一个行项文本颜色。请帮助谢谢。

  mainListViews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    itemPosition =i;
            String text = textView.getText().toString();
            Toast.makeText(getContext(), "Selected item: " + text + " - " + i, Toast.LENGTH_SHORT).show();

            song_namef.setText(text);

            playSong(i);
            hello.setVisibility(View.VISIBLE);

            setItemNormal();
            View rowView = view;
            setItemSelected(rowView);

        }
    });
 public void setItemSelected(View view){
    View rowView = view;
    TextView tv = (TextView)rowView.findViewById(R.id.textView);
    tv.setTextColor(Color.WHITE);
}

public void setItemNormal()
{
    for (int i=0; i< mainListViews.getChildCount(); i++)
    {
        View v = mainListViews.getChildAt(i);
        TextView txtview = ((TextView)v.findViewById(R.id.textView));
        txtview.setTextColor(getResources().getColor(R.color.tabsScrollColor));
    }}
hello.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
   Toast.makeText(getContext(),"Next",Toast.LENGTH_SHORT).show();

                itemPosition=itemPosition+1;
here i want to color next row of item text
}});

getView

 public View getView(final int position, View convertView, ViewGroup viewGroup) {

         val = position;
        viewHolder = null;

        if (convertView == null) {
            viewHolder = new ViewHolder();
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = layoutInflater.inflate(R.layout.list_items, viewGroup, false);

            // Find the child views.
            viewHolder.country = (TextView) convertView.findViewById(R.id.textView);
            viewHolder.cancel = (ImageView)convertView.findViewById(R.id.button);



            convertView.setTag(viewHolder);
            //  convertView.setTag(vh);
        }
        else {
            viewHolder = (ViewHolder) convertView.getTag();
            //vh = (ViewHolder)vi.getTag();
        }
        String hello = countryList.get(position);
        Log.d("Hello",hello);


        viewHolder.country.setText(hello.substring(18));

2 个答案:

答案 0 :(得分:1)

首先创建一个selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@color/black" />
<item android:state_focused="true" android:color="@color/black" />
<item android:state_pressed="true" android:color="@color/black" />
<item android:color="@color/white" />
</selector> 

在List的项目布局中,将textColor属性定义为:

android:textColor="@color/list_item_text"

答案 1 :(得分:1)

试试这个:

hello.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Toast.makeText(getContext(),"Next",Toast.LENGTH_SHORT).show();
        itemPosition = itemPosition + 1;

        setItemNormal();
        // check if the next view is visible on screen, then change color
        if(itemPosition <= mainListViews.getLastVisiblePosition()){
            View rowView = mainListViews.getChildAt(itemPosition - mainListViews.getFirstVisiblePosition());
            setItemSelected(rowView);
        }
}});

希望有所帮助!

顺便说一句,直接修改视图是不好的,因为当滚动列表时将重绘视图。如果您的列表很长,请查看您的代码。还给出了变量合适的名称,例如“hello” - &gt;“btnNext”,将帮助其他人了解您的代码并提出解决方案。