Android: fastScrollEnabled não funciona no início
OK, estou trabalhando em um aplicativo que tem uma página com um listview e uma caixa de texto editável na parte superior. À medida que você digita as coisas na caixa de texto editável, ele filtra os itens que são mostrados no listview. O problema que estou tendo é com o ícone de rolagem rápida que aparece no lado do controle deslizante.
Quando a primeira página carrega NO MATTER o que eu faço, o ícone do controle deslizante de rolagem rápida não aparece na tela. Em seguida, clico na caixa de texto de edição, digito um caractere e depois o apago, e agora aparece o ícone do controle deslizante de rolagem rápida.
Primeiro carregue nenhum ícone de rolagem rápida.
Caixa de texto e, em seguida, apaga o texto e o ícone de rolagem rápida aparece.
Eu tenho o android: fastScrollEnabled = "true" definido no meu listview. Além disso, configurei-o manualmente no código, fazendo lv1.setFastScrollEnabled (true);
Não importa o que eu mude, eu ainda consigo o mesmo comportamento, a menos que eu o remova do código e do xml e ele pare de funcionar na segunda página. Eu tentei limpar meu projeto e ainda não é bom. Estou inclinado a ser um bug no android ou estou sentindo falta de algo extremamente simples.
Aqui está o meu código.
<code>public class SearchByFood extends ParentClass { private ListView lv1; private EditText ed; int textlength = 0; private ArrayList<String> arr_sort = new ArrayList<String>(); private ArrayList<String> foods = new ArrayList<String>(); private LayoutInflater mInflater; private ArrayList<Food> foodList; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.search_by_food); setTextTitle("Search by Food"); lv1 = (ListView) findViewById(R.id.ListView01); ed = (EditText) findViewById(R.id.EditText01); mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); DataLayerFunctions d = new DataLayerFunctions(getApplicationContext()); foodList = d.selectFoodsWithSubstitutes(); for (Food f : foodList) { // this is to build a ArrayList<String> to pass to the setAdapter Log.d("SearchByFood", "FoodName: " + f.getFood_Name()); foods.add(f.getFood_Name()); } ArrayAdapter<String> firstAdapter = new ArrayAdapter<String>(SearchByFood.this, R.layout.search_food_listview, foods); lv1.setAdapter(firstAdapter); lv1.setFastScrollEnabled(true); ed.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) { } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { textlength = ed.getText().length(); arr_sort.clear(); for (String f : foods) { if (textlength <= f.length()) { if (f.toString().toLowerCase().contains((CharSequence) ed.getText().toString().toLowerCase())) { Log.d("STRING", "STRING: " + f.toString() + " contains " + ed.getText()); if (ed.getText().length() > 0) { String newString = boldMyString(f, ed.getText().toString()); arr_sort.add(newString); } else { arr_sort.add(f); } } } } // if empty add a no foods found if (arr_sort.isEmpty()) { arr_sort.add("No Foods Found"); } // Load array // lv1.setAdapter(new ArrayAdapter<String> adapter = new ArrayAdapter<String>(SearchByFood.this, R.layout.search_food_listview, arr_sort) { @Override public View getView(int position, View convertView, ViewGroup parent) { View row; if (null == convertView) { row = mInflater.inflate(R.layout.search_food_listview, null); } else { row = convertView; } TextView tv = (TextView) row.findViewById(android.R.id.text1); tv.setText(Html.fromHtml(getItem(position))); // tv.setText(getItem(position)); return row; } }; lv1.setAdapter(adapter); } private String boldMyString(String foodName, String guess) { int gLength = guess.length(); ArrayList<Integer> results = new ArrayList<Integer>(); for (int i = foodName.toLowerCase().indexOf(guess.toLowerCase()); i >= 0; i = foodName.toLowerCase() .indexOf(guess.toLowerCase(), i + 1)) { System.out.println("TEST:" + i); results.add(i); } // Count value is for words that have 2 or more values of guess // in them. int count = 0; for (int i : results) { StringBuffer s1 = new StringBuffer(foodName); s1.insert(i + count, "<b>"); count = count + 3; s1.insert(i + count + gLength, "</b>"); count = count + 4; foodName = s1.toString(); System.out.println("FOOD NAME:" + i + ":" + foodName); } return foodName; } }); // This is what actually does stuff when you click on a listview item. lv1.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // Strip out the bold tags String clicked = (String) lv1.getItemAtPosition(position); clicked = clicked.replaceAll("<b>", ""); System.out.println("Clicked" + clicked); clicked = clicked.replaceAll("</b>", ""); // Find the Food ID match and pass the food id to the // fooddisplay page for (Food f : foodList) { if (null != clicked && clicked.equals(f.getFood_Name())) { Intent intent = new Intent(SearchByFood.this, SubstituteDisplay.class); intent.putExtra("FoodID", f.getFood_ID()); startActivity(intent); } } } }); } @Override public void onBackPressed() { final Intent intent = new Intent(this, MasterTemplateActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); this.startActivity(intent); return; } } </code>
Mais uma vez, qualquer ajuda sobre o motivo de meu ícone de rolagem rápida não aparecer no começo seria muito apreciado. É uma coisa pequena, mas é realmente irritante para mim.