0

我想ListView通过按钮切换 a 的可见性,但是当我@android:id/list用于 ListView 时,我不知道如何通过以下方式在 Activity 中检索视图findViewById(R.id. list);

为此,我有以下代码:

public void feedButtonListener() {

    final ImageButton ToggleButton = (ImageButton) findViewById(R.id.ToggleFeed);

    ToggleButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Toast.makeText(MainActivity.this, "Toggleis clicked!",
                    Toast.LENGTH_SHORT).show();
            ListView listView = (ListView) findViewById(R.id.list); //HOW?
            if (feedView) {
                listView.setVisibility(View.GONE); 
                ToggleButton
                        .setImageResource(R.drawable.ic_inactive);
                feedView = false;
            } else {
                listView.setVisibility(View.VISIBLE);
                ToggleButton
                        .setImageResource(R.drawable.ic_active);
                feedView = true;

            }
        }

    });

在布局中:

<ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginBottom="@dimen/margin"
        android:layout_marginTop="@dimen/margin" >
</ListView>
4

1 回答 1

3
ListView listView = (ListView) findViewById(android.R.id.list); //thats how :)

注意前面的android:android .R.id.list

如果你在一个ListFragment或者ListActivity你可以打电话

ListView listView = getListView();
于 2013-10-17T21:13:15.337 回答