ListView多选

时间:2013-10-15 10:05:35

标签: android listview multi-select

我尝试在Android ListView上实现多选。我想选择/取消选择多个项目,这些项目应该是否突出显示。 我不想使用CheckBox来选择项目。

我把functionallity放在我的适配器中。这是代码:

public class MultiSelectionAdapter extends BaseAdapter {

private List<Event> streams;
private LayoutInflater inflater;
private Context context;

// I keep the selected items index.
private static List<Integer> selectedIndexList = new ArrayList<Integer>();


public MultiSelectionAdapter(Context context, List<Event> streams) {
    this.streams = streams;
    this.inflater = LayoutInflater.from(context);
    this.context = context;

    selectedIndexList.clear();
    Integer i = -1;
    selectedIndexList.add(i);
}



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

    //if (convertView == null) {
        convertView = inflater.inflate(R.layout.adapter_stream_ins, null);
        holder = new ViewHolder();
        holder.hTitle = (TextView) convertView.findViewById(R.id.adapter_title);
        holder.hProfileImage = (ImageView) convertView.findViewById(R.id.adapter_profileimage);
        holder.hMainImage = (WebImageView) convertView.findViewById(R.id.adapter_mainimage);
        //convertView.setTag(holder);
    //} else {
    //  holder = (ViewHolder) convertView.getTag();
    //}


    for(Integer i : selectedIndexList){
        if (position == i) {
            // Here I get the view inside the layout and set it to enable (the item has a selector as background) 
            RelativeLayout mainLayout = (RelativeLayout) ((ViewGroup) convertView).getChildAt(1);
            LinearLayout item = (LinearLayout) ((ViewGroup) mainLayout).getChildAt(1);
            item.setSelected(true);
        }
    }


    Ins eventItem = (Inspiration) getItem(position);
    holder.hTitle.setText(eventItem.getName());
    holder.hMainImage.setImageWithURL(context, WebServiceConfig.getImageUrl(eventItem.getImagePath()));

    return convertView;
}



public int getCount() {
    return this.streams.size();
}

public Event getItem(int position) {
    return this.streams.get(position);
}

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

public void add(Event e) {
    streams.add(e);
}

public void addAll(List<Event> events) {
    streams.addAll(events);
}

public void remove(Event e) {
    streams.remove(e);
}



public void setSelectdIndexList(List<Integer> l){
    selectedIndexList = l;
    notifyDataSetChanged();
}


class ViewHolder {
    TextView hTitle;
    ImageView hProfileImage;
    WebImageView hMainImage;
}

}

我的活动我将所选项目和索引保存在列表

public List<Event> insSelected = new ArrayList<Event>();
public static List<Integer> selecteditems = new ArrayList<Integer>();

最后在ListView setOnItemClickListener方法中,我更新了列表和适配器。

streamsListView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position, long arg3) {   
            Ins selectedItem = (Ins) streamsListView.getItemAtPosition(position);


            if(insSelected.contains(selectedItem)){
                insSelected.remove(selectedItem);

                selecteditems.remove((Integer)position);
                eventAdapter.setSelectdIndexList(selecteditems);
                eventAdapter.notifyDataSetChanged();
            }else{
                insSelected.add(selectedItem);

                selecteditems.add((Integer)position); 
                eventAdapter.setSelectdIndexList(selecteditems);
                eventAdapter.notifyDataSetChanged();
            }

        }
    });

我知道适配器中的代码效率很低,因为每次选择或取消选择项目时都必须绘制ListView。

有关如何改进它的任何想法? 谢谢!

1 个答案:

答案 0 :(得分:0)

这是我用来创建自定义适配器的代码示例。 第一个Function用于从主活动中调用自定义适配器。 ListAdapter是您要调用的自定义Java文件。

List<ListAdapter> CustomAdapterList = new ArrayList<CustomAdapterList>();//List  with items to send to the custom adapter

List<String> items = new ArrayList<String>();
private void populateCustomList()
{
    ArrayAdapter<ListAdapter> Population= new CustomAdapter();
    ListView list= (ListView)findViewById(R.id.listView);
    list.setAdapter(Population);
}

private class CustomAdapter extends ArrayAdapter<ListAdapter>
{
    public CustomAdapter (){super(Settings.this,R.layout.listadapter,CustomAdapterList);}

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View itemView = convertView;
        if (itemView == null)
        {
            itemView = getLayoutInflater().inflate(R.layout.listadapter, parent, false);
        }
        final ListAdapter Position = CustomAdapterList.get(position);

        final TextView tv= (TextView)itemView.findViewById(R.id.tv);
        tv.setText(Position.getText());

        final RelativeLayout layout= (RelativeLayout)itemView.findViewById(R.id.layoutID);

        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) 
            {
            if(!items.contains(Position.getText())
            {
                 items.add(Position.getText());
                 layout.setBackgroundColor(Color.parseColor("")); //input RGB value of selected item
            }
            else()
            {
                items.remove(Position.getText());
                layout.setBackgroundColor(Color.parseColor("")); //Input RGB value of unselected colour
            }
            }
        });

        return itemView;
    }
}

这将更改您单击的项目的布局颜色,并将它们添加到列表中以供您在何时使用它们。它还会在选择后更改颜色,并从列表中删除该项目。

ListAdapter类如下:

public class ListAdapter{


String text;

public ListAdapter(String text)
{
    super();
    this.text= text;

}

public String getText()
{
    return text;
}

public void setText(String text)
{
    this.text= text;
}


}

我希望这会有所帮助。 PS。 listadapter.xml只在RelativeLayout中保存textview。