ViewHolder不工作(再重复一次)

时间:2014-10-27 18:30:46

标签: android layout-inflater android-viewholder

在自定义适配器列表视图中重复项目反应时遇到问题。 实际上onClick方法申请点击元素,如果我向下滚动我的列表,一些元素重复相同 所以,当我试图用Tag解决它时,它没有帮助我。 我尝试使用在Stack和Google上找到的不同实现,但它也无济于事。 这是我的欲望实现代码

public class CAChanal extends BaseAdapter {
    private Context context;

    protected MainActivity MAcontext;

    public CAChanal(MainActivity _context){
        MAcontext = _context;
    }

    public CAChanal(Context context) { 
        this.context = context;
    }

    ArrayList<DMChanal> listArray;
    public CAChanal() {
        listArray = new ArrayList<DMChanal>();
    }
    @Override
    public int getCount() {
        return listArray.size(); 
    }

    @Override
    public Object getItem(int i) {
        return listArray.get(i); 
    }

    @Override
    public long getItemId(int i) {
        return i; 
    }

    @Override
    public View getView(int index,View view, ViewGroup parent) {
         final ViewHolder holder ; 
        if (view == null) { 
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            view = inflater.inflate(R.layout.chanal_template, null);
            holder = new ViewHolder(); 
            holder.chanalElement=(ChanalView)view.findViewById(R.id.chanalView_forList);
            view.setTag(holder);
        }else
        {
             holder = (ViewHolder)view.getTag();
        }


         holder.chanalElement.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if(!holder.chanalElement.getChanalStatus())
                {
                     holder.chanalElement.enableChanal();
                }else
                {
                     holder.chanalElement.disableChanal();
                }
            }
        });     
        return view;
    } 
    static class ViewHolder {
         ChanalView chanalElement;
        }

    public Object getFilter() {

        return null;
    } 


}  

任何人都可以帮助我解决我的问题,并解释我的代码中可能出现的问题。 P.S:ChanalView是我的自定义视图

1 个答案:

答案 0 :(得分:0)

public class MyAdapter extends BaseAdapter {

     List<DMChanal> listArray;
     List<Boolean> chanalElementStates = new ArrayList<Boolean>();

     ...

     @Override
     public View getView(final int index,View view, ViewGroup parent) {
         final ViewHolder holder ; 
         if (view == null) { 
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            view = inflater.inflate(R.layout.chanal_template, null);
            holder = new ViewHolder(); 
            holder.chanalElement = (ChanalView)view.findViewById(R.id.chanalView_forList);
            view.setTag(holder);
        } else {
            holder = (ViewHolder)view.getTag();
        } 

        holder.chanalElement.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean enabled = !chanalElementStates[index];
                chanalElementStates[index] = enabled;
                updateEnabledState(holder, enabled);
            }
        }); 

        // What you were missing. You always have to reset the state to 
        // what you want because the views are recycled.
        updateEnabledState(holder, chanalElementStates[index]);

        return view;
    } 

    private void updateEnabledState(ViewHolder holder, boolean enable) {
        if (enable)
            holder.chanalElement.enableChanal();
        else
            holder.chanalElement.disableChanal();    
    }
}