【问题标题】:Android custom ArrayAdapter not staying on scrollAndroid自定义ArrayAdapter不保持滚动
【发布时间】:2013-07-24 04:44:16
【问题描述】:

我创建了一个自定义的ArrayAdapter(下面的代码),在我滚动之前它工作得很好。一旦我滚动所有项目都会变成绿色,我一直在拉头发试图找出原因。任何帮助将不胜感激。

自定义适配器旨在使任何 ListView 项的文本长度为 3 个字符 变为绿色,而所有其他颜色应保持默认颜色为黑色。

public class FoundAdapter extends ArrayAdapter<String> {

  private final Activity context;
  private final ArrayList<String> names;

  static class ViewHolder {
    public TextView text;
  }

  public FoundAdapter(Activity context, ArrayList<String> names) {
    super(context, R.layout.found, names);
    this.context = context;
    this.names = names;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;
    if (rowView == null) {
      LayoutInflater inflater = context.getLayoutInflater();
      rowView = inflater.inflate(R.layout.found, null);
      ViewHolder viewHolder = new ViewHolder();
      viewHolder.text = (TextView) rowView.findViewById(R.id.found_txt);
      rowView.setTag(viewHolder);
    }

    ViewHolder holder = (ViewHolder) rowView.getTag();
    String s = names.get(position);
    if(s.length()==3) {
        holder.text.setTextColor(0xFF008B45); //green
    }
    holder.text.setText(s);

    return rowView;
  }
} 

在 .java 代码中通过以下方式调用:

adapter=new FoundAdapter(this, array);      
ListView view = (ListView) findViewById(R.id.list);
view.setAdapter(adapter);

R.layout.found:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView 
    android:id="@+id/found_txt"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:gravity="center"  
  android:textSize="20sp"     
/>
</LinearLayout>

【问题讨论】:

    标签: android android-layout listview customization android-arrayadapter


    【解决方案1】:

    问题是如果条件不满足,您不会重置文本颜色。在 Listviews 中,视图被回收。

    所以做这样的事情

    if(s.length()==3) {
        holder.text.setTextColor(0xFF008B45); //green
    }else{
        holder.text.setTextColor(xyz); //xyz
    }
    

    【讨论】:

      【解决方案2】:

      您应该在适配器外部使用此颜色条件,将其放在适配器外部的数组列表中

             if(s.length()==3) {
             /**
             put your green color in array list 
             */
             }else{
             //put your other color
              }
      

      & 在您的适配器中只需使用 :-

       holder.text.setText(list.get(position).colorName);
      

      【讨论】:

      • 你能说清楚一点吗,我不太清楚你的意思
      【解决方案3】:
      public class FoundAdapter extends ArrayAdapter<String> {
      
          private final Activity context;
          private final ArrayList<String> names;
      
          public FoundAdapter(Activity context, ArrayList<String> names) {
              super(context, R.layout.found, names);
              this.context = context;
              this.names = names;
          }
      
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
              View rowView = convertView;
              if (rowView == null) {
                  LayoutInflater inflater = context.getLayoutInflater();
                  rowView = inflater.inflate(R.layout.found, null);
                  ViewHolder viewHolder = new ViewHolder();
                  viewHolder.text = (TextView) rowView
                          .findViewById(R.id.found_txt);
                  rowView.setTag(viewHolder);
              }
      
              ViewHolder holder = (ViewHolder) rowView.getTag();
              String s = names.get(position);
              if (names.get(position).length() == 3) {
                  holder.text.setTextColor(0xFF008B45); // green
                  holder.text.setText(s); // green
              }
              else
              {
                  holder.text.setTextColor(0x11111111); // green
                  holder.text.setText(s); // green
              }
      
              //holder.text.setText(s);
      
              return rowView;
          }
      }
      
      static class ViewHolder {
          public TextView text;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-04
        • 1970-01-01
        • 2020-08-03
        相关资源
        最近更新 更多