首页 > 代码库 > ListView Item 点击展开隐藏问题

ListView Item 点击展开隐藏问题

publicclass ListAdapter extendsBaseAdapter {
    privateContext mContext;
    privateView mLastView;
    privateint mLastPosition;
    privateint mLastVisibility;
     
    publicListAdapter(Context context) {
        this.mContext = context;
        mLastPosition = -1;
    }
 
    @Override
    publicint getCount() {
        return30;
    }
 
    @Override
    publicObject getItem(intposition) {
        returnnull;
    }
 
    @Override
    publiclong getItemId(intposition) {
        return0;
    }
 
    @Override
    publicView getView(intposition, View convertView, ViewGroup parent) {
        Holder holder;
        if(convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.list_item, null);
            holder =newHolder();
            holder.textView = (TextView)convertView.findViewById(R.id.textView);
            holder.UEFAView = (ImageView)convertView.findViewById(R.id.image_uefa);
            holder.mascotView = (ImageView)convertView.findViewById(R.id.image_mascot);
            holder.hint = convertView.findViewById(R.id.hint_image);
            convertView.setTag(holder);
        }else{
            holder = (Holder) convertView.getTag();
        }
        if(mLastPosition == position){
            holder.hint.setVisibility(mLastVisibility);
        }else{
            holder.hint.setVisibility(View.GONE);
        }
        holder.textView.setText("Hello,It is " + position);
        returnconvertView;
    }
     
    classHolder {
        TextView textView;
        ImageView UEFAView;
        ImageView mascotView;
        View hint;
    }
     
    publicvoid changeImageVisable(View view,intposition) {
        if(mLastView != null&& mLastPosition != position ) {
            Holder holder = (Holder) mLastView.getTag();
            switch(holder.hint.getVisibility()) {
            caseView.VISIBLE:
                holder.hint.setVisibility(View.GONE);
                mLastVisibility = View.GONE;
                break;
            default:
                break;
            }
        }
        mLastPosition = position;
        mLastView = view;
        Holder holder = (Holder) view.getTag();
        switch(holder.hint.getVisibility()) {
        caseView.GONE:
            holder.hint.setVisibility(View.VISIBLE);
            mLastVisibility = View.VISIBLE;
            break;
        caseView.VISIBLE:
            holder.hint.setVisibility(View.GONE);
            mLastVisibility = View.GONE;
            break;
        }
    }
}




12
13
14
15
16
17
18
19
publicclass ListViewTestActivity extendsActivity implementsOnItemClickListener{
    privateListView mListView;
    privateListAdapter mAdapter;
    @Override
    publicvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        mListView = (ListView)findViewById(R.id.list);
        mAdapter = new ListAdapter(this);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);
    }
     
    @Override
    publicvoid onItemClick(AdapterView<?> parent, View view, intposition, longid) { 
        mAdapter.changeImageVisable(view, position); 
    
}

ListView Item 点击展开隐藏问题