在listview onItemSelected Android中更改ImageView BackgroundResource

时间:2013-07-05 05:57:44

标签: android android-listview

我使用effectiveAdapter来填充mylistview,如下所示:

2 个答案:

答案 0 :(得分:2)

您可以使用setTag()getTag()来维护滚动位置并检查ListView行的ImageView是否不会互换资源。

在适配器的getView()课程中使用

vi.setTag(R.id.btOnOFF, holder.btOnOFF);

然后使用onItemClick()

getTag()内取得它
ImageView imgview = (ImageView) view.getTag(R.id.btOnOFF);
imgview.setBackgroundResource(R.drawable.air_radio_button_rouge);

如需进一步参考,您可以查看我的blog post

答案 1 :(得分:0)

你可以尝试这样的事情。我清理并简化了你的代码。

public class EfficientAdapter extends BaseAdapter {

    private Activity mActivity;
    private ArrayList<SearchTracks> mSearchTracks;
    private ResultatMultiple mResultatMultiple;
    private int mSelectedPosition;

    public EfficientAdapter(Activity activity, ArrayList<SearchTracks> searchTracks) {
        mActivity = activity;
        mSearchTracks = searchTracks;
    }

    public EfficientAdapter(ResultatMultiple resultatMultiple){
        mResultatMultiple = resultatMultiple ;
    }

    public void setSelectedPosition(int position) {
        mSelectedPosition = position;
    }

    @Override
    public int getCount() {
        return mSearchTracks.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder holder = null;

        if(convertView == null) {
            LayoutInflater inflater = (LayoutInflater)mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.ecran_multiple_row, null);

            holder = new ViewHolder();
            holder.indexTextView = (TextView)view.findViewById(R.id.txIndex);
            holder.titleTextView = (TextView)view.findViewById(R.id.txSTitle);
            holder.buttonOnOffImageView = (ImageView)view.findViewById(R.id.btOnOFF);
            view.setTag(holder);
        }else {
            holder = (ViewHolder)view.getTag();
        }

        int index = position + 1;
        holder.indexTextView.setText((index <= 9 ? "0" : "") + Integer.toString(index));
        holder.titleTextView.setText(mSearchTracks.get(position).getTitle());

        if(position % 2 == 0) {
            view.setBackgroundResource(R.drawable.listview_selector_odd);
        }else {
            view.setBackgroundResource(R.drawable.listview_selector_even);
        }

        if(mSearchTracks.size() == 1 || position == mSelectedPosition) {
            holder.buttonOnOffImageView.setBackgroundResource(R.drawable.air_radio_button_rouge);
        }else {
            holder.buttonOnOffImageView.setBackgroundResource(R.drawable.air_deezer_check);
        }

        return view;
    }

    private class ViewHolder {
        public TextView indexTextView;
        public TextView titleTextView;
        public ImageView buttonOnOffImageView;
    }
}

现在,在您的onClick侦听器中,您可以调用listAdapter.setSelectedPosition(position);然后listAdapter.notifyDataSetChanged();来重新加载您的表。