需要在android中改变getite()中的listitem背景

时间:2012-09-06 18:18:30

标签: android listview background

我有列表视图。在通过适配器设置listitems时,我在getView()中的特定位置设置了项目的背景。当我点击listitems时,我需要设置该项目的背景,以及删除我在适配器类的getView()中设置的项目的背景。我点击了listitem后设置了背景。但我无法删除我在getView()中设置的背景。我的时间不多了。需要帮忙。

Listclick:

View lastView;

     lv_school.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> pa, View v, int pos,long row1) {
    if(lastView==null){                       
    v.setBackgroundResource(R.drawable.list_selected);                      
    lastView=v;
    }
    else{
    lastView.setBackgroundColor(Color.WHITE);                          
    v.setBackgroundResource(R.drawable.list_selected);
                                lastView=v;
                                }

                        }
                    });

我的适配器类是:

public class SchoolAdapterSetting extends BaseAdapter {
            private Activity activity;
            List<String>data=new ArrayList<String>();
            private LayoutInflater inflater = null;
            public SchoolAdapterSetting(Activity a, List<String> school_name_List) {
                // TODO Auto-generated constructor stub
                activity = a;
                data = school_name_List;

                inflater = (LayoutInflater) activity
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return data.size();
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                View vi = convertView;

                    vi = inflater.inflate(R.layout.school_item, null);
                final TextView info = (TextView) vi.findViewById(R.id.schl_text);
                Typeface type1 = Typeface.createFromAsset(getAssets(),"CORBEL.TTF"); 
                info.setTypeface(type1);
                if(position==5)
                {
                    info.setBackgroundResource(R.drawable.list_selected);
                    pressedView=vi;
                }
                info.setText(data.get(position));
                return vi;
            }

        }

4 个答案:

答案 0 :(得分:0)

我想做类似的事情并开发自己的逻辑来实现它。我的代码用于突出显示当前选择的ChildView,如果您看到代码,则可以为ParentView和ChildView实现它。

    public int _groupPosition = -1;
    View _lastColored;
    private int _highlightedGroup = -1;
    private int _highlightedChild;



    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {

        _groupPosition = groupPosition;
        _highlightedGroup = groupPosition;
        _highlightedChild = childPosition;
        if (_lastColored != null) {
            _lastColored.setBackgroundColor(Color.TRANSPARENT);
        }
        _lastColored = v;
        v.setBackgroundColor(Color.rgb(214, 214, 214)); // whatever colour you want to set
             ..... // your code here
}

注意:以上代码适用于ExapandableListView,但我相信您可以根据自己的要求进行修改。

无需在XML文件中使用任何选择器。这段代码完成了所有工作。祝你好运!

答案 1 :(得分:0)

您可以在视图上调用V.setBackgroundResource(0)来删除背景。

我遇到了类似的情况,所以设​​计了一个满足我需求的解决方案。检查一下,可能对你有所帮助。 Link to my answer

答案 2 :(得分:0)

  

我的时间不多了。需要帮助。

Stack Overflow永远不应该是你获得帮助的最后尝试,这看起来很像 homework ,你的问题应该有homework标签。

无论如何,你的getView()非常低效。请观看Android的Romain Guy explain adapters and getView()。但它确实有效。


要解决您的问题:我注意到pressedView被保存为第一个“已选中”行,只要您声明pressedView这样:

public class SchoolAdapterSetting extends BaseAdapter {
    public View pressedView;
    ...

然后解决方案很简单:

if(lastView==null){
    ((SchoolAdapterSetting) pa.getAdapter()).pressedView.setBackgroundColor(Color.WHITE)
    v.setBackgroundResource(R.drawable.list_selected);                      
    lastView=v;
}
else{
    lastView.setBackgroundColor(Color.WHITE);                          
    v.setBackgroundResource(R.drawable.list_selected);
    lastView=v;
}

中提琴!


<强>临提示:
你注意到每个if-else块中的三条线中有两条是相同的吗?你可以把它们拉出来。

如果您在初始化适配器后立即将pressedView设置为lastView

SchoolAdapterSetting adapter = new SchoolAdapterSetting(...)
lastView = adapter.pressedView;

然后你可以完全删除if-else!

答案 3 :(得分:0)

我只是想通了。我使用lv_school.setAdapter(null);清除了listitems并再次调用了适配器。 这是我的代码。

    lv_school.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> pa, View v, int pos,
                            long row1) {

                        lv_school.setAdapter(null);
                        schl_adapter = new SchoolAdapterSetting(Setting.this,
                                school_name_List, pos);
                        lv_school.setAdapter(schl_adapter);
                        schl_adapter.notifyDataSetChanged();
                        lv_school.setSelection(pos);
                        spinner_selected_school_name = school_name_List.get(pos);
                        spinner_selected_school_id = schooltagid_List.get(pos);

                    }
                });


public class SchoolAdapterSetting extends BaseAdapter {
        private Activity activity;
        public View pressedView;
        private int highlight_pos;
        List<String> data = new ArrayList<String>();
        private LayoutInflater inflater = null;

        public SchoolAdapterSetting(Activity a, List<String> school_name_List,
                int pos) {
            // TODO Auto-generated constructor stub
            activity = a;
            data = school_name_List;
            highlight_pos = pos;
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

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

            vi = inflater.inflate(R.layout.school_item, null);
            final TextView info = (TextView) vi.findViewById(R.id.schl_text);
            Typeface type1 = Typeface
                    .createFromAsset(getAssets(), "CORBEL.TTF");
            info.setTypeface(type1);
            if (position == highlight_pos) {
                info.setBackgroundResource(R.drawable.list_selected);
            }
            info.setText(data.get(position));
            return vi;
        }

    }