简体   繁体   English

getView ListView所选项目的颜色更改

[英]getView ListView selected item color change

I am looking to set the selected item in getView() , is working however every item in my list is selected. 我想在getView()设置所选项目,但是我列表中的每个项目都被选中了。 I have tested with toasts and the correct is displayed so the condition is working. 我已经测试过吐司,并且显示正确,所以情况正常。 The condition checks to see if an entry from a DB for the specific item is set to true (thus being selected). 该条件检查以查看DB中针对特定项目的条目是否设置为true(因此处于选中状态)。

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    if(isItemSelected.equals("true")){
          listviewTitles.setBackgroundColor(0xAAAAFFFF);
     }
     else if (isItemSelected.equals("false")){
     // Default color    
     }
}

您应该按照以下所有条件更新背景色;

 listviewTitles.setBackgroundColor(isItemSelected.equals("true") ? selectedColor : unSelectedColor);

Try this and make these changes in your code hope it works out. 尝试此操作,并在代码中进行这些更改,希望能解决。

   @Override
 public View getView(int position, View convertView, ViewGroup parent) {  
        if (convertView == null) {    
        if (isItemSelected.equals("true")){
listviewTitles.setBackgroundColor(0xAAAAFFFF);
          }
          else if (isItemSelected.equals("false")){
            // Default color    
          }
            }else{

         if (isItemSelected.equals("true")){

    listviewTitles.setBackgroundColor(0xAAAAFFFF);
           }
          else if (isItemSelected.equals("false")){
             // Default color    
          } 
        }

please try this 请尝试这个

I believe I have a solution for your requirement, 我相信我有解决您的要求的方法,

Paste this code in your view file 将此代码粘贴到您的视图文件中

SparseBooleanArray singleChecked;

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        if (position != singleListSelectedPosition) {
            singleListSelectedPosition = position;
            int totalCount = lvSingleSelect.getCount();
            for (int i = 0; i < totalCount; i++) {
                if (i == position) {
                    boolean stat = singleChecked.get(position, false);
                    singleChecked.put(position, !stat);
                } else {
                    singleChecked.put(i, true);
                }
            }
             adapter.setChecked(singleChecked);
        }
    }

And this is your adapter class code: 这是您的适配器类代码:

public void setChecked(SparseBooleanArray ch) {
        singleChecked = ch;
        notifyDataSetChanged();
}

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

    if (singleChecked.get(position, false)) {
        convertView.setBackgroundColor(getResources()
                    .getColor(R.color.titlebar_background_color));
    } else {
        convertView.setBackgroundColor(
                    getResources().getColor(R.color.emphasis_color));
    }

Please let me know if you have any trouble with this, always happy to help. 如果您对此有任何疑问,请随时告诉我,我们随时乐意为您提供帮助。

I think you should try adding an extra value like boolean while adding data in your arraylist. 我认为您应该尝试在数组列表中添加数据时尝试添加布尔值等额外值。 true for selected and false for not selected. 选择为true,未选择为false。 Initially add false with all. 最初全部添加false。 And then when you click on listviewTitles 然后,当您单击listviewTitles

int positionClicked;

listviewTitles.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           for (int i = 0; i < bean_List.size(); i++) {
           if (i == position) {
                positionClicked = position;
                bean_List.get(position).setIsClicked(true);
                notifyDataSetChanged();
                //Do your task here...
           } else {
                bean_List.get(i).setIsClicked(false);
                notifyDataSetChanged();
           }
     }
});

And in getView() use this:- 在getView()中使用:

    if (bean_List.get(position).getIsClicked() == true) {
        listviewTitles.setBackgroundColor(0xAAAAFFFF);
        //change color accordingly
    } else {
        listviewTitles.setBackgroundColor(0xAAAAFFFF);
        //change color accordingly
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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