繁体   English   中英

动态更改ListView中三个不同组的TextView颜色

[英]Dynamically changing TextView color for Three different group in ListView

这是当前输出

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

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

尝试过:

  1. 我尝试了3级宜人的Listview,但我无法实现。

  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块,并将所有值更改为青色。

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

终于我找到了解决我问题的答案..这可能会帮助某人.. Expentable listview实例,您可以使用listview .. ThanQ eveyone ..

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

   }}

现在我的屏幕是:

改变颜色后我的屏幕

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM