2014-09-26 66 views
0

我遇到了ListView的问题。 我想,当它按下设置特殊颜色的项目上的ListView,所以我这个属性添加到ListView的XML:如何在按下和点击后在ListView上设置项目的背景?

android:listSelector="@color/popup_right_bg" 

而且它工作得很好。 但是,我也想保持它的点击操作后颜色的项目,所以我在适配器的getView()方法中添加以下代码:

if (position == selectedPosition){ 
     convertView.setBackgroundColor(context.getResources().getColor(R.color.popup_right_bg)); 
    }else{ 
     convertView.setBackgroundColor(context.getResources().getColor(R.color.popup_left_bg)); 
    } 

然后我遇到了一个问题:点击动作后, ListView上的项目颜色保持不变,但按下项目时的颜色缺失。这就是说下面的代码似乎没用。

android:listSelector="@color/popup_right_bg" 

谁能帮帮我?非常感谢。

+0

请问您可以发布适配器代码吗? – 2014-09-26 04:41:47

回答

0

在大家的帮助下,我的问题现在已经解决了。谢谢大家!
原因是,当我写这篇文章的代码:

if (position == selectedPosition){ 
    convertView.setBackgroundColor(context.getResources().getColor(R.color.popup_right_bg)); 
}else{ 
    convertView.setBackgroundColor(context.getResources().getColor(R.color.popup_left_bg)); 
} 

的“listSelector”写在XML将由convertView的背景覆盖。 所以,我只是创建了两个选择:
selector_left_normal:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_pressed="true" 
    android:drawable="@android:color/transparent"/> 

<item android:state_pressed="false" 
    android:drawable="@color/popup_left_bg"/> 

selector_left_selected:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_pressed="true" 
    android:drawable="@android:color/transparent"/> 

<item android:state_pressed="false" 
    android:drawable="@color/popup_right_bg"/> 

,并改成这样:

if (position == selectedPosition){ 
     convertView.setBackgroundResource(R.drawable.selector_left_selected); 
    }else{ 
     convertView.setBackgroundResource(R.drawable.selector_left_normal); 
    } 

当按下时,convertView的背景将是透明的,所以它下面的“listSelector”变得可见。

0

请试试这个:

   android:choiceMode="multipleChoice" 

在XML

加入这一行你的列表视图,并选择这样

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@android:color/transparent"></item> 
    <item android:state_selected="true" android:drawable="@android:color/transparent"></item> 
    <item android:state_focused="true" android:drawable="@android:color/transparent"></item> 
    <item android:drawable="@drawable/list_item_background_normal"></item> 
</selector> 
0

我已经为它设置为我的适配器,上午粘贴代码..所以你可能会得到参考

package com.learningperiod.functional; 

import java.util.ArrayList; 

import android.content.Context; 
import android.graphics.Color; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class BlogArrayAdapter extends BaseAdapter { 

ArrayList<BlogURLObject> blogArrayList = new ArrayList<BlogURLObject>(); 
BlogURLObject blogObj = new BlogURLObject(); 
private int selectedIndex; 

Context mContext; 

private int selectedColor = Color.parseColor("#1b1b1b"); 


public BlogArrayAdapter(Context context, int resource, 
     ArrayList<BlogURLObject> blogArrayList) { 


    this.blogArrayList = blogArrayList; 
    this.mContext = context ; 
    selectedIndex = -1; 
} 

public void setSelectedIndex(int ind) 
{ 
    selectedIndex = ind; 
    notifyDataSetChanged(); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    int positionInt = position; 

    ViewHolder viewHolder; 

    View view = convertView; 
    blogObj = new BlogURLObject(); 
    blogObj = blogArrayList.get(positionInt); 


    if (view == null) { 

      viewHolder = new ViewHolder(); 

     LayoutInflater inflater = (LayoutInflater) mContext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     view = inflater.inflate(R.layout.list_item, null); 

     viewHolder.blogNameTextView = (TextView) view 
       .findViewById(R.id.blogNameTextView); 

     viewHolder.layoutCell = (LinearLayout) view 
        .findViewById(R.id.layout_cell); 


     view.setTag(viewHolder); 
    } 
    else 
    { 

     viewHolder = (ViewHolder) view.getTag(); 
    } 


    String blogObjString = blogObj.getTitle(); 

    if(selectedIndex!= -1 && position == selectedIndex) 
    { 
     viewHolder.layoutCell.setBackgroundColor(mContext.getResources().getColor(R.color.pressed_color)); 
    } 
    else 
    { 
     viewHolder.layoutCell.setBackgroundColor(mContext.getResources().getColor(R.color.default_color)); 
    } 

    viewHolder.blogNameTextView.setText(blogObjString); 

    return view; 
} 


class ViewHolder 
{ 
    LinearLayout layoutCell; 
    TextView blogNameTextView; 
} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return blogArrayList.size(); 
} 

@Override 
public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return blogArrayList.get(position); 
} 

@Override 
public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 
} 

,并在ListView`s onItemClick事件

@Override 
    public void onItemClick(AdapterView<?> adapterView, View view, 
      int position, long id) { 

     // view.setSelected(true); 

     adapter.setSelectedIndex(position); // do not forgot this 


    } 
0

你不能将背景设置为列表视图。

如果要将背景状态设置为列表项目,请将背景设置为适配器布局。 (即,为每个列表添加一个适配器,该适配器只包含一个布局,只是一个TextView。)

对于Textview将背景设置为sam在他的答案中所述。

相关问题