控制ListActivity中的各个视图

时间:2011-06-29 08:47:05

标签: android android-layout android-listview

我希望通过ListActivity显示的列表中的所有项目都是不同的颜色(请不要评论糟糕的图形设计;我正在尝试创建演示)。

显示ListActivity很简单。但是,我从阅读其他问题看到你无法遍历ListView。那么,如何控制单个列表条目的TextView?

这是我的示例程序;注释掉的区域显示不起作用的代码。

package com.explorenm.colorlist;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ColorList extends ListActivity {
    private static final String colors[] = {
        "red", "green", "blue", "cyan", "magenta",
        "yellow", "black", "white"
    };
    private static final int colorvalues[] = {
        // red,   green,    blue,     cyan,     
        0xFF0000, 0x00FF00, 0x0000FF, 0x00FFFF, 
        // magenta, yellow,   black,   white
        0xFF00FF, 0xFFFF00, 0x000000, 0xFFFFFF
    };
@Override
public void onCreate(Bundle savedInstanceState) {
    TextView tv = null;
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(this, R.layout.listitem, colors));
    ListView lv = getListView();
//    for (int i=0; i<colors.length; i++) {
//        tv = (TextView) lv.getItemAtPosition(i); // lv.getChildAt(i) also fails
//        tv.setBackgroundColor(colorvalues[i]);
//    }
    }
}

2 个答案:

答案 0 :(得分:2)

您可以实施自己的ListAdapter(或更具体地说,扩展ArrayAdapter<String>)并覆盖getView( int position, View convertView, ViewGroup parent)方法。这使您可以自己构建单个列表项的视图,因此您可以完全按照自己的意愿执行操作。

public class MyAdapter extends ArrayAdapter<String>
    public View getView( final int position, View convertView, final ViewGroup parent )
    {
        View v = convertView;
        if( v == null )
        {
            LayoutInflater li = (LayoutInflater)context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
            v = li.inflate( R.layout.listitem, null );
        }
        String getItem = getItem( position );
        // Style the item and set the text here
    }
}

值得指出的是,每次调用此方法时,应始终尝试重新使用convertView参数,而不是膨胀新视图。这样做的原因是膨胀视图很费劲,不必要地这样做会影响ListView的平滑滚动。

答案 1 :(得分:0)

希望这会有所帮助..

MyList.class

public class MyList extends Activity implements OnItemClickListener {

private List<String> strings = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    strings.add("Android");
    strings.add("Android");
    strings.add("Android");
    strings.add("Android");
    strings.add("Android");
    strings.add("Android");
    strings.add("Android");
    strings.add("Android");
    ListView listView = (ListView) findViewById(R.id.list);
    listView.setAdapter(new MyArrayAdatpter(getApplicationContext(), 0,
            strings));
    listView.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    Toast.makeText(getApplicationContext(), strings.get(arg2)+" at position-"+arg2,
            Toast.LENGTH_SHORT).show();
}
}   

自定义ArrayAdapter

public class MyArrayAdatpter extends ArrayAdapter<String>{

private List<String> list=new ArrayList<String>();
private Context context;
private int randomColour;
private static final int colorvalues[] = {
    // red,   green,    blue,     cyan,     
    Color.RED, Color.GREEN, Color.BLUE, Color.CYAN, 
    // magenta, yellow,   black,   white
    Color.MAGENTA, Color.YELLOW, Color.BLACK, Color.WHITE
};
public MyArratAdatpter(Context applicationContext, int i,   List<String> strings) {
    super(applicationContext,i);
    this.context=applicationContext;
    list=strings;
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

@Override
public String getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
}
@Override
public int getPosition(String item) {
    // TODO Auto-generated method stub
    return list.indexOf(item);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view=LayoutInflater.from(context).inflate(R.layout.textlayout,null);
    TextView textView=(TextView)view.findViewById(R.id.text);
    textView.setText(list.get(position));
    randomColour = new Random().nextInt(7);
    textView.setTextColor(colorvalues[randomColour]);
    return view;
}   
}