ListView - 设置前两个具有不同背景颜色的项目

时间:2011-06-04 06:22:50

标签: android android-listview

在谷歌博客片段布局的附图中,我的代码中有一个列表视图。我的问题是将前两个项目(Henry IV和Henry V)设置为背景颜色RED,其他项目仍为defaul backgroudn值。这可能吗?

任何帮助都会非常感激。

谢谢, 萨纳。

我用来获取ListView的代码是

Log.i(TAG, "Selected item position = "+getListView().getCount()+" Child count = "+getListView().getChildCount());
for (int i=0; i<getListView().getCount();i++) { 
     Log.i(TAG, "Item id = "+getListView().getItemIdAtPosition(i));
     View v = getActivity().findViewById((int) getListView().getItemIdAtPosition(i));
     View v1 = getListView().getChildAt(i);
     Log.i(TAG, "Condition = "+(v == null)); //Here I get the display as true
     Log.i(TAG, "Condition1 = "+(v1 == null)); //Here I get again a true, so basically both are none!
}

enter image description here

2 个答案:

答案 0 :(得分:2)

您最好的办法是为国王保留一个数据项,并使用数组适配器渲染它们,您可以通知它们更改并在渲染时设置适当的颜色。

您所关注的完整示例如下:

public class TestStuff extends Activity {

    private class KingItem {
        public KingItem(String name, String description) {
            this.name = name;
            this.description = description;
            this.hexColor = "#000000";
        }
        public String name;
        public final String description;
        public String hexColor;
    }

    private List<KingItem> kings = new ArrayList<TestStuff.KingItem>();
    private ListView kingsList;
    private ArrayAdapter<String> kingAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        kings.add(new KingItem("Henry IV(1)", "Description form Henry IV(1)"));
        kings.add(new KingItem("Henry V", "Description for Henry V"));
        kings.add(new KingItem("Henry VIII", "Description for Henry VIII"));
        kings.add(new KingItem("Richard II", "Description for Richard II"));

        kingsList = (ListView) findViewById(R.id.list);
        if (kingAdapter == null) {
            kingAdapter = new KingsListAdapter(this, this, android.R.layout.simple_list_item_1, new ArrayList<String>());
        }
        kingAdapter.clear();
        kingsList.setAdapter(kingAdapter);
        kingsList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView desc = (TextView) TestStuff.this.findViewById(R.id.text);
                KingItem king = kings.get(position);
                desc.setText(king.description);
                king.hexColor = "#0000ff";
                for (int i=0; i<position; i++) {
                    kings.get(i).hexColor = "#ff0000";
                }
                for (int i=position+1; i<kings.size(); i++) {
                    kings.get(i).hexColor = "#000000";
                }
                kingAdapter.notifyDataSetChanged();
            }
        });
        for (KingItem king : kings) {
            kingAdapter.add(king.name);
        }
        kingAdapter.notifyDataSetChanged();
    }

    private class KingsListAdapter extends ArrayAdapter<String> {

        private final Activity activity;
        private final int textViewResourceId;

        public KingsListAdapter(Activity activity, Context context, int textViewResourceId, List<String> kingsNames) {
            super(context, textViewResourceId, kingsNames);
            this.activity = activity;
            this.textViewResourceId = textViewResourceId;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(textViewResourceId, null);
            }
            KingItem kingItem = kings.get(position);

            TextView text1 = (TextView) v.findViewById(android.R.id.text1);
            text1.setText(kingItem.name);
            v.setBackgroundColor(Color.parseColor(kingItem.hexColor));
            return v;
        }

    }
}

这会将当前所选项目中的每个项目呈现为红色,将所选项目呈现为蓝色,当前所选项目之后的任何内容都将返回黑色背景。

注意我只是将KingItem设置为正确的颜色,然后告诉列表重新渲染。没有必要获得观点,或父母,并尝试巧妙地设置背景。相反,我将这些信息保存在KingItem中,并使适配器非常简单。

答案 1 :(得分:1)

是的,有可能......请查看以下链接

http://www.androidpeople.com/android-custom-listview-tutorial-part-1

http://www.androidpeople.com/android-custom-listview-tutorial-part-2

在自定义列表视图中添加一个用于下面代码的内容。

public View getView(int position, View convertView, ViewGroup parent) {
   ...... your code add extra line.
   if (position == 0 || position == 1){
    holder.layout.setBackgroundColor(Color.RED);
   }
}