android - 更改listView的可见性

时间:2017-03-06 03:23:47

标签: android listview visibility

我有一个listView,国家/地区为字符串。

lv = (ListView)findViewById(R.id.listViewCountry);
        ArrayList<String> arrayCountry = new ArrayList<>();
        arrayCountry.addAll(Arrays.asList(getResources().getStringArray(R.array.array_country)));

        adapter = new ArrayAdapter<>(
                MainActivity.this,
                android.R.layout.simple_list_item_1,
                arrayCountry);
        lv.setAdapter(adapter);

我在工具栏上有一个searchView。

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        MenuItem item = menu.findItem(menuSearch);
        SearchView searchView = (SearchView)item.getActionView();

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                adapter.getFilter().filter(newText);
                return false;
            }
        });


        return super.onCreateOptionsMenu(menu);
    }

当应用程序启动时,我需要我的listView GONE的可见性,当我按工具栏中的搜索时,listView应该是可见的。

我是否必须对onOptionsItemSelected

做任何事情
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == menuSearch) {
            lv.setVisibility(View.VISIBLE);

            return true;
        }

        return super.onOptionsItemSelected(item);
    }

但是我的代码无效......

... main.xml中

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/menuSearch"
        android:title="Search"
        android:icon="@android:drawable/ic_menu_search"
        app:actionViewClass="android.widget.SearchView"
        app:showAsAction="always">
    </item>
</menu>

4 个答案:

答案 0 :(得分:2)

setOnSearchClickListener

使用SearchView
@Override
public void onCreate() {
     ...
     yourListView.setVisibility(View.GONE); // or use View.INVISIBLE 
}
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
     SearchView searchView = (SearchView)item.getActionView();
     searchView.setOnSearchClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            lv.setVisibility(View.VISIBLE);
        }
    });
}

答案 1 :(得分:1)

如果您想将视图设置为不可见,您可以尝试:

lv.setVisibility(View.INVISIBLE);

lv.setVisibility(View.GONE);

在您的情况下,您可以将lv设置为onCreate中不可见, 并将listview设置为在onQueryTextSubmit中可见。

  @Override
  public void onCreate() {
     //... others
     lv.setVisibility(View.INVISIBLE);
  }


  @Override
  public boolean onQueryTextSubmit(String query) {
       lv.setVisibility(View.VISIBLE);
       return false;
  }

关闭侦听器的设置:

searchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
                lv.setVisibility(View.GONE);
                return false;
            }
        });

答案 2 :(得分:0)

试试这个 listview.visible = false; 或者您可以更改listview属性.. 找到可见并设置错误

答案 3 :(得分:0)

添加:

<option value="<?php $row[fmake];?>" <?php if($_POST['make'] == $row[fmake]) echo 'selected="selected" '; ?>><?php echo $row[fmake];?></option>
onCreateOptionsMenu

中的