本文介绍了OnItemClick监听器和点击查看项目的知名度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有每个项目包含一个一个ListView 的TextView 的ImageView 其中的ImageView 无形键,通过点击每个ListView项将是可见我的code是:

  @覆盖
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
    ImageView的acceptImageView =(ImageView的)view.findViewById(R.id.iAccept);
    如果(acceptImageView.getVisibility()== ImageView.INVISIBLE){
        acceptImageView.setVisibility(ImageView.VISIBLE);
    }其他{
        acceptImageView.setVisibility(ImageView.INVISIBLE);
    }
}

列表查看适配器:

 公共类ContentViewrAdaptor扩展BaseAdapter实现过滤的{私人列表<串GT; originalData = NULL;
私人列表<串GT; filteredData = NULL;
私人LayoutInflater mInflater;
私人定期字样;
私人ItemFilter MFILTER =新ItemFilter();
公共ContentViewrAdaptor(上下文的背景下,列表与LT;弦乐>的数据,定期字样){
    this.filteredData =数据;
    this.originalData =数据;
    this.regular =普通;
    mInflater = LayoutInflater.from(上下文);
}
公众诠释的getCount(){
    返回filteredData.size();
}
公共对象的getItem(INT位置){
    返回filteredData.get(位置);
}
众长getItemId(INT位置){
    返回的位置;
}
公共查看getView(INT位置,查看convertView,父母的ViewGroup){    ViewHolder持有人;
    如果(convertView == NULL){
        convertView = mInflater.inflate(R.layout.text_view,NULL);
        持有人=新ViewHolder();
        holder.text =(TextView中)convertView.findViewById(R.id.contentText);
        holder.text.setTypeface(常规);        convertView.setTag(保持器);
    }其他{
        支架=(ViewHolder)convertView.getTag();
    }    如果(filteredData.get(位置)。载有(字体))
        holder.text.setText(Html.fromHtml(filteredData.get(位置)));
    其他
        holder.text.setText(filteredData.get(位置));
    返回convertView;
}
静态类ViewHolder {    TextView的文本;
}
公共过滤用getFilter(){
    返回MFILTER;
}
私有类ItemFilter扩展过滤器{    @覆盖
    保护FilterResults performFiltering(CharSequence的约束){        字符串filterString = constraint.toString()与toLowerCase()。        FilterResults结果=新FilterResults();        最终名单<串GT;名单= originalData;        诠释计数=则为list.size();
        最终的ArrayList<串GT; NLIST =新的ArrayList<串GT;(计数);        串filterableString;        的for(int i = 0; I<计数;我++){
            filterableString = list.get(ⅰ);
            如果(filterableString.toLowerCase()。包括(filterString)){
                filterableString = filterableString.replaceAll(filterString,<字体颜色='红'>中+ filterString +< / FONT>中);
                nlist.add(filterableString);
            }
        }        results.values​​ = NLIST;
        results.count = nlist.size();        返回结果;
    }
    @燮pressWarnings(未登记)
    @覆盖
    保护无效publishResults(CharSequence的约束,FilterResults结果){
        filteredData =(ArrayList的<串GT;)results.values​​;
        notifyDataSetChanged();
    }}
}

但问题是,当我接受的第一项,第三项也将被acceped,因为它是当向下滚动第一项

我怎样才能解决这个问题?

更新(getView)

但仍是同样的问题。

 公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){    ViewHolder持有人;    如果(convertView == NULL){
        convertView = mInflater.inflate(R.layout.text_view,NULL);
        持有人=新ViewHolder();
        holder.image =(ImageView的)convertView.findViewById(R.id.iAccept);
        convertView.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                Toast.makeText(上下文,位置,Toast.LENGTH_SHORT).show();
                如果(v.findViewById(R.id.iAccept).getVisibility()== ImageView.VISIBLE)
                    v.findViewById(R.id.iAccept).setVisibility(ImageView.INVISIBLE);
                其他
                    v.findViewById(R.id.iAccept).setVisibility(ImageView.VISIBLE);
            }
        });
        holder.text =(TextView中)convertView.findViewById(R.id.contentText);
        holder.text.setTypeface(常规);        convertView.setTag(保持器);
    }其他{
        支架=(ViewHolder)convertView.getTag();
    }    如果(filteredData.get(位置)。载有(字体))
        holder.text.setText(Html.fromHtml(filteredData.get(位置)));
    其他
        holder.text.setText(filteredData.get(位置));
    返回convertView;

}

最后更新

终于将其与DIVA帮助解决,

我改变适配器这样的:

 公共类ContentViewrAdaptor扩展BaseAdapter实现过滤的{私人列表<串GT; originalData = NULL;
私人列表<串GT; filteredData = NULL;
私人的ArrayList<整数GT; visiblePositions;
私人LayoutInflater mInflater;
私人定期字样;
私人上下文的背景下;
私人ItemFilter MFILTER =新ItemFilter();
公共ContentViewrAdaptor(上下文的背景下,列表与LT;弦乐>的数据,定期字样){
    this.filteredData =数据;
    this.originalData =数据;
    visiblePositions =新的ArrayList<整数GT;();
    this.regular =普通;
    this.context =背景;
    mInflater = LayoutInflater.from(上下文);
}
公众诠释的getCount(){
    返回filteredData.size();
}
公共对象的getItem(INT位置){
    返回filteredData.get(位置);
}
众长getItemId(INT位置){
    返回的位置;
}公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){    最后ViewHolder持有人;    如果(convertView == NULL){
        convertView = mInflater.inflate(R.layout.text_view,NULL);        持有人=新ViewHolder();
        holder.image =(ImageView的)convertView.findViewById(R.id.iAccept);
        holder.image.setTag(位置);
        holder.text =(TextView中)convertView.findViewById(R.id.contentText);
        holder.text.setTypeface(常规);        convertView.setTag(保持器);
    }其他持有人=(ViewHolder)convertView.getTag();
    adjustImageVisibility(holder.image,visiblePositions.contains(位置));    convertView.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            onViewClicked(保持,位);
        }
    });    如果(filteredData.get(位置)。载(字体))holder.text.setText(Html.fromHtml(filteredData.get(位置)));
    其他holder.text.setText(filteredData.get(位置));    返回convertView;
}公共无效onViewClicked(ViewHolder viewHolder,整数位置){
    如果(visiblePositions.contains(位置)){
        adjustImageVisibility(viewHolder.image,FALSE);
        visiblePositions.remove(位置);
    }其他{
        adjustImageVisibility(viewHolder.image,真);
        visiblePositions.add(位置);
    }
}公共无效adjustImageVisibility(ImageView的ImageView的,布尔可见){
    如果(可见)imageView.setVisibility(View.VISIBLE);
    其他imageView.setVisibility(View.INVISIBLE);
}
静态类ViewHolder {
    ImageView的形象;
    TextView的文本;
}
公共过滤用getFilter(){
    返回MFILTER;
}
私有类ItemFilter扩展过滤器{    @覆盖
    保护FilterResults performFiltering(CharSequence的约束){        字符串filterString = constraint.toString()与toLowerCase()。        FilterResults结果=新FilterResults();        最终名单<串GT;名单= originalData;        诠释计数=则为list.size();
        最终的ArrayList<串GT; NLIST =新的ArrayList<串GT;(计数);        串filterableString;        的for(int i = 0; I<计数;我++){
            filterableString = list.get(ⅰ);
            如果(filterableString.toLowerCase()。包括(filterString)){
                filterableString = filterableString.replaceAll(filterString,<字体颜色='红'>中+ filterString +< / FONT>中);
                nlist.add(filterableString);
            }
        }        results.values​​ = NLIST;
        results.count = nlist.size();        返回结果;
    }
    @燮pressWarnings(未登记)
    @覆盖
    保护无效publishResults(CharSequence的约束,FilterResults结果){
        filteredData =(ArrayList的<串GT;)results.values​​;
        notifyDataSetChanged();
    }}
}


解决方案

有u需要确保一些事情:


  • PLZ使用ArrayList,而不是静态的String []

  • 使用View.Visible或不可见的,而不是Imagview.Visible和
    这里看不见的,而比较可视性为code,它的工作原理:

适配器

 公共类ListAdapter扩展BaseAdapter实现过滤的{    私人的ArrayList<串GT; originalData = NULL;
    私人的ArrayList<串GT; filteredData = NULL;
    私人LayoutInflater mInflater;
    私人上下文的背景下;    私人ItemFilter MFILTER =新ItemFilter();    公共ListAdapter(上下文的背景下,ArrayList的<串GT;数据){
        this.filteredData =新的ArrayList<>(数据);
        this.originalData =新的ArrayList<>(数据);
        this.context =背景;
        mInflater = LayoutInflater.from(上下文);
    }    公众诠释的getCount(){
        返回filteredData.size();
    }    公共对象的getItem(INT位置){
        返回filteredData.get(位置);
    }    众长getItemId(INT位置){
        返回的位置;
    }    公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){        ViewHolder持有人=新ViewHolder();        如果(convertView == NULL){
            convertView = mInflater.inflate(R.layout.list_item,NULL);            持有人=新ViewHolder();
            holder.image =(ImageView的)convertView.findViewById(R.id.image);
            // holder.image.setVisibility(View.INVISIBLE);            holder.text =(TextView中)convertView.findViewById(R.id.text);            convertView.setTag(保持器);
        }其他{
            支架=(ViewHolder)convertView.getTag();
        }
        // holder.image.setTag(位置);
        convertView.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){                如果(v.findViewById(R.id.image).getVisibility()== View.VISIBLE){
                    v.findViewById(R.id.image).setVisibility(View.INVISIBLE);
                }其他{
                    v.findViewById(R.id.image).setVisibility(View.VISIBLE);
                }            }
        });
        如果(filteredData.get(位置)。载有(字体))
            holder.text.setText(Html.fromHtml(filteredData.get(位置)));
        其他
            holder.text.setText(filteredData.get(位置));
        返回convertView;
    }    静态类ViewHolder {
        ImageView的形象;
        TextView的文本;
    }    @覆盖
    公共过滤用getFilter(){        返回MFILTER;
    }    私有类ItemFilter扩展过滤器{        @覆盖
        保护FilterResults performFiltering(CharSequence的约束){            字符串filterString = constraint.toString()与toLowerCase()。            FilterResults结果=新FilterResults();            最终的ArrayList<串GT;名单= originalData;            诠释计数=则为list.size();
            最终的ArrayList<串GT; NLIST =新的ArrayList<串GT;(计数);            串filterableString;            的for(int i = 0; I<计数;我++){
                filterableString = list.get(ⅰ);
                如果(filterableString.toLowerCase()。包括(filterString)){
                    filterableString = filterableString.replaceAll(
                            filterString,<字体颜色='红'>中+ filterString
                                    +&下; /字体>中);
                    nlist.add(filterableString);
                }
            }            results.values​​ = NLIST;
            results.count = nlist.size();            返回结果;
        }        @燮pressWarnings(未登记)
        @覆盖
        保护无效publishResults(CharSequence的约束,
                FilterResults结果){
            filteredData =(ArrayList的<串GT;)results.values​​;
            notifyDataSetChanged();
        }    }
}

活动

 公共类MyActivity延伸活动{    私人的ListView ListView的;
    私人ListAdapter适配器;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        ListView控件=(ListView控件)findViewById(R.id.listView);        ArrayList的<串GT; LIST_ITEM =新的ArrayList<串GT;();
        LIST_ITEM.add(项目1);
        LIST_ITEM.add(项目2);
        LIST_ITEM.add(项目3);
        LIST_ITEM.add(项目4);
        LIST_ITEM.add(项目5);
        LIST_ITEM.add(项目6);
        LIST_ITEM.add(项目7);
        LIST_ITEM.add(项目8);
        LIST_ITEM.add(第9项);
        LIST_ITEM.add(项目10);        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        适配器=新ListAdapter(MyActivity.this,LIST_ITEM);
        listView.setAdapter(适配器);    }
}

编辑

I have a ListView that each item contains one Textview and ImageView which the imageView is invisible and by clicking the each listview item it will be visible my code is :

 @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    ImageView acceptImageView= (ImageView) view.findViewById(R.id.iAccept);
    if (acceptImageView.getVisibility()==ImageView.INVISIBLE) {
        acceptImageView.setVisibility(ImageView.VISIBLE);
    } else {
        acceptImageView.setVisibility(ImageView.INVISIBLE);
    }
}

List View adapter :

public class ContentViewrAdaptor extends BaseAdapter implements Filterable {

private List<String>   originalData = null;
private List<String>   filteredData = null;
private LayoutInflater mInflater;
private Typeface       regular;
private ItemFilter     mFilter      = new ItemFilter();


public ContentViewrAdaptor(Context context, List<String> data, Typeface regular) {
    this.filteredData = data;
    this.originalData = data;
    this.regular = regular;
    mInflater = LayoutInflater.from(context);
}


public int getCount() {
    return filteredData.size();
}


public Object getItem(int position) {
    return filteredData.get(position);
}


public long getItemId(int position) {
    return position;
}


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

    ViewHolder holder;


    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.text_view, null);


        holder = new ViewHolder();
        holder.text = (TextView) convertView.findViewById(R.id.contentText);
        holder.text.setTypeface(regular);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    if (filteredData.get(position).contains("font"))
        holder.text.setText(Html.fromHtml(filteredData.get(position)));
    else
        holder.text.setText(filteredData.get(position));
    return convertView;
}


static class ViewHolder {

    TextView  text;
}


public Filter getFilter() {
    return mFilter;
}


private class ItemFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        String filterString = constraint.toString().toLowerCase();

        FilterResults results = new FilterResults();

        final List<String> list = originalData;

        int count = list.size();
        final ArrayList<String> nlist = new ArrayList<String>(count);

        String filterableString;

        for (int i = 0; i < count; i++) {
            filterableString = list.get(i);
            if (filterableString.toLowerCase().contains(filterString)) {
                filterableString = filterableString.replaceAll(filterString, "<font color='red'>" + filterString + "</font>");
                nlist.add(filterableString);
            }
        }

        results.values = nlist;
        results.count = nlist.size();

        return results;
    }


    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        filteredData = (ArrayList<String>) results.values;
        notifyDataSetChanged();
    }

}
}

but the problem is that when i accept the first item , the third item will be acceped too , because it's the first item when scroll down

how can i solve this problem ?

Update(getView)

but still the same problem

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

    ViewHolder holder;

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.text_view, null);


        holder = new ViewHolder();
        holder.image = (ImageView) convertView.findViewById(R.id.iAccept);
        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(context,position,Toast.LENGTH_SHORT).show();
                if (v.findViewById(R.id.iAccept).getVisibility() == ImageView.VISIBLE)
                    v.findViewById(R.id.iAccept).setVisibility(ImageView.INVISIBLE);
                else
                    v.findViewById(R.id.iAccept).setVisibility(ImageView.VISIBLE);
            }
        });
        holder.text = (TextView) convertView.findViewById(R.id.contentText);
        holder.text.setTypeface(regular);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    if (filteredData.get(position).contains("font"))
        holder.text.setText(Html.fromHtml(filteredData.get(position)));
    else
        holder.text.setText(filteredData.get(position));
    return convertView;

}

Final Update

finally it solved with DIVA help ,

i changed Adapter to this :

public class ContentViewrAdaptor extends BaseAdapter implements Filterable {

private List<String> originalData = null;
private List<String> filteredData = null;
private ArrayList<Integer> visiblePositions;
private LayoutInflater mInflater;
private Typeface regular;
private Context context;
private ItemFilter mFilter = new ItemFilter();


public ContentViewrAdaptor(Context context, List<String> data, Typeface regular) {
    this.filteredData = data;
    this.originalData = data;
    visiblePositions = new ArrayList<Integer>();
    this.regular = regular;
    this.context = context;
    mInflater = LayoutInflater.from(context);
}


public int getCount() {
    return filteredData.size();
}


public Object getItem(int position) {
    return filteredData.get(position);
}


public long getItemId(int position) {
    return position;
}

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

    final ViewHolder holder;

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.text_view, null);

        holder = new ViewHolder();
        holder.image = (ImageView) convertView.findViewById(R.id.iAccept);
        holder.image.setTag(position);
        holder.text = (TextView) convertView.findViewById(R.id.contentText);
        holder.text.setTypeface(regular);

        convertView.setTag(holder);
    } else holder = (ViewHolder) convertView.getTag();


    adjustImageVisibility(holder.image, visiblePositions.contains(position));

    convertView.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            onViewClicked(holder, position);
        }
    });

    if (filteredData.get(position).contains("font")) holder.text.setText(Html.fromHtml(filteredData.get(position)));
    else holder.text.setText(filteredData.get(position));

    return convertView;
}

public void onViewClicked(ViewHolder viewHolder, Integer position) {
    if (visiblePositions.contains(position)) {
        adjustImageVisibility(viewHolder.image, false);
        visiblePositions.remove(position);
    } else {
        adjustImageVisibility(viewHolder.image, true);
        visiblePositions.add(position);
    }
}

public void adjustImageVisibility(ImageView imageView, boolean visible) {
    if (visible) imageView.setVisibility(View.VISIBLE);
    else imageView.setVisibility(View.INVISIBLE);
}


static class ViewHolder {
    ImageView image;
    TextView text;
}


public Filter getFilter() {
    return mFilter;
}


private class ItemFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        String filterString = constraint.toString().toLowerCase();

        FilterResults results = new FilterResults();

        final List<String> list = originalData;

        int count = list.size();
        final ArrayList<String> nlist = new ArrayList<String>(count);

        String filterableString;

        for (int i = 0; i < count; i++) {
            filterableString = list.get(i);
            if (filterableString.toLowerCase().contains(filterString)) {
                filterableString = filterableString.replaceAll(filterString, "<font color='red'>" + filterString + "</font>");
                nlist.add(filterableString);
            }
        }

        results.values = nlist;
        results.count = nlist.size();

        return results;
    }


    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        filteredData = (ArrayList<String>) results.values;
        notifyDataSetChanged();
    }

}
}
解决方案

There are few thing u need to make sure :

  • plz use arraylist instead of static string[]
  • use View.Visible or invisible instead of Imagview.Visible andinvisible while comparing visiblity here is code that works :

Adapter

public class ListAdapter extends BaseAdapter implements Filterable {

    private ArrayList<String> originalData = null;
    private ArrayList<String> filteredData = null;
    private LayoutInflater mInflater;
    private Context context;

    private ItemFilter mFilter = new ItemFilter();

    public ListAdapter(Context context, ArrayList<String> data) {
        this.filteredData = new ArrayList<>(data);
        this.originalData = new ArrayList<>(data);
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return filteredData.size();
    }

    public Object getItem(int position) {
        return filteredData.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

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

        ViewHolder holder = new ViewHolder();

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);

            holder = new ViewHolder();
            holder.image = (ImageView) convertView.findViewById(R.id.image);
            // holder.image.setVisibility(View.INVISIBLE);

            holder.text = (TextView) convertView.findViewById(R.id.text);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        // holder.image.setTag(position);
        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (v.findViewById(R.id.image).getVisibility() == View.VISIBLE) {
                    v.findViewById(R.id.image).setVisibility(View.INVISIBLE);
                } else {
                    v.findViewById(R.id.image).setVisibility(View.VISIBLE);
                }

            }
        });
        if (filteredData.get(position).contains("font"))
            holder.text.setText(Html.fromHtml(filteredData.get(position)));
        else
            holder.text.setText(filteredData.get(position));
        return convertView;
    }

    static class ViewHolder {
        ImageView image;
        TextView text;
    }

    @Override
    public Filter getFilter() {

        return mFilter;
    }

    private class ItemFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            String filterString = constraint.toString().toLowerCase();

            FilterResults results = new FilterResults();

            final ArrayList<String> list = originalData;

            int count = list.size();
            final ArrayList<String> nlist = new ArrayList<String>(count);

            String filterableString;

            for (int i = 0; i < count; i++) {
                filterableString = list.get(i);
                if (filterableString.toLowerCase().contains(filterString)) {
                    filterableString = filterableString.replaceAll(
                            filterString, "<font color='red'>" + filterString
                                    + "</font>");
                    nlist.add(filterableString);
                }
            }

            results.values = nlist;
            results.count = nlist.size();

            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
            filteredData = (ArrayList<String>) results.values;
            notifyDataSetChanged();
        }

    }
}

Activity

public class MyActivity extends Activity {

    private ListView listView;
    private ListAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        listView = (ListView) findViewById(R.id.listView);

        ArrayList<String> LIST_ITEM = new ArrayList<String>();
        LIST_ITEM.add("Item 1");
        LIST_ITEM.add("Item 2");
        LIST_ITEM.add("Item 3");
        LIST_ITEM.add("Item 4");
        LIST_ITEM.add("Item 5");
        LIST_ITEM.add("Item 6");
        LIST_ITEM.add("Item 7");
        LIST_ITEM.add("Item 8");
        LIST_ITEM.add("Item 9");
        LIST_ITEM.add("Item 10");

        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        adapter = new ListAdapter(MyActivity.this, LIST_ITEM);
        listView.setAdapter(adapter);

    }
}

COMPLETE SOURCE CODE

EDITALTERNATIVE CODE.

这篇关于OnItemClick监听器和点击查看项目的知名度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:14