android - 更改主要 Activity 中的 ListView 字段属性

标签 android listview android-listview baseadapter

我正在开发一个 Android 应用程序。在我的主要 Activity 中,我必须实现一个列表。以下是我的页面的示例形状

|----------------------| \
|  |Button|            |  \
|----------------------|   \                  
|listview row1         | \   \
|listview row1         |  \   \---------Screen
|listview row1         | / --/----- ListView 
|                      |/   /
|                      |  /
|                      | /
|______________________|/

该按钮位于我的 Activity 页面中, ListView 行正在基础适配器中创建。 ListView 包含 TextView 。现在,当我从 Activity 中单击按钮时,我必须更改 TextView 背景颜色,下次单击按钮时,必须更改 TextView 颜色会保留旧的颜色。 friend 们我该怎么办?我在 getview() 方法中声明了textview。

最佳答案

可能还有其他方法,但我会循环浏览按钮的 OnClick 方法中的列表行。像这样的东西:

在您的 Activity 字段定义中:

    static final int colourA=Color.argb(255,255,0,0);
    static final int colourB=Color.argb(255,0,255,0);
    int currentColour=colourA;

在您的 Activity OnCreate 中:

        Button myButton = (Button) findViewById(R.id.myButton); 
        final ListView myListView = (ListView) findViewByID(R.id.myListView);
        //change myButton to your button id, and myListView to your ListView id
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //This is the code to toggle the colours, you can do pretty much whatever you want here though
                if (currentColour==colourA){
                    currentColour=colourB;
                } else {
                    currentColour=colourA;
                }

                //This cycles through all the root views in the ListView. If you want to change the
                //colour of only one view in the row layout, in the for loop use 
                //rowView.findViewById(R.id.myViewInRow).setBackgroundColor(currentColour);
                //instead, to get the relevant view in the row
                View rowView;
                for (int i=0;i<myListView.getChildCount();i++){
                    rowView=myListView.getChildAt(i);
                    rowView.setBackgroundColor(currentColour);
                }
            }
        });

关于android - 更改主要 Activity 中的 ListView 字段属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12119735/

相关文章:

现有项目的 Android xml 图形布局/设计未正确加载

android - 禁用 Android 应用程序中的所有动画

android - RecyclerView.ViewHolder 与 Fragment 通信

java - 单击 ListView 中的按钮不起作用

android - ListView 上的动画背景

android - 由 EditText 组成的自定义 ListAdapter 失去焦点调用两次

android - ListView 项目在滚动时随机排列

android - 单击 ListView

c# - 在 C# 中从 ListView 中选择一个项目

android - 实现 "faster"backToTop - ListView