onListViewItemClick查看参数是指多个时listItems [英] onListViewItemClick View argument refer to multiple listitems

查看:182
本文介绍了onListViewItemClick查看参数是指多个时listItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在onListItemClick我想改变单击项目的背景下,这是我的code:

 公共无效onListItemClick(ListView的父母,视图V,INT位置,长的id){    如果(位置!= previous_position){
        v.setBackgroundResource(R.drawable.category_clicked_item_bg);
        如果(previous_category!= NULL)
            previous_category.setBackgroundResource(R.drawable.category_item_bg);
        previous_category = V;
        previous_position =位置;
    }
}

previous_category和previous_position保护变量。

问题是,上项目单击列表中的多个项目被突出显示(更具体地说1或2)。在列表中有20行,如果我点击之一的中间,只有一个被突出,如果我点击开始或结束时,对面另一列获取与间隔13行突出显示的任何行(这可能是为什么pressing的原因中间排不高亮秒)。

我需要列表强调只有一行单击的之一,这是什么问题?

其他code

如果可以帮助,这里也ListView控件适配器类:

 包domehotel.guestbook.page.category;进口android.app.Activity;
进口android.content.Context;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.ImageView;
进口android.widget.TextView;
进口domehotel.guestbook.R;公共类CategoryAdapter扩展ArrayAdapter< CategoryItem> {上下文语境;
INT layoutResourceId;
CategoryItem数据[] = NULL;公共CategoryAdapter(上下文的背景下,INT layoutResourceId,CategoryItem []数据){
    超级(上下文,layoutResourceId,数据);
    this.layoutResourceId = layoutResourceId;
    this.context =背景;
    this.data =数据;
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    查看排= convertView;
    CategoryHolder支架=无效;    如果(行== NULL)
    {
        LayoutInflater充气=((活动)上下文).getLayoutInflater();
        行= inflater.inflate(layoutResourceId,父母,假);        持有人=新CategoryHolder();
        holder.category_name =(TextView中)row.findViewById(R.id.category_name);        row.setTag(保持器);
    }
    其他
    {
        支架=(CategoryHolder)row.getTag();
    }    CategoryItem类别=数据[位置]
    holder.category_name.setText(category.category_name);    返回行;
}静态类CategoryHolder
{
    TextView的CATEGORY_NAME;
}
}


解决方案

在事实上正在重用的意见。 getView需要作为第二个参数名为convertView查看。 ListView控件调用此方法与旧的浏览项(如果有),这被实例化(充气),但目前没有显示。您的甲基。 onListItemClick现在将这些意见之一的底色,但这种观点将在后面的另一行重复使用,无需重新设定它的底色。

一个可能的soloution:


  1. 的点击监听器只能存储背景颜色,或点击的标志,对于点击位置的地方,然后要求查看(行)有关重新呈现(它也可能保存状态,然后直接设置背景)。


  2. 在getView(..)方法应该的总是的根据保存的颜色/状态的位置设置视图的背景色。设置为单击或设置其他背景标志着点击收听者点击的位置没有标记位置的正常的背景。


In onListItemClick I want to change clicked item background, this is my code:

public void onListItemClick(ListView parent, View v, int position, long id){

    if (position != previous_position){
        v.setBackgroundResource(R.drawable.category_clicked_item_bg);
        if (previous_category != null)
            previous_category.setBackgroundResource(R.drawable.category_item_bg);
        previous_category = v;
        previous_position = position;
    }
}

previous_category and previous_position are protected variables.

The problem is, that on item click multiple items in list are highlighted (more specifically 1 or 2). In list there are 20 rows, if I click one in middle, only one gets highlighted, if I click any row in beginning or end, other row in opposite side gets highlighted with interval 13 rows (that's probably the reason why pressing middle row don't highlight second).

I need list to highlight only one row the clicked one, what is the problem?

ADDITIONAL CODE

If that can help, here is also ListView adapter class:

package domehotel.guestbook.page.category;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import domehotel.guestbook.R;

public class CategoryAdapter extends ArrayAdapter<CategoryItem>{

Context context; 
int layoutResourceId;    
CategoryItem data[] = null;

public CategoryAdapter(Context context, int layoutResourceId, CategoryItem[] data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    CategoryHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new CategoryHolder();
        holder.category_name = (TextView)row.findViewById(R.id.category_name);

        row.setTag(holder);
    }
    else
    {
        holder = (CategoryHolder)row.getTag();
    }

    CategoryItem category = data[position];
    holder.category_name.setText(category.category_name);

    return row;
}

static class CategoryHolder
{
    TextView category_name;
}
}

解决方案

In fact you are reusing views. getView takes as 2nd parameter a View named convertView. The ListView calls this method with an old View item (if there is one), which is instantiated (inflated) but not displayed at the moment. Your meth. onListItemClick now sets the backround of one of these Views, but this View will be reused later for another row, without resetting it's backround.

A possible soloution:

  1. The click listener should only store the background color, or a clicked flag, for a clicked position somewhere and then ask the View (row) in question to rerender (it could also save state and then set the background directly).

  2. The getView(..) method should always set the background color of a View according to the saved color/state for its position. Set a normal background for positions not marked as clicked or set another background for positions marked as clicked by the click listener.

这篇关于onListViewItemClick查看参数是指多个时listItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆