默认设置特定列表视图项的背景

时间:2014-08-22 13:42:11

标签: android

在我的片段中,我已经能够根据列表中项目的点击来控制列表项的背景,例如,如果单击项目0,则其背景设置为黄色,其他列表的背景项目设置为灰色。

我在onitemclicklistener中执行了所有这些操作,它按预期工作。请参阅以下代码:

    lvCodeType.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
            //lvCodeType.getChildAt(position).setBackgroundColor(Color.parseColor("#FFCC00"));

            //lvCodeType.getChildAt(0).setBackgroundResource(R.drawable.bgnd_lv_code_type_item_rollover);

            if(position == 0) {
                desiredCodeType = "ABCD";

                lvCodeType.getChildAt(position).setBackgroundResource(R.drawable.bgnd_lv_code_type_item_rollover);

                lvCodeType.getChildAt(1).setBackgroundResource(R.drawable.bgnd_lv_code_type_item_default);
                lvCodeType.getChildAt(2).setBackgroundResource(R.drawable.bgnd_lv_code_type_item_default);
                lvCodeType.getChildAt(3).setBackgroundResource(R.drawable.bgnd_lv_code_type_item_default);
            } else if(position == 1) {
                desiredCodeType = "EFGH";

                lvCodeType.getChildAt(0).setBackgroundResource(R.drawable.bgnd_lv_code_type_item_default);

                lvCodeType.getChildAt(1).setBackgroundResource(R.drawable.bgnd_lv_code_type_item_rollover);

                lvCodeType.getChildAt(2).setBackgroundResource(R.drawable.bgnd_lv_code_type_item_default);
                lvCodeType.getChildAt(3).setBackgroundResource(R.drawable.bgnd_lv_code_type_item_default);
            } else if(position == 2) {
                desiredCodeType = "AGSOPA";

                lvCodeType.getChildAt(0).setBackgroundResource(R.drawable.bgnd_lv_code_type_item_default);
                lvCodeType.getChildAt(1).setBackgroundResource(R.drawable.bgnd_lv_code_type_item_default);

                lvCodeType.getChildAt(2).setBackgroundResource(R.drawable.bgnd_lv_code_type_item_rollover);

                lvCodeType.getChildAt(3).setBackgroundResource(R.drawable.bgnd_lv_code_type_item_default);                      
            } else if(position == 3) {
                desiredCodeType = "MNOP";

                lvCodeType.getChildAt(0).setBackgroundResource(R.drawable.bgnd_lv_code_type_item_default);
                lvCodeType.getChildAt(1).setBackgroundResource(R.drawable.bgnd_lv_code_type_item_default);
                lvCodeType.getChildAt(2).setBackgroundResource(R.drawable.bgnd_lv_code_type_item_default);

                lvCodeType.getChildAt(3).setBackgroundResource(R.drawable.bgnd_lv_code_type_item_rollover); 
            } else {
                desiredCodeType = "";

                lvCodeType.getChildAt(0).setBackgroundResource(R.drawable.bgnd_lv_code_type_item_default);
                lvCodeType.getChildAt(1).setBackgroundResource(R.drawable.bgnd_lv_code_type_item_default);
                lvCodeType.getChildAt(2).setBackgroundResource(R.drawable.bgnd_lv_code_type_item_default);
                lvCodeType.getChildAt(3).setBackgroundResource(R.drawable.bgnd_lv_code_type_item_default);
            }
        }
    });

现在我想在第一次加载此列表之前,用户参与任何列表项我想设置特定列表项的背景,例如当列表第一次加载背景颜色时项目0的颜色应为黄色,其他项目的背景颜色为灰色。

我在我的片段中尝试了以下代码,但是在设置列表适配器之后在listview的onitemclicklistener之外但它失败了:                 lvCodeType.getChildAt(0).setBackgroundResource(R.drawable.bgnd_lv_code_type_item_default);

我有什么想法可以解决这个问题。

下面是我片段中的列表代码:

// Inflate and customise code type listview
lvCodeType = (ListView) rlMain.findViewById(R.id.lv_codetype);
lvCodeType.setDividerHeight(2);
lvCodeType.setBackgroundResource(R.drawable.bgnd_lv_code_type);

lvCodeTypeData = new ArrayList<LVCodeTypeDrawer>();
lvCodeTypeData.add(new LVCodeTypeDrawer("ABCD"));
lvCodeTypeData.add(new LVCodeTypeDrawer("EFGH"));
lvCodeTypeData.add(new LVCodeTypeDrawer("IJKL"));
lvCodeTypeData.add(new LVCodeTypeDrawer("MNOP"));

lvCodeTypeAdapter = new LVCodeTypeAdapter(((MainActivity) getActivity()), 
                                            lvCodeType, 
                                            R.layout.lv_layout_code_type, 
                                            lvCodeTypeData, 
                                            officialRegularFont, 
                                            officialBoldFont);

lvCodeType.setAdapter(lvCodeTypeAdapter);

以下是我的列表适配器代码:

public class LVCodeTypeAdapter extends ArrayAdapter<LVCodeTypeDrawer> {

    public static String TAG = "LVCodeTypeAdapter";
    Context context;
    List<LVCodeTypeDrawer> drawerItemList;
    int layoutID;
    public Typeface officialBoldFont, officialRegularFont;
    public ListView lvCodeType;

    public LVCodeTypeAdapter(Context context, ListView _lvCodeType, int _layoutResourceID, List<LVCodeTypeDrawer> listItems, 
            Typeface _officialRegularFont, Typeface _officialBoldFont) {

        super(context, _layoutResourceID, listItems);

        this.context = context;
        this.lvCodeType = _lvCodeType;
        this.layoutID = _layoutResourceID;
        this.drawerItemList = listItems;
        this.officialBoldFont =  _officialBoldFont;
        this.officialRegularFont = _officialRegularFont;
    }

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

        final int _position = position;

        LVCodeTypeDrawerItemHolder lvCodeTypeDrawerItemHolder;

        View view = convertView;

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

            lvCodeTypeDrawerItemHolder = new LVCodeTypeDrawerItemHolder();

            view = inflater.inflate(layoutID, parent, false);

            lvCodeTypeDrawerItemHolder.itemName = (TextView) view.findViewById(R.id.tv_codetype);

            view.setTag(lvCodeTypeDrawerItemHolder);
        } else {
            lvCodeTypeDrawerItemHolder = (LVCodeTypeDrawerItemHolder) view.getTag();
        }

        LVCodeTypeDrawer dItem = (LVCodeTypeDrawer) this.drawerItemList.get(_position);

        lvCodeTypeDrawerItemHolder.itemName.setText(dItem.getCodeTypeValue());
        lvCodeTypeDrawerItemHolder.itemName.setTypeface(officialBoldFont);
        lvCodeTypeDrawerItemHolder.itemName.setTextSize(14.5f);
        lvCodeTypeDrawerItemHolder.itemName.setTextColor(Color.BLACK);

        return view;
    }

    private static class LVCodeTypeDrawerItemHolder {
        TextView itemName;
    }

}

感谢。

0 个答案:

没有答案