3

我正在研究一个列表视图,并想突出显示列表视图中的第 3 行到第 7 行。

代码:

@Override
public void onCreate(Bundle savedInstanceState) 
{   
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sample_list);
    lv = (ListView) findViewById(R.id.sample_listview);

    String[] Sample_List = getResources().getStringArray(R.array.Sample_List);  
    final ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i < Sample_List.length; ++i) 
    {
        list.add(Sample_List[i]);
    }

    final StableArrayAdapter adapter = new StableArrayAdapter(this, R.layout.list_item, list);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            ...
        }
    });
}

更新:

我已将代码更新如下:

    lv.setAdapter(myAdapter);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            ... 
        }
    });
}

public class MyCustomArrayAdapter extends ArrayAdapter<String> 
{
      private final Context context;
      private final String[] values;

      public MyCustomArrayAdapter(Context context, String[] values) 
      {
        super(context, R.layout.list_item, values);
        this.context = context;
        this.values = values;
      }

      @Override
      public View getView  (int position, View convertView, ViewGroup parent)
      {
          View  view = super.getView(position, convertView, parent);
          if(position>=3&&position<=7)
          {
              view.setBackgroundResource(R.drawable.green_btn);
          }
          else
          {
              view.setBackgroundResource(R.drawable.white_btn);
          }  
          return view;
      }
} 

问题:

我在网上进行了很多研究,但我仍然无法弄清楚如何实现getView...以这种方式如何更改上述内容,例如,将背景第 3 行到第 7 行设置为红色?

实现了更新的代码,listview不能正常展开来显示每个item,只能显示item的边框(绿色按钮有一些黑色边框),可能是因为没有链接列表。

如何将列表与 MyCustomArrayAdapter 连接起来?

4

2 回答 2

3

将突出显示的位置存储在 aList中怎么样,以便您可以执行类似的操作。-

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
    if (highlightedPositions.contains(position)) {
        view.setBackgroundColor(yourColor);
    }
    return view;
}

编辑

现在您有了自己的自定义适配器,请替换您设置StableArrayAdapter.-的代码

final StableArrayAdapter adapter = new StableArrayAdapter(this, R.layout.list_item, list);
lv.setAdapter(adapter);

有了这个。-

final MyCustomArrayAdapter adapter = new MyCustomArrayAdapter(this, list);
lv.setAdapter(adapter);
于 2013-10-12T10:25:35.403 回答
2

这是一个简单的解决方案

 public View getCustomView(int position, View convertView,
                    ViewGroup parent) {
                LayoutInflater inflater = getLayoutInflater();
                View mySpinner = inflater.inflate(R.layout.custom_spinner, parent,
                        false);
                TextView main_text = (TextView) mySpinner
                        .findViewById(R.id.text_main_seen);
                if(position==1)
                main_text.setBackgroundColor(Color.GREEN);
                main_text.setText(spinnerValues[position]);





                return mySpinner;
            }

位置 1 的项目将显示为绿色

于 2013-10-12T11:18:35.347 回答