Android使用自定义适配器突出显示所选列表视图项目

时间:2016-01-11 11:15:48

标签: android listview android-listview

我需要你的帮助。我的问题是我需要突出显示所选的列表视图项。请注意,它只需要设置单个项目,而不是多个项目。

这是我迄今为止所做的尝试:

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

    final ViewHolder viewHolder;

    if(convertView == null) {

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = inflater.inflate(R.layout.list_video_item, parent, false);

        viewHolder = new ViewHolder();
        viewHolder.background = (LinearLayout) convertView.findViewById(R.id.background);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    result = new HashMap<>();
    result = data.get(position);

    if(firstTimeStartup && position == 0) { // default selected
        result = data.get(0);
        highlightCurrentRow(viewHolder.background);
    } else {
        unhighlightCurrentRow(viewHolder.background);
    }

    convertView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            result = data.get(position);

            if (firstTimeStartup) { // first time  highlight first row
                firstTimeStartup = false;
                result = data.get(0);
                unhighlight(viewHolder.background, 0);
            }

            highlightCurrentRow(viewHolder.background);
    });

    return convertView;

}

private void unhighlightCurrentRow(View rowView) {
    rowView.setBackgroundResource(android.R.color.transparent);
}

private void highlightCurrentRow(View rowView) {
    rowView.setBackgroundResource(R.color.main_blue);
}

private Boolean firstTimeStartup = true;

但问题是,当我点击convertView时,我无法取消突出显示该项目。当我尝试单击其他项目时,默认(第一个)项目也不会突出显示。非常感谢任何帮助。谢谢!

3 个答案:

答案 0 :(得分:0)

convertView.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        result = data.get(position);

        for(int i=0;i<data.size();i++){
              if(i == position){
                      highlightCurrentRow(viewHolder.background);
                  }
                else{
                    unhighlightCurrentRow(viewHolder.background);
                     }
              }


});

答案 1 :(得分:0)

创建一个这样的方法:

CustomAdapter extends BaseAdapter {

int selectedPosition=0;

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

 if(position==selectedPosition){
   highlightCurrentRow(viewHolder.background);
 }else{
     unhighlightCurrentRow(viewHolder.background);
 }




 }

  void highlightUnhighlight(int position){

 seslectedPostion= postion;
 notifyDatasetChanged

 }
 }

在onItemClickListener中调用highlightUnhighlight(postion)。

答案 2 :(得分:0)

我编辑了你的代码,请检查这段代码可能会解决您的问题。我在我的项目中使用了这段代码。

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

final ViewHolder viewHolder;

if(convertView == null) {

    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    convertView = inflater.inflate(R.layout.list_video_item, parent, false);

    viewHolder = new ViewHolder();
    viewHolder.background = (LinearLayout) convertView.findViewById(R.id.background);
} else {
    viewHolder = (ViewHolder) convertView.getTag();
}

result = new HashMap<>();
result = data.get(position);

if(firstTimeStartup[position]) { // default selected
    result = data.get(0);
    highlightCurrentRow(viewHolder.background);
} else {
    unhighlightCurrentRow(viewHolder.background);
}

convertView.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        result = data.get(position);

        if (firstTimeStartup[position]) { 
            Arrays.fill(firstTimeStartup, Boolean.FALSE);
            firstTimeStartup[position] = false;
            result = data.get(0);
            unhighlight(viewHolder.background, 0);
        }else{
        Arrays.fill(firstTimeStartup, Boolean.FALSE);
        firstTimeStartup[position] = true;
        highlightCurrentRow(viewHolder.background);
        }

   notifyDataSetChanged();
});

return convertView;
}

private void unhighlightCurrentRow(View rowView) {
rowView.setBackgroundResource(android.R.color.transparent);
}

private void highlightCurrentRow(View rowView) {
rowView.setBackgroundResource(R.color.main_blue);
}

 private Boolean firstTimeStartup[] = new Boolean[data.size()];
 firstTimeStartup[0]=true;