如何自定义列表视图行的android [英] how to customize listview row android

查看:122
本文介绍了如何自定义列表视图行的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名称列表,我想改变颜色取决于名字的开头字母行。这是我使用什么显示的ListView:

i have a names list and i want to change the color to the rows depending on the letter the name starts with. This is what i'm using to show the ListView:

的main.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<ListView android:id="@+id/listview"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" 
     />

</LinearLayout>

lisviewrows.xml

<?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
</TextView>

main.java

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        List<String> words = populateList();
        List<String> l = removeDoubles(words);
        Collections.sort(l);

        ListView lv = (ListView)findViewById(R.id.listview);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.listviewrows, l);

        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

            }
        });
    }

有一些有用的链接,你可以为我提供或者一些例子吗?

Is there some useful link you can provide me with or some example?

谢谢!

推荐答案

要做到这一点,你需要创建自己的自定义适配器。在getView方法(它返回视图可以显示每个列表项),将您想要的颜色,你的TextView的背景。 关于customAdapters很酷的事情是,你可以做任何事情绝,并显示更加复杂视图的列表项,因为你不局限于TextViews了,你可以在你的列表项XML布局更改为任何类型的浏览/布局..

To do so, you will need to create your own custom adapter. In the getView method (which returns the View to display for each list item), apply the color you want to your TextView's background. The cool thing about customAdapters is that you can do absolutely anything, and display much more complicated Views for your list items, because you are not restricted to TextViews anymore, you can change your list item XML layout to any kind of View/Layout...

事情是这样的:

MyAdapter.java

public class MyAdapter extends BaseAdapter{
    private LayoutInflater inflater;
    private ArrayList<String> data;

    public MyAdapter(Context context, ArrayList<String> data){
    // Caches the LayoutInflater for quicker use
    this.inflater = LayoutInflater.from(context);
    // Sets the events data
    this.data= data;
    }

    public int getCount() {
        return this.data.size();
    }

    public String getItem(int position) throws IndexOutOfBoundsException{
        return this.data.get(position);
    }

    public long getItemId(int position) throws IndexOutOfBoundsException{
        if(position < getCount() && position >= 0 ){
            return position;
        }
    }

    public int getViewTypeCount(){
        return 1;
    }

    public View getView(int position, View convertView, ViewGroup parent){
        String myText = getItem(position);           

        if(convertView == null){ // If the View is not cached
            // Inflates the Common View from XML file
            convertView = this.inflater.inflate(R.id.my_row_layout, null);
        }

        // Select your color and apply it to your textview
        int myColor;
        if(myText.substring(0, 1) == "a"){
            myColor = Color.BLACK;
        }else{
        ....
        }

        convertView.findViewById(R.id.myTextViewId).setBackground(myColor);
        // Of course you will need to set the same ID in your item list XML layout.

        return convertView;
    }
}

,然后在活动的设置是这样的适配器:

and then in your activity set the adapter like this:

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        List<String> words = populateList();
        List<String> l = removeDoubles(words);
        Collections.sort(l);

        ListView lv = (ListView)findViewById(R.id.listview);

        MyAdapter adapter = new MyAdapter(getApplicationContext(), l);

        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

            }
        });
    }

这篇关于如何自定义列表视图行的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆