gpt4 book ai didi

单击搜索按钮时,Android ListView 滚动到底部

转载 作者:行者123 更新时间:2023-11-30 00:38:40 24 4
gpt4 key购买 nike

我已经实现了带有搜索功能的 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" />

我的搜索功能工作正常但问题是当有人点击搜索按钮时,整个 ListView 滚动到底部。我将如何解决这个问题?提前致谢!

注意:我已经在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>

最佳答案

实际上我无意中将以下行放在搜索菜单按钮的 OnClick 中。这导致 ListView 向下滚动。

 listView.smoothScrollToPosition(myAppAdapter.getCount());

很抱歉,我搜索了我所有的代码是否有误,但没有搜索到 onClick 事件。感谢大家和花时间解决这个问题的 Stackoverflowians。再次抱歉!

关于单击搜索按钮时,Android ListView 滚动到底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42937042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com