【问题标题】:Change background color and font color of selected item in listview更改列表视图中所选项目的背景颜色和字体颜色
【发布时间】:2016-03-16 07:37:58
【问题描述】:
  • 当我在列表视图的特定行上进行选择时,我想更改背景颜色和字体颜色

MyAdapter 类:

public class ListViewAdapter extends BaseAdapter {

    Context ctx;
    ArrayList<HashMap<String, String>> arraylist;
    LayoutInflater inflater;

    TextView a, b;
    String la, lb;

    String out;
    private int position_visible = -1;


    ListViewAdapter(Context ctx, ArrayList<HashMap<String, String>> arraylist) {

        this.ctx = ctx;
        this.arraylist = arraylist;
    }


    @Override
    public int getCount() {
        return arraylist.size();
    }

    @Override
    public Object getItem(int position) {
        return arraylist.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        inflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.listitem, parent, false);

       a = (TextView) itemView.findViewById(R.id.a);
        b = (TextView) itemView.findViewById(R.id.b);

        tvPlaceName.setText(arraylist.get(position).get("a"));

        a = arraylist.get(position).get("a");
        b = arraylist.get(position).get("b");

        if (position_visible == position) {
            tvPlaceName.setBackgroundResource(R.color.divider);
            tvTime.setBackgroundResource(R.color.divider);

        } else {
            tvPlaceName.setBackgroundResource(R.color.white);
            tvTime.setBackgroundResource(R.color.white);
        }

        return itemView;
    }

    public void show(int position) { // show a delete button on swipe of list
        // item position
        position_visible = position;
        notifyDataSetChanged();

    }

    }

MyList.java:

private int posionClicked = -1;

  lv = (ListView) findViewById(R.id.listView);



     lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);


        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            posionClicked = arg2;
            adapter.show(arg2);
        }

list.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:orientation="horizontal">


    <TextView
        android:padding="5dp"
        android:id="@+id/tvPlaceName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:minLines="2"
        android:gravity="center_vertical"
        android:text="Large Text"
        android:textStyle="bold"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#0052a5"
        android:textSize="@dimen/font_large"
        android:background="@color/white"/>


    <TextView
        android:background="@color/white"
        android:padding="5dp"
        android:id="@+id/tvTime"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="bottom"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#0052a5" />


</LinearLayout>

-- 当用户选择列表视图的任何一行时,它的背景颜色和字体颜色应该改变。

-- 衷心欢迎所有建议。

【问题讨论】:

标签: android listview


【解决方案1】:

这样做:

holder.view.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        //Get the text View and set the font as shown below
        TextView tv_the = (TextView) findViewById(R.id.the);
        Typeface face = Typeface.createFromAsset(getAssets(), "condenced.ttf");
        tv_the.setTypeface(face);

        tv_the.setTextColor(Color.parseColor("#ffffff"));

        //And set backgroundColor of whole item by following
        holder.view.setBackgroundColor(Color.parseColor("#FF7A83"));
    });
}

试试这个,让我知道它是否有效。

【讨论】:

  • 文字颜色怎么样?
  • 这会起作用,但如果您只需要单选,那么您需要在设置另一个选择之前将所有其他项目重置为默认状态。我会建议你选择 xml,让代码更简洁。
  • 完成检查答案
  • 基本上你需要给适配器布局的主标签提供id,当你点击视图时,颜色会相应改变
  • 但不正确。它的反应很奇怪.. 匿名行背景正在改变而不是被选中
【解决方案2】:

您可以将可绘制文件设置为背景和 textColor 到 TextView

示例:

 android:background="@drawable/selected_background"
 android:textColor="@color/selected_text"

selected_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
    <shape android:shape="rectangle">
        <solid android:color="@color/white"  />
        <corners android:radius="15dp"/>
    </shape>
</item>
<item>
    <shape android:shape="rectangle">
        <solid android:color="@color/darkGrey" />
        <corners android:radius="15dp"/>
    </shape>
  </item>

selected_text.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:color="@color/colorPrimary" android:state_selected="true" />
 <item android:color="@color/blue" />
</selector>

【讨论】:

    【解决方案3】:
    @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
       posionClicked = arg2;
       adapter.show(arg2);
       TextView text_item = (TextView) arg1.findViewById(R.id.id_of_item_listview);
       tv_the.setTextColor(Color.parseColor("#ffffff"));
    
        }
    

    试试吧!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多