简体   繁体   English

在可滚动列表视图上更改背景颜色

[英]Changing background color on scrollable listview

I have a scrollable listview in which multiple rows can be selected. 我有一个可滚动的列表视图,可以在其中选择多行。 When selected, the background color changes to green and if selected again it changes back to its original color. 选中后,背景色变为绿色,如果再次选择,则变回其原始颜色。

I managed to get that to work but the problem I am having is when I scroll, other rows are being highlighted green that I did not select. 我设法使它正常工作,但是我遇到的问题是滚动时,其他行被突出显示为绿色,而我没有选择。 I presume this is because the view is being reused/recycled but I'm not sure how to fix the issue. 我认为这是因为视图已被重用/回收,但是我不确定如何解决该问题。

Code: 码:

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

    myDB = new DatabaseHandler(getActivity());
    listViewSchools = (ListView)getView().findViewById(R.id.listViewFromDB);


    if (myDB.getSchoolCount() != 0){
        SchoolsList.addAll(myDB.getAllSchools());
        isSelected = new boolean[SchoolsList.size()];
    }
    populateSchoolsView();


    listViewSchools.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            t_school item = SchoolsList.get(position);
            if (isSelected[position]) {
                v.setBackgroundColor(getResources().getColor(R.color.orig));
                isSelected[position] = false;
            }
            else if (!isSelected[position]) {
                v.setBackgroundColor(getResources().getColor(R.color.green));
                isSelected[position] = true;
            }
            schoolAdapter.notifyDataSetChanged();

        }
    });
 }

private void populateSchoolsView() {
    schoolAdapter = new SchoolListAdapter();
    listViewSchools.setAdapter(schoolAdapter);
    schoolAdapter.notifyDataSetChanged();

}

private class SchoolListAdapter extends ArrayAdapter<t_school> {
    public SchoolListAdapter() {
        super(getActivity(), R.layout.itemschool_layout, SchoolsList);
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        View row = view;
        ViewHolder holder = new ViewHolder();
        if (row == null) {
            row = getActivity().getLayoutInflater().inflate(R.layout.itemschool_layout, parent, false);
            holder.name = (TextView) row.findViewById(R.id.school_name);
            row.setTag(holder);
        }
        else{
            holder = (ViewHolder)row.getTag();
        }

        t_school currentSchool = SchoolsList.get(position);

        if(currentSchool!=null){
            holder.name.setText(currentSchool.getInfo());

        }

        return row;
    }
}

public class ViewHolder
{
    TextView name;

}

EDIT 编辑

I attempted to implement getItemViewType() and getViewTypeCount() but I'm not sure if I'm on the right track. 我尝试实现getItemViewType()和getViewTypeCount(),但不确定是否在正确的轨道上。 I followed this tutorial for reference: http://android.amberfog.com/?p=296 but I could use some more guidance on what I'm missing in those methods and if I'm utilizing them correctly in getView(). 我按照本教程进行了参考: http : //android.amberfog.com/?p=296,但是我可以针对这些方法中缺少的内容以及是否在getView()中正确使用它们提供了更多指导。

Edited Code: 编辑代码:

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

    myDB = new DatabaseHandler(getActivity());
    if(myDB.getSchoolCount()==0)
        Toast.makeText(getActivity().getApplicationContext(), "Please Import Data", Toast.LENGTH_LONG).show();
    listViewSchools = (ListView)getView().findViewById(R.id.listViewFromDB);

    registerForContextMenu(listViewSchools);


    listViewSchools.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            longClickedItemIndex = position;
            return false;
        }
    });

    if (myDB.getSchoolCount() != 0){
        SchoolsList.addAll(myDB.getAllSchools());
        isSelected = new boolean[SchoolsList.size()];
    }
    populateSchoolsView();


    listViewSchools.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            t_school item = SchoolsList.get(position);
            if (isSelected[position]) {
                v.setBackgroundColor(getResources().getColor(R.color.orig));
                isSelected[position] = false;
            }
            else if (!isSelected[position]) {
                v.setBackgroundColor(getResources().getColor(R.color.green));
                isSelected[position] = true;
            }
            schoolAdapter.notifyDataSetChanged();

        }
    });

}

private void populateSchoolsView() {
    schoolAdapter = new SchoolListAdapter();
    listViewSchools.setAdapter(schoolAdapter);
    schoolAdapter.notifyDataSetChanged();

}

private class SchoolListAdapter extends ArrayAdapter<t_school> {
    public SchoolListAdapter() {
        super(getActivity(), R.layout.itemschool_layout, SchoolsList);
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return isSelected[position] ? 1 : 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        View row = view;
        ViewHolder holder;
        int type = getItemViewType(position);

        if (row == null) {
            holder = new ViewHolder();
            switch ((type)) {
                case 1:
                    row = getActivity().getLayoutInflater().inflate(R.layout.itemschool_layout, parent, false);
                    holder.name = (TextView) row.findViewById(R.id.school_name);
                    break;

                case 0:
                    row = getActivity().getLayoutInflater().inflate(R.layout.itemschool_layout, parent, false);
                    holder.name = (TextView) row.findViewById(R.id.school_name);
                    break;

            }
            row.setTag(holder);
        }
        else{
            holder = (ViewHolder)row.getTag();
        }

        t_school currentSchool = SchoolsList.get(position);
        if(currentSchool!=null){
            holder.name.setText(currentSchool.getSchool_info());

        }

        return row;
    }
}

public class ViewHolder
{
    TextView name;

}   

What you are expiring is the Recycling mechanism of the Listview. 您即将到期的是Listview的回收机制。

You change the appearance of your Listitem and get the same Item again when you scrolling further down. 您可以更改Listitem的外观,并在向下滚动时再次获得相同的Item。

You could always reset the color but the clean solution would be to provide 2 Item Types. 您可以始终重置颜色,但是干净的解决方案是提供2种项目类型。

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    return isSelected[position] ? 1 : 0;
}

that way the ListView provides you with recyled Items of the same type. 这样,ListView可为您提供相同类型的已回收项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM