【问题标题】:Dynamically changing TextView color for Three different group in ListView动态更改 ListView 中三个不同组的 TextView 颜色
【发布时间】:2015-02-14 12:41:07
【问题描述】:

我正在尝试开发像索引一样的ListView。在我的情况下,它们是三个级别的列表、章节、子章节和子......我成功地实现了三个级别。

我想为所有三个列表更改三种不同的颜色。

试过了:

  1. 我尝试了 3 级可扩展列表视图,但我无法实现。

  2. 所以我尝试使用单个数组更动态地使用 Listview..

现在我想通过getview方法改变listview中的列表颜色

这是我的 getview() 用于将子章节添加到列表:

            for (int i = listsubchapt.size() - 1; i >= 0; i--) {

                Mainarray.add(pos + 1, listsubchapt.get(i));
                Mainarrayid.add(pos + 1, listsubchaptid.get(i));

                //System.out.println("mainarraywithsub==++ " + Mainarray);
                ArrayAdapter<String> adapter = (new ArrayAdapter<String>(
                        IndexChapter.this, R.layout.singlerow,
                        R.id.textView1, Mainarray){@Override
                        public View getView(int position,
                                View convertView, ViewGroup parent) {

                            LayoutInflater mInflater = (LayoutInflater)getSystemService(
                                    Context.LAYOUT_INFLATER_SERVICE);
                         convertView = mInflater.inflate(R.layout.singlerow, null);

                         for(int i=0;i<Mainarray.size();i++){

                            String sample=Mainarray.get(i);
                            String[] items = sample.split(":"); 
                            if (items.length == 1) {
                                TextView txt = ((TextView) convertView.findViewById(R.id.textView1));
                                txt.setBackgroundColor(Color.CYAN);

                            }else if(items.length == 2){
                                TextView txt = ((TextView) convertView.findViewById(R.id.textView1));
                                txt.setBackgroundColor(Color.BLUE);
                            }else if(items.length == 3){
                                TextView txt = ((TextView) convertView.findViewById(R.id.textView1));
                                txt.setBackgroundColor(Color.GRAY);
                            }else{
                                TextView txt = ((TextView) convertView.findViewById(R.id.textView1));
                                txt.setBackgroundColor(Color.CYAN);
                            }
                         }
                            return super.getView(position, convertView, parent);
                        }});


                clientdetailslist.setAdapter(adapter);
                clientdetailslist.setSelection(pos-1);
                adapter.notifyDataSetChanged();

            } 

编辑:

它移动到 length=1 块并将所有值更改为青色。

【问题讨论】:

  • 你有什么问题?
  • @SweetWishershi 我想给章节、子章节和孩子三种不同的颜色
  • 那么txt.setBackgroundColor(Color.BLUE);这是做什么的?
  • return convertView 而不是 return super.getView(position, convertView, parent);
  • @SweetWishershi 列表中的所有值都变为蓝色.. 但我想单独更改子章节变为蓝色

标签: android listview android-listview getview


【解决方案1】:

返回 convertView 而不是 super.getView(...)。以下是一些示例:https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView

【讨论】:

  • 查看我的输出图像..我想通过颜色显示差异..比如章节、子章节、子项
【解决方案2】:

Expentable listview 的实例,您可以使用listview。

   @Override
   public View getView(int position,
     View convertView, ViewGroup parent) {
    LayoutInflater mInflater = (LayoutInflater)getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE);
        View rowView = mInflater.inflate(R.layout.singlerow, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.textView1);
        RelativeLayout txt = ((RelativeLayout) rowView.findViewById(R.id.clicked));
        
        textView.setText(Mainarray.get(position));
        
        String s = Mainarray.get(position);
        String s2 = s.substring (1,7);
        String[] items = s2.split(":");
        if (items.length == 1) {
      //chapter color
      txt.setBackgroundColor(Color.WHITE);
      
     }else if(items.length == 2){
      //subchapter color
      txt.setBackgroundColor(Color.parseColor("#E6F5FF"));
     }else if(items.length == 3){
      //child color
      txt.setBackgroundColor(Color.parseColor("#CCEBFF"));
     }else{
      // else block if u need
      txt.setBackgroundColor(Color.MAGENTA);
     }


        return rowView;

   }}

现在我的屏幕是:

【讨论】:

    猜你喜欢
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 2011-12-26
    • 2015-12-17
    相关资源
    最近更新 更多