【问题标题】:Android: fastScrollEnabled not working at firstAndroid:fastScrollEnabled 一开始不工作
【发布时间】:2012-04-21 02:16:28
【问题描述】:

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

首先加载没有快速滚动图标。

编辑文本框,然后擦除文本并出现快速滚动图标。


我在列表视图中设置了 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;
}
}


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

【问题讨论】:

  • 尝试在 xml 中设置 fastScrollEnabled=true。从代码中删除它。可能会奏效。我对 fastscroll 也有类似的问题..

标签: android android-layout android-listview


【解决方案1】:

试试list.setFastScrollAlwaysVisible(true)

还可以尝试list.smoothScrollToPosition(0);,以便在调用滚动时出现图标...

类似的东西..

new Handler().postDelayed(new Runnable() {
  @Override
  public void run(){
    list.smoothScrollToPosition(0);
  }
}, 100);

【讨论】:

  • 好的,我今天正在使用我的应用程序向朋友展示它,并与我的在线数据库同步。我的妻子在列表中添加了大约 10 件东西,在我突然与在线数据库同步后(没有任何代码更改),它一直开始工作。我不确定为什么会解决它,但它确实解决了。虽然,我得给你道具。这也很有效。只需将其修复为 new Handler().po .....再次感谢。
  • @S.A.Jay .. 可能是因为之前您的列表计数小于 35.. 仅当列表计数高于 35 时才会出现快速滚动条...
  • 我以前没有听说过,但它是有道理的。我确实有少于 35 个,然后超过 35 个。我遇到的问题是,正如您从第二个屏幕截图中看到的那样,我在输入字符后出现快速滚动图标,然后在列表中有 35 个之前删除它们.
  • @ngesh 快速滚动是否可用取决于设备上 ListView 的实际高度,而不是一般项目数,是吗? ;)
  • 2019,@ngesh 评论仍然有效。万分感谢!我想知道为什么它不起作用,但它实际上取决于计数。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多