0

我有列表视图。通过适配器设置列表项时,我在 getView() 中为特定位置的项目设置了背景。当我单击列表项时,我需要为该项目设置背景,并删除我在适配器类的 getView() 中设置的项目的背景。我已经实现了在单击列表项时设置背景。但我无法删除我在 getView() 中设置的背景。我没时间了。需要帮忙。

列表点击:

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

4 回答 4

0

我没时间了。需要帮忙。

Stack Overflow 永远不应该是你最后一次寻求帮助的尝试,这看起来很像家庭作业,你的问题应该有homework标签。

无论如何,您getView()的效率非常低下。请观看 Android 的Romain Guy 解释适配器和 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 !

于 2012-09-06T18:46:16.983 回答
0

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

    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;
        }

    }
于 2012-09-07T12:06:28.440 回答
0

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

我遇到了类似的情况,因此设计了一个解决方案来满足我的需求。检查一下,可能对你有帮助。链接到我的答案

于 2012-09-06T18:39:34.060 回答
0

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

    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 文件中使用任何选择器。这段代码完成了所有工作。祝你好运!

于 2012-09-06T18:28:35.723 回答