更改列表项的背景颜色自定义列表视图 [英] Changing background color of list items in custom list view

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

问题描述

我用下面的code创建一个自定义列表视图....但有了这个code它,它只是选择一个item..but突出的许多项目的问题......我的意思是..for例子..如果我在list..And 8个项目我只能看到3个项目(其余的我必须滚动看到)..如果我单击第一个项目......它就会与第四和第七项突出沿...

I used the following code create a custom listview ....But the problem with this code it it is selecting only one item..but highlighting many items...i mean ..for example..if i have 8 items in the list..And i can see only 3 items(rest i have to scroll to see)..if i click the first item...it gets highlighted along with the fourth and the 7th item...

public class MainMenu extends Activity {
    ListView lmenu;
    View v1;
    String s;
    Class<?> ourclass;
    View layout, row;
    static int trantype;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menulist);

        Menu Menu_data[] = new Menu[] { new Menu("1.White"),
                new Menu("2.Blue"), new Menu("3.Purple"), new Menu("4.Red"),
                new Menu("5.Yellow"), new Menu("6.Black"), new Menu("6.Black"),
                new Menu("6.Black"), new Menu("6.Black"), new Menu("6.Black"),
                new Menu("6.Black"), new Menu("6.Black") };

        MenuAdapter adapter = new MenuAdapter(this, R.layout.menutext,
                Menu_data);
        lmenu = (ListView) findViewById(R.id.mainmenu);

        lmenu.setAdapter(adapter);

        lmenu.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> ada, View v, int position,
                    long id) {
                // TODO Auto-generated method stub

                /*
                 * v.setBackgroundColor(Color.parseColor("#FCD5B5")); if (!(v1
                 * == null) && v1 != v)
                 * v1.setBackgroundColor(Color.parseColor("#EEEEEE")); v1 = v;
                 */
                Intent swipeit = new Intent(getBaseContext(), Swipeit.class);
                trantype = position + 1;
                startActivity(swipeit);
            }
        });

        findViewById(R.id.BLogout).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        });
    }

    public class Menu {
        public String title;

        public Menu() {
            super();
        }

        public Menu(String title) {
            super();
            this.title = title;
        }
    }

    public class MenuAdapter extends ArrayAdapter<Menu> {

        Context context;
        int layoutResourceId;
        Menu data[] = null;
        LayoutInflater inflater;
        boolean[] arrBgcolor;
        private int activeHex, inactiveHex;

        public MenuAdapter(Context context, int layoutResourceId, Menu[] data) {
            super(context, layoutResourceId, data);
            this.layoutResourceId = layoutResourceId;
            this.context = context;
            this.data = data;
            activeHex = Color.parseColor("#FCD5B5");
            inactiveHex = Color.parseColor("#EEEEEE");

            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            arrBgcolor = new boolean[13];
        }

        @Override
        public View getView(final int position, final View convertView,
                ViewGroup parent) {
            try {
                MenuHolder holder = null;
                row = convertView;
                // convertView.setBackgroundColor(Color.BLACK);
                if (row == null) {
                    LayoutInflater inflater = ((Activity) context)
                            .getLayoutInflater();
                    row = inflater.inflate(layoutResourceId, parent, false);
                    holder = new MenuHolder();
                    holder.txtTitle = (TextView) row.findViewById(R.id.tv1);
                    row.setTag(holder);
                } else {
                    holder = (MenuHolder) row.getTag();
                }

                Menu Menu = data[position];
                holder.txtTitle.setText(Menu.title);
                holder.txtTitle.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        resetArrbg();
                        arrBgcolor[position] = true;
                        if (arrBgcolor[position]) {
                            row.setBackgroundColor(activeHex);
                        } else {
                            row.setBackgroundColor(inactiveHex);
                        }
                        notifyDataSetChanged();
                    }
                });
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), String.valueOf(e),
                        Toast.LENGTH_LONG).show();
            }
            return row;
        }

        private void resetArrbg() {
            for (int i = 0; i < arrBgcolor.length; i++) {
                arrBgcolor[i] = false;
            }
        }

        public class MenuHolder {
            TextView txtTitle;
        }
    }
}

包含列表我的XML ...
    
    

my xml containing list...

    <include
        android:id="@+id/header"
        android:layout_alignParentTop="true"
        layout="@layout/header" />

    <RelativeLayout
        android:id="@+id/Rlmain"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/header"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/TMain"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="8dp"
            android:text="Main Menu"
            android:textColor="#000000"
            android:textSize="15dp" />

        <View
            android:id="@+id/Vtop"
            android:layout_width="fill_parent"
            android:layout_height="2dp"
            android:layout_below="@+id/TMain"
            android:background="@android:color/darker_gray" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/Vbot"
        android:layout_below="@+id/Rlmain"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/mainmenu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#E0E0E0"
            android:cacheColorHint="#00000000"
            android:divider="@android:color/transparent"
            android:dividerHeight="20dp" >
        </ListView>
    </RelativeLayout>

    <View
        android:id="@+id/Vbot"
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:layout_above="@+id/textView1"
        android:background="@android:color/darker_gray" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="© India Transact Services Ltd."
        android:textColor="#000000"
        android:textSize="15dp" />

</RelativeLayout>

我的XML的名单....

my xml for list....

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LLtv"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#EEEEEE"
    android:cacheColorHint="#00000000" >

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:paddingBottom="12dp"
        android:paddingTop="12dp"
        android:textColor="#000000"
        android:textSize="20dp" />

</LinearLayout>

可以请任何人能帮助我,告诉我要去的地方错了?

Can please anyone help me and tell where i am going wrong?

推荐答案

您想要什么不能用当前设置来实现。你需要在这里你可以访问 getView()方法来实现自定义适配器。由于在回答更加明确的原因<一href=\"http://stackoverflow.com/questions/13275036/how-can-i-make-persistent-changes-to-children-in-an-expandablelistview/13275422#13275422\">here,你需要做的是使用某种类型的数据容器中,将使用一些指标单独举行行的状态,然后执行你的行动基于它的容器上的位置(这应该对应于它的位置在列表视图)

What you want can't be achieved with your current setup. You need to implement a custom adapter where you have access to the getView() method. For reasons made clearer in the answer here, what you need to do is use some sort of data-container that will hold the status of an individual row using some indicator and then perform your action based on it's position on the container (which should correspond to its position on the listview)

例如,看看这个重写:

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    resetArrbg();
    arrBgcolor[position] = true;
    if (arrBgcolor[position]) {
        v.setBackgroundColor(Color.parseColor("#FCD5B5"));
    } else {
        v.setBackgroundColor(Color.BLUE);           
    }
}

boolean[] arrBgcolor = new boolean[list.size()];

private void resetArrbg() {
    for (int i = 0; i < arrBgcolor.length; i++) {
        arrBgcolor[i] = false;
    }
}

是否有意义,现在为什么不能用当前的设置工作?该方法的其他部分,影响其他意见的一部分,不能发生,因为你没有访问其他位置的 onListItemClick 方法,但你 getView做()。这是当然的,除非你知道解决的办法,然后,通过各种手段,更多的权力给你。都是一样的,我不认为在 V1 技术做你任何好处。

Does it make sense now why it can't work with the current set-up? The else part of the method, the part affecting the other views, can never take place because you don't have access to the other positions in the onListItemClick method, but you do in getView(). This is of course, unless you know of a way around this then, by all means, more power to you. all the same i don't think the v1 technique do you any good.

编辑:

public class MainActivity extends Activity {
    ListView lmenu;
    View v1;
    String s;
    Class<?> ourclass;
    View layout, row;
    static int trantype;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menulist);

        Menu Menu_data[] = new Menu[] { new Menu("1.White"),
                new Menu("2.Blue"), new Menu("3.Purple"), new Menu("4.Red"),
                new Menu("5.Yellow"), new Menu("6.Black"), new Menu("6.Black"),
                new Menu("6.Black"), new Menu("6.Black"), new Menu("6.Black"),
                new Menu("6.Black"), new Menu("6.Black") };

        MenuAdapter adapter = new MenuAdapter(this, R.layout.menutext, Menu_data);
        lmenu = (ListView) findViewById(R.id.mainmenu);
        lmenu.setAdapter(adapter);
    }

    public class Menu {
        public String title;

        public Menu() {
            super();
        }

        public Menu(String title) {
            super();
            this.title = title;
        }
    }

    public class MenuAdapter extends ArrayAdapter<Menu> {

        Context context;
        int layoutResourceId;
        Menu data[];
        LayoutInflater inflater;
        boolean[] arrBgcolor;
        private int activeHex, inactiveHex;

        public MenuAdapter(Context context, int layoutResourceId, Menu[] data) {
            super(context, layoutResourceId, data);
            this.layoutResourceId = layoutResourceId;
            this.context = context;
            this.data = data;


            activeHex = Color.parseColor("#FCD5B5");
            inactiveHex = Color.parseColor("#EEEEEE");

            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            arrBgcolor = new boolean[data.length];
            resetArrbg();
        }

        @Override
        public View getView(final int position, final View convertView,
                ViewGroup parent) {

            final MenuHolder holder;
            row = convertView;
            // convertView.setBackgroundColor(Color.BLACK);
            if (row == null) {
                row = inflater.inflate(layoutResourceId, parent, false);
                holder = new MenuHolder();
                holder.txtTitle = (TextView) row.findViewById(R.id.tv1);
                row.setTag(holder);
            } else {
                holder = (MenuHolder) row.getTag();
            }

            Menu Menu = data[position];
            holder.txtTitle.setText(Menu.title);


            if (arrBgcolor[position]) {
                row.setBackgroundColor(activeHex);
            } else {
                row.setBackgroundColor(inactiveHex);
            }

            holder.txtTitle.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    resetArrbg();
                    arrBgcolor[position] = true;
                    notifyDataSetChanged();
                }
            });

            return row;
        }

        private void resetArrbg() {
            for (int i = 0; i < arrBgcolor.length; i++) {
                arrBgcolor[i] = false;
            }
        }

        public class MenuHolder {
            TextView txtTitle;
        }
    }
}

这篇关于更改列表项的背景颜色自定义列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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