2

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

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

该按钮在我的活动页面中,listview 行正在baseadapter 中创建。Listview 包含一个 textview。现在,当我从活动中单击按钮时,我必须更改 Textviews 背景颜色,下次单击按钮时,textviews 颜色将保留旧颜色。我该怎么做朋友?我在 getview() 方法中声明了 textview。

4

2 回答 2

2

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

在您的活动字段定义中:

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

在您的活动 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);
                }
            }
        });
于 2012-08-25T07:15:57.167 回答
0

最后我得到了解决方案。它可能对其他人有帮助。但我不是关于我的代码的质量。

第1步)

我在我的活动中创建变量

static int hidestate=0;

在点击方法的隐藏按钮中,我写了这个

hide_btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if (hidestate==0) {

                hidestate=1;

                sMSConversationAdapter.notifyDataSetChanged();
                hide_btn.setText("Show All");

            }else {
                hidestate=0;

                sMSConversationAdapter.notifyDataSetChanged();
                hide_btn.setText("Hide All");
            }

        }
    });

步骤 2) 以下是我的 BaseAdapter 类 getView()

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

    View vi=convertView;
    final TextView message;

    if(convertView==null)
        vi = inflater.inflate(R.layout.smsconversation_row, null);

    RelativeLayout nws=(RelativeLayout)vi.findViewById(R.id.all);
    message=(TextView)vi.findViewById(R.id.snt_txt);

    if (SMSConversationHome.hidestate==1) {
        message.setVisibility(View.INVISIBLE);

    }
    else{

        message.setVisibility(View.VISIBLE);

    }
}

谢谢朋友们的帮助。

于 2012-08-25T10:24:46.367 回答