2

好的,我正在开发一个应用程序,该应用程序的页面顶部有一个列表视图和一个编辑文本框。当您在编辑文本框中输入内容时,它将过滤列表视图中显示的项目。我遇到的问题是出现在滑块侧面的快速滚动图标。
当页面第一次加载时,无论我做什么,快速滚动滑块图标都不会出现在屏幕上。然后我单击编辑文本框并输入一个字符,然后删除它,现在我的快速滚动滑块图标将出现。

首先加载没有快速滚动图标。
没有快速滚动图标
编辑文本框,然后擦除文本并出现快速滚动图标。
出现快速滚动


我在我的列表视图中设置了 android:fastScrollEnabled="true" 。另外,我通过执行 lv1.setFastScrollEnabled(true); 在代码中手动设置它;

无论我进行更改,我仍然会得到相同的行为,除非我从代码和 xml 中完整删除它,然后它将停止在第二页上工作。我已经尝试清理我的项目,但仍然没有好处。我倾向于它是android中的一个错误,或者我错过了一些非常简单的东西。

这是我的代码。

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;
}
}


同样,对于为什么我的快速滚动图标一开始没有出现的任何帮助将不胜感激。这是一件小事,但它真的让我很烦。

4

2 回答 2

14

尝试list.setFastScrollAlwaysVisible(true)

并尝试list.smoothScrollToPosition(0);在调用滚动时显示图标...

像这样的东西..

new Handler().postDelayed(new Runnable() {
  @Override
  public void run(){
    list.smoothScrollToPosition(0);
  }
}, 100);
于 2012-04-24T03:58:31.010 回答
0

所以我再次发布这个是因为我的问题是完全不同的。快速滚动似乎也不适用于ConstraintLayout. ListView快速滚动和快速滚动似乎都RecyclerView不起作用。

于 2018-05-11T07:33:50.037 回答