单击搜索按钮时,Android列表视图会滚动到底部

时间:2017-03-21 19:58:55

标签: android listview

我已经使用搜索工具实现了listview。搜索按钮位于操作栏中,使用以下代码一切正常。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.top_right, menu);
    MenuItem searchItem = menu.findItem(R.id.search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    //*** setOnQueryTextFocusChangeListener ***
    searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {

        }
    });

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

            return false;
        }

        @Override
        public boolean onQueryTextChange(String searchQuery) {
                myAppAdapter.filter(searchQuery.toString().trim());
                listView.invalidate();
            return true;
        }
    });

    MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            // Do something when collapsed
            return true;  // Return true to collapse action view
        }

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            // Do something when expanded
            return true;  // Return true to expand action view
        }
    });
    return true;
} 

这是我的XML:

    <ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/appBar"
    android:choiceMode="multipleChoice"
    android:id="@+id/list1"
    android:layout_above="@+id/txtTotal" />

我的搜索功能正常但问题是当有人点击搜索按钮时,整个列表视图会滚动到底部。我怎么能解决这个问题? 提前谢谢!

注意:我已经搜索了stackoverflow这个问题,但我没有找到任何问题。如果有,请与我分享链接。

编辑:有关我的适配器的其他信息。正如评论部分所述。

     public class MyAppAdapter extends BaseAdapter {
    public class ViewHolder {
        TextView txtName;
        CheckBox chkItem;
    }

    public List<ClassOrder> parkingList;

    public Context context;
    ArrayList<ClassOrder> arraylist;

    private MyAppAdapter(List<ClassOrder> apps, Context context) {
        this.parkingList = apps;
        this.context = context;
        arraylist = new ArrayList<ClassOrder>();
        arraylist.addAll(parkingList);
    }

    @Override
    public int getCount() {
        return parkingList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View rowView = convertView;
        /*ViewHolder viewHolder;*/

        if (rowView == null) {
            LayoutInflater inflater = getLayoutInflater();
            rowView = inflater.inflate(R.layout.list_xml, null);
            // configure view holder
            viewHolder = new ViewHolder();
            viewHolder.chkItem = (CheckBox) rowView.findViewById(R.id.chkItem);
            rowView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.txtName.setText(parkingList.get(position).getItemDetail() + "");
        viewHolder.chkItem.setFocusable(false);
        viewHolder.chkItem.setChecked(positionArray.get(position));

        return rowView;
    }
}

我的list_xml:

    <RelativeLayout
    android:layout_width="match_parent"
    android:id="@+id/ac"
    android:padding="10dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtSub"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        app:srcCompat="@mipmap/ic_launcher"
        android:id="@+id/imageView2"
        android:layout_marginRight="5dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:text="menu name"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/txtName"
        android:textSize="14sp"
        android:textStyle="bold"
        android:textColor="@color/colorPrimary"
        android:gravity="center|left"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/imageView2"
        android:layout_toEndOf="@+id/imageView2"
        android:layout_alignBottom="@+id/imageView2" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:button="@null"
        android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
        android:buttonTint="@color/colorPrimary"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="right|center"
        android:id="@+id/chkItem"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignBottom="@+id/txtName"
        android:layout_toRightOf="@+id/txtName"
        android:layout_toEndOf="@+id/txtName" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

实际上我无意中在搜索菜单按钮的 OnClick 中添加了以下行。这导致listview向下滚动。

 listView.smoothScrollToPosition(myAppAdapter.getCount());

很抱歉,我搜索了所有代码是因为错误而不是onClick事件。谢谢大家和 Stackoverflowians 谁花时间解决这个问题。我再次感到抱歉!