Home > Net >  Searchview appear on each row of listview
Searchview appear on each row of listview

Time:03-21

the searchview appears on each listview row: Check the scrrenshot attached.

How can i make it apppear only once, at the top.

xml:
<FrameLayout 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"
    tools:context=".Home"
    android:padding="30dp"
   >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ED0316"
        android:textSize="22sp"
        android:fontFamily="sans-serif-condensed-light"
        android:textColor="#fafafa"
        android:id="@ id/txt" />
    <ListView
        android:id="@ id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <!-- TODO: Update blank fragment layout -->
    <SearchView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:queryHint="Search here..."
        android:iconifiedByDefault="false"
        android:focusedByDefault="t`enter code here`rue"
        android:transitionName="Search_box"
        android:id="@ id/search_view">

    </SearchView>
</FrameLayout>

[enter image description here][1]

public class contractorAdapter extends ArrayAdapter { SearchView searchView; public contractorAdapter(@NonNull Context context, ArrayList ArrayList) { super(context, R.layout.fragment_home, ArrayList);

}
public static class ViewHolder {
    SearchView searchView;
}

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

    searchView.setVisibility(position == 0 ? View.VISIBLE : View.GONE); // set it visible if it is at position 0. Else set it invisible
    return convertView;
}

} [1]: https://i.stack.imgur.com/GAsZq.png

CodePudding user response:

It is very easy. You can just pass the position of the ViewHolder to the ViewHolder class. If the position == 0, set the view visible. Else. set it invisible. It can also be done easily like this:

search_view.setVisibility(position == 0 ? View.VISIBLE : View.GONE);

A sample adapter code could be like this:

public class CustomAdapter extends ArrayAdapter<DataModel> {

    private ArrayList<DataModel> dataSet;
    Context mContext;

    // View lookup cache
    private static class ViewHolder {
        SearchView searchView;
    }

    public CustomAdapter(ArrayList<DataModel> data, Context context) {
        super(context, R.layout.row_item, data);
        this.dataSet = data;
        this.mContext=context;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        searchView = convertView.findViewById(R.id.search_view);
        searchView.setVisibility(position == 0 ? View.VISIBLE : View.GONE); // set it visible if it is at position 0. Else set it invisible
        return convertView;
    }
}

CodePudding user response:

If You Use Recycler view for Showing list then you remove search view from the recycler item and put search view in the main activity.

And If You Not Use Recycler View Then Try Below Code

  if (position == 0){
        searchView.visibility = View.VISIBLE
    }else{
        searchView.visibility = View.VISIBLE
    }
  • Related