本文介绍了包含复选框,文本列表视图越来越复位上滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得出一个定制的导航抽屉ListView和头,但是当我滚动列表,列表中的复选框越来越选中。

I have drawn a customized navigation drawer with ListView and header but when i scroll the List the checkbox in the List are getting unchecked.

其次,当我在想,在列表视图所有复选框应该得到得到选中的头部分点击复位按钮。我一直在想这使其能正常工作,但无法找到任何解决方案。

Secondly when i click on the reset button in the header part I want that all the checkbox in the Listview should get get unchecked. I have been trying this to get it working but unable to find any solution..

该片段是

public class NavigationDrawer extends Fragment{
   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.filter_navigation_drawer, container,false);
        drawerListView= ( ListView ) view.findViewById( R.id.listDrawer );
        drawerListView.setOnItemClickListener(new FilterDrawerItemClickListener());

        dataList.add(new FilterDrawerItem("sample1",true));
        dataList.add(new FilterDrawerItem("sample2",true));
        dataList.add(new FilterDrawerItem("sample3",true));
        dataList.add(new FilterDrawerItem("sample4",true));
        dataList.add(new FilterDrawerItem("sample5",true));
        dataList.add(new FilterDrawerItem("sample2",true));
        dataList.add(new FilterDrawerItem("sample2",true));
        dataList.add(new FilterDrawerItem("sample2",true));
        dataList.add(new FilterDrawerItem("sample2",true));

        adapter = new FilterCustomDrawerAdapter(getActivity(), R.layout.drawer_filter,dataList,drawerStatus);
        drawerListView.setAdapter(adapter);
        adapter.getFilterList();

        resetBtn = (TextView)view.findViewById(R.id.filterby_reset);
        if(resetBtn != null){
            resetBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    application.setFilterStatus("reset");
                    for(int i=0; i<dataList.size(); i++){
                       dataList.get(i).setCheckBoxId(false);
                    }
                    adapter.notifyDataSetChanged();
                    // this.onCreateView();
                }
            });
        }

        return view;
    }
}

FilterCustomDrawerAdapter.java

public class FilterCustomDrawerAdapter extends ArrayAdapter<FilterDrawerItem> {

    Context context;
    List<FilterDrawerItem> drawerItemList;
    int layoutResID;
    int item = 0;
    String status;
    List<Integer> filterList = new ArrayList<Integer>();
    DrawerStatus drawerStatus;
    StataApplication application = StataApplication.getInstance();

    HashMap<Integer, Boolean> checked;   // newly added code

    public FilterCustomDrawerAdapter(Context context, int layoutResourceID,
                                     List<FilterDrawerItem> listItems,DrawerStatus drawerStatus) {
        super(context, layoutResourceID, listItems);
        this.context = context;
        this.drawerItemList = listItems;
        this.layoutResID = layoutResourceID;
        this.drawerStatus = drawerStatus;
        checked = new HashMap<Integer, Boolean>(getCount());
    }

    public FilterCustomDrawerAdapter(Context context, int layoutResourceID,
                                     List<FilterDrawerItem> listItems) {
        super(context, layoutResourceID, listItems);
        this.context = context;
        this.drawerItemList = listItems;
        this.layoutResID = layoutResourceID;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        final FilterDrawerItemHolder drawerHolder;
        View view = convertView;

        if (view == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            drawerHolder = new FilterDrawerItemHolder();

            view = inflater.inflate(layoutResID, parent, false);
            drawerHolder.ItemName = (TextView) view.findViewById(R.id.drawer_filterName);
            drawerHolder.checkBox = (CheckBox) view.findViewById(R.id.drawer_cbox);

            view.setTag(drawerHolder);
        } else {
            drawerHolder = (FilterDrawerItemHolder) view.getTag();
        }

        FilterDrawerItem dItem = (FilterDrawerItem) this.drawerItemList.get(position);

        drawerHolder.ItemName.setText(dItem.getItemName());
        TextView resetView = (TextView)view.findViewById(R.id.filterby_reset);
        CheckBox checkBox = (CheckBox) view.findViewById(R.id.drawer_cbox);

        // Newly added code
        Boolean isChecked = checked.get(position);
        checkBox.setChecked(isChecked == null ? false : isChecked);

      // if(application.getFilterStatus() != null) {
           if(checkBox.isChecked()){
               drawerHolder.checkBox.setChecked(false);
           }
      // }

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                if (isChecked) {
                    filterList.add(new Integer(position));
                    checked.put(position, true);
                } else {
                    filterList.remove(new Integer(position));
                    checked.put(position, false);
                }
            }
        });

        drawerHolder.checkBox.setTag(position);
        Log.d("FILTER_LIST_SIZE",String.valueOf(filterList.size()));

        return view;
    }

    private static class FilterDrawerItemHolder {
        TextView ItemName;
        CheckBox checkBox;
    }

    public List<Integer> getFilterList(){
        return filterList;
    }
}

在我滚动列表及以下,如果像我做的复选框SAMPLE1和样品2检查变得选中上滚动。并且还对单击标题复位按钮,我想我所有的复选框取消选中..没能得到这个工作...

In the image below when I scroll the list and if i make the checkbox sample1 and sample 2 checked it becomes unchecked on scrolling.and also on clicking reset button in the header i want all my checkbox to be unchecked..Not able to get this working ...

更新1

 resetBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    List<FilterDrawerItem> adapterDataList = adapter.getDrawerItemList();
                    for(int i=0; i<adapterDataList.size(); i++){  // At this place i am getting the size as 9
                        adapterDataList.get(i).setCheckBoxId(false);
                    }
                    adapter.setDrawerItemList(adapterDataList);
                    adapter.notifyDataSetChanged();
                }
            });

推荐答案

这是因为循环利用的意见的ListView。你应该创建一些HashMap的:

It because of recycling use of views in ListView. You should create some HashMap:

HashMap<Integer, Boolean> checked;

然后在你的构造函数中做到这一点:

Then in your constructor do this:

checked = new HashMap<Integer, Boolean>(getCount());

设置OnCheckedChangeListener您的复选框后,在事件方法做到这一点:

After set OnCheckedChangeListener on your checkboxes, and in event method do this:

checked.put(position, yourCheckBoxCheckedState);

而在getView()方法做到这一点:

And in getView() method do this:

Boolean isChecked = checked.get(position);
checkBox.setChecked(isChecked == null ? false : isChecked)

试试这个,我觉得它应该帮助

Try this, I think it should help

更新

resetBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.deselectAll();
            }
        });

然后适配器创建的方法:

Then in adapter create method:

 public void deselectAll() {
   checked = new HashMap<Integer, Boolean>(getCount());
   notifyDataSetChanged();
 }

这篇关于包含复选框,文本列表视图越来越复位上滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:15