列表视图显示多项选择 [英] List view shows multiple selected items

查看:131
本文介绍了列表视图显示多项选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种感觉,这是由于某种渲染优化或什么的,但我不知道是什么。

我有一个列表视图,并且在选择一个项目,使其在视觉上保持选中状态我改变它的背景色。

在列表超出屏幕(它滚动),如果我选择一个项目接近顶部,然后向下滚动,也显示另一个选择的项目,曾经出视图(我可从来没有看到2中选择时,会出现问题屏幕上的项在同一时间)。这也适用于相反的方式,如果我选择在底部附近一个项目,然后滚动到顶部,它会显示另一个项目为被选择。

此外另一个音符,所选的项目之间的距离是不均匀的,如果我旋转我的设备到景观,所选择的项目之间的距离小于

如果目前还不清楚是什么问题我还附上照片。

这是具有最小列表时,它仅显示一个所选择的项目。

在这里,我选择了接近顶部的项目。

向下滚动后(见滚动条),它显示的另一个项目为被选择。

现在为code。

该列表片段。

 公共类ResultListFragment扩展ListFragment {BookListAdapter mBooksArray;
BookData API;
查看脚注;
ListView控件列表;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    活性亲= getActivity();
        与BookSearch应用=(与BookSearch)parent.getApplicationContext();    API = app.bookAPI;    mBooksArray =新BookListAdapter(/ *有些PARAMS * /);
    mBooksArray.currentActivity =父母;
}公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){        名单=(ListView控件)inflater.inflate(R.layout.search_results,NULL);    页脚= inflater.inflate(R.layout.load_more,NULL);    返回列表;
}公共无效onActivityCreated(捆绑savedInstanceState){
    super.onActivityCreated(savedInstanceState);    list.addFooterView(页脚);    list.setAdapter(mBooksArray);
}}

和一个ArrayAdapter,颜色在onclick方法得到改变。

 公共类BookListAdapter扩展ArrayAdapter<图书> {ArrayList的<图书>图书;
私人BookData bookData;
活动currentActivity;
最后BookListAdapter自我=这一点;
私有视图中选择= NULL;公共无效更新(){
    currentActivity.runOnUiThread(新的Runnable(){
        公共无效的run(){
            self.notifyDataSetChanged();
        }
    });
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    视图V = convertView;
    如果(V == NULL){        LayoutInflater VI =(LayoutInflater)的getContext()。getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        V = vi.inflate(R.layout.search_list_view_item,NULL);
    }
    / *一些code设置图片和文字* /
    v.setOnClickListener(新OnItemClickListener(位置));
    返回伏;
}公共无效的onClick(INT位置,查看视图){
            //此日志始终报告正确的位置,当我选择列表项
    Log.i(新的整数(位置)的ToString(),books.get(位置).title伪);
    如果(选择!= NULL){
        selected.setBackgroundResource(R.drawable.list_view_bg);
    }
    选择=视图。
    selected.setBackgroundResource(R.color.listSelected);}私有类OnItemClickListener实现OnClickListener {
        私人诠释mPosition;        OnItemClickListener(INT位置){
            mPosition =位置;
        }
        @覆盖
        公共无效的onClick(查看视图){
        BookListAdapter.this.onClick(mPosition,视图);
        }
    }
}


解决方案

ListView控件回收的意见(以视图V = convertView的一部分;如果(V == NULL){} ... ),所以你的设定将被反复使用的视图的背景。相反,你还需要设置一个选择标志在你的模型(图书对象本身)。在部分你的 getView 被注释掉,你然后说:

 如果(book.isSelected()){
  v.setBackgroundResource(R.color.listSelected)
}

I have a feeling this is due to some sort of rendering optimization or something, but I'm not sure what.

I have a list view and when an item is selected, I change its background color so that it visually remains selected.

The problem occurs when the list extends beyond the screen (it scrolls), if I select an item near the top and then scroll down it also displays another selected item that was once out of the view (i can never see 2 selected items on the screen at the same time). This also works in the opposite way, if I select an item near the bottom and then scroll to the top, it will display another item as being selected.

Also another note, the distance between the selected items is not uniform, if i rotate my device into landscape, the distance between the selected items is less.

If it's not clear what the problem is I also attached pictures.

This is with a minimal list, it only displays one selected item.

Here I have selected an item near the top.

After scrolling down (see scroll bar) it displays another item as being selected.

Now for the code.

The list fragment.

public class ResultListFragment extends ListFragment {

BookListAdapter mBooksArray;
BookData api;
View footer;
ListView list;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Activity parent = getActivity();    
        BookSearch app = (BookSearch) parent.getApplicationContext();

    api = app.bookAPI;

    mBooksArray = new BookListAdapter(/*some params*/);
    mBooksArray.currentActivity = parent;


}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        list = (ListView) inflater.inflate(R.layout.search_results, null);

    footer = inflater.inflate(R.layout.load_more, null);

    return list;
}

public void onActivityCreated( Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    list.addFooterView(footer);

    list.setAdapter(mBooksArray);
}

}

And the ArrayAdapter, the color gets changed in the onClick method.

public class BookListAdapter extends ArrayAdapter<Book> {

ArrayList<Book> books;
private BookData bookData;
Activity currentActivity;
final BookListAdapter self = this;
private View selected = null;



public void update() {
    currentActivity.runOnUiThread(new Runnable() {
        public void run() {     
            self.notifyDataSetChanged();
        }
    }); 
}

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

        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.search_list_view_item, null);
    }
    /*some code to set the image and text */
    v.setOnClickListener(new OnItemClickListener(position));
    return v;
}

public void onClick(int position, View view) {
            // this log always reports the correct position when i select a list item
    Log.i(new Integer(position).toString(), books.get(position).title);
    if(selected != null) {
        selected.setBackgroundResource(R.drawable.list_view_bg);
    }
    selected = view;
    selected.setBackgroundResource(R.color.listSelected);

}

private class OnItemClickListener implements OnClickListener{           
        private int mPosition;

        OnItemClickListener(int position){
            mPosition = position;
        }
        @Override
        public void onClick(View view) {
        BookListAdapter.this.onClick(mPosition, view);
        }               
    }
}

解决方案

The ListView recycles views (the part with View v = convertView; if (v == null) { } ...), so you're setting the background on a view that will be used again and again. Instead, you also need to set a "selected" flag on your model (the Book object itself). In the part of your getView that is commented out, you then say:

if (book.isSelected()) { 
  v.setBackgroundResource(R.color.listSelected) 
}

这篇关于列表视图显示多项选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆