Когда я нажимаю на элемент в RecyclerView
, фокус с SearchView
исчезает, когда я снова нажимаю на любой элемент RecyclerView
, фокус снова появляется на SearchView
, и клавиатура мешает с полной список. Как это исправить?
frag_search. xml (добавлено в head_container в MainActivity)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/toolbar_main" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewSearch"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
app:layoutManager="LinearLayoutManager" />
</RelativeLayout>
</LinearLayout>
activity_main. xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/container_from_toolbar"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:orientation="vertical" />
</ScrollView>
</LinearLayout>
поисковый_фрагмент. java
public class SearchFragment extends Fragment {
private List<Radio> mRadios;
private List<Station> mStations;
private SearchView mSearchView;
private RecyclerView mRecyclerView;
private FilterRecyclerStationAdapter mAdapter;
private LinearLayout mLinearLayout;
private String preSearchText;
public SearchFragment(List<Radio> radios, LinearLayout container) {
mRadios = radios;
mStations = new ArrayList<>();
mLinearLayout = container;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_search, container, false);
setHasOptionsMenu(true);
for(Radio radio : mRadios)
mStations.addAll(radio.getStations());
mRecyclerView = view.findViewById(R.id.recyclerViewSearch);
mAdapter = new FilterRecyclerStationAdapter(inflater, mStations, view.getContext());
mRecyclerView.setAdapter(mAdapter);
Toolbar toolbar = view.findViewById(R.id.toolbar_main);
AppCompatActivity activity = (AppCompatActivity)getActivity();
activity.setSupportActionBar(toolbar);
return view;
}
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
getActivity().getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.action_search);
mSearchView = (SearchView) item.getActionView();
mSearchView.setBackgroundResource(R.drawable.bg_grey_searchview);
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
KeyboardHelper.closeKeyboard(getContext());
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
if (newText.isEmpty()){
setVisibilityContainers(View.VISIBLE, View.GONE);
} else {
setVisibilityContainers(View.GONE, View.VISIBLE);
}
mAdapter.getFilter().filter(newText);
return true;
}
private void setVisibilityContainers(int mainContainerVisibility, int bodyContainerVisibility) {
mLinearLayout.setVisibility(mainContainerVisibility);
mRecyclerView.setVisibility(bodyContainerVisibility);
}
});
super.onCreateOptionsMenu(menu, inflater);
}
}
адаптер. java
public class FilterRecyclerStationAdapter extends RecyclerView.Adapter<FilterRecyclerStationAdapter.ViewHolder> implements Filterable {
private LayoutInflater mLayoutInflater;
private Context mContext;
private static AnimatorHelper animatorHelper;
private List<Station> mAllStations;
private List<Station> filteredList;
public FilterRecyclerStationAdapter(LayoutInflater layoutInflater, List<Station> stations, Context context) {
this.mLayoutInflater = layoutInflater;
this.mContext = context;
mAllStations = new ArrayList<>(stations);
filteredList = mAllStations;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<Station> tempFilteredList = new ArrayList<>();
if (constraint.toString().isEmpty()) {
filteredList = mAllStations;
} else {
for (Station station : mAllStations) {
if (station.getName().toLowerCase().contains(constraint.toString().toLowerCase())) {
tempFilteredList.add(station);
}
}
filteredList = tempFilteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = filteredList;
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredList = (ArrayList<Station>) results.values;
notifyDataSetChanged();
}
};
return filter;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mLayoutInflater.inflate(R.layout.example_list_item_station, parent, false);
FilterRecyclerStationAdapter.ViewHolder viewHolder = new FilterRecyclerStationAdapter.ViewHolder(view);
return new FilterRecyclerStationAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.textView.setText(filteredList.get(position).getName());
Glide.with(mContext)
.load(filteredList.get(position).getImage())
.error(R.drawable.ic_launcher_foreground)
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.into(holder.imageView);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createFirebaseReport(position);
Log.d("debug", filteredList.get(position).getName());
Player player = new Player(filteredList.get(position).getStream());
player.start(mContext);
startPlayerService();
if (animatorHelper != null)
animatorHelper.stopAnimation();
animatorHelper = new AnimatorHelper(holder.playViewAnimation);
animatorHelper.startAnimation();
KeyboardHelper.closeKeyboard(mContext);
Log.d("anm", String.valueOf(holder.getItemId()));
}
private void startPlayerService() {
Intent serviceIntent = new Intent(mContext, NotificationService.class);
serviceIntent.setAction(Const.ACTION.STARTFOREGROUND_ACTION);
mContext.startService(serviceIntent);
}
});
}
private void createFirebaseReport(int position) {
FirebaseAnalytics firebaseAnalytics = FirebaseAnalytics.getInstance(mContext);
Bundle eventDetails = new Bundle();
StringBuilder sb = new StringBuilder();
sb.append(filteredList.get(position).getName())
.append(" : ").append(filteredList.get(position).getStream());
eventDetails.putString("station", sb.toString());
firebaseAnalytics.logEvent("select_station", eventDetails);
}
@Override
public int getItemCount() {
return filteredList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
final ImageView imageView;
final TextView textView;
final AVLoadingIndicatorView playViewAnimation;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.ivStation);
textView = itemView.findViewById(R.id.tvStation);
playViewAnimation = itemView.findViewById(R.id.playing_anim);
}
}
}