2017-05-04 60 views
0

我有一個editText和一個正常工作的列表視圖。但是我想讓listview在用戶開始輸入之前不可見。 我看着這些鏈接,但它沒有工作android-我不能隱藏listview berfore在editText中輸入

Show/Hide the Custom ListView in android

Hiding Listview until search is initiated

Hide an android ListView until search string is entered

這裏是我的代碼

public class MapAcWithMarker extends FragmentActivity implements OnMapReadyCallback { 

public ListView listView; 
private View parentView; 
public DataAdapter adapter; 
ArrayList<Locations> arrayListTemp=new ArrayList<>(); 
EditText inputSearch; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main_recyc); 
    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 

    inputSearch = (EditText) findViewById(R.id.inputSearch); 
    listView = (ListView) findViewById(R.id.listView); 

    inputSearch.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { 


      } 
     } 

     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
             int arg3) { 
      if(listView.getVisibility() != View.INVISIBLE) 
       listView.setVisibility(View.INVISIBLE); 


     } 



     @Override 
     public void afterTextChanged(Editable arg0) { 
      if(listView.getVisibility() != listView.VISIBLE) 
       listView.setVisibility(listView.VISIBLE); 

     } 
    }); 

回答

1

您可以隱藏你的onCreate的ListView,而不是beforeTextChangedonTextChangedafterTextChanged,你可以取消隱藏它。

+0

由於它的工作! –

0

嘗試使用

listView.setVisibility(View.GONE); 

代替

listView.setVisibility(View.INVISIBLE); 
+0

感謝,但沒有奏效 –

0

首先hideListView從lauout XMLonCreate()方法。

2.顯示ListViewonTextChanged()方法,因爲這將被稱爲當用戶類型的東西。

試試這個:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    ........... 
    .................. 

    inputSearch = (EditText) findViewById(R.id.inputSearch); 
    listView = (ListView) findViewById(R.id.listView); 

    // First hide ListView 
    listView.setVisibility(View.INVISIBLE); 

    inputSearch.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { 
      // Show ListView 
      if(listView.getVisibility() != View.VISIBLE) 
       listView.setVisibility(View.VISIBLE); 
     } 

     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 

     } 

     @Override 
     public void afterTextChanged(Editable arg0) { 

     } 
    }); 
}