menu klasöründeyken menu_main.xml isimli dosya üretiniz ve içerisine;

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".Activity.MainActivity">

    <item
        app:showAsAction="always"
        android:id="@+id/app_bar_search"
        android:icon="@drawable/ic_search_black_24dp"
        android:title="Arama yap"
        app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

satir_goruntusu.xml layout'u üretiniz ve içerisine

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <ImageView
        android:id="@+id/ivUlkeBayrak"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        app:srcCompat="@drawable/ic_launcher_background" />

    <TextView
        android:id="@+id/tvUlkeAdi"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:layout_toEndOf="@+id/ivUlkeBayrak"
        android:text="TextView"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="33dp"
        android:layout_toEndOf="@+id/ivUlkeBayrak"
        android:text="Başkent" />

    <TextView
        android:id="@+id/tvBaskent"
        android:layout_width="124dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="77dp"
        android:layout_marginBottom="-1dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvBaskent"
        android:layout_marginLeft="10dp"
        android:layout_toEndOf="@+id/ivUlkeBayrak"
        android:text="Kuruluş" />

    <TextView
        android:id="@+id/tvKurulus"
        android:layout_width="124dp"
        android:layout_height="22dp"
        android:layout_below="@+id/tvBaskent"
        android:layout_alignStart="@+id/tvBaskent"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="77dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView4"
        android:layout_marginLeft="10dp"
        android:layout_toEndOf="@+id/ivUlkeBayrak"
        android:text="Tel Kodu" />

    <TextView
        android:id="@+id/tvTelKodu"
        android:layout_width="125dp"
        android:layout_height="18dp"
        android:layout_below="@+id/tvKurulus"
        android:layout_alignStart="@+id/tvBaskent"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="77dp"
        android:text="TextView" />
</RelativeLayout>

activity_main.xml için

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activity.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />
</RelativeLayout>

Ulke.java modeli üretiniz;

package com.serifgungor.recyclerview_searchview.Model;

public class Ulke {
    private String ulkeAdi;
    private String ulkeResimUrl;
    private String telefonKodu;
    private String ulkeBaskent;
    private int kurulusYili;

    public Ulke() {
    }

    public Ulke(String ulkeAdi, String ulkeResimUrl, String telefonKodu, String ulkeBaskent, int kurulusYili) {
        this.ulkeAdi = ulkeAdi;
        this.ulkeResimUrl = ulkeResimUrl;
        this.telefonKodu = telefonKodu;
        this.ulkeBaskent = ulkeBaskent;
        this.kurulusYili = kurulusYili;
    }

    public String getUlkeAdi() {
        return ulkeAdi;
    }

    public void setUlkeAdi(String ulkeAdi) {
        this.ulkeAdi = ulkeAdi;
    }

    public String getUlkeResimUrl() {
        return ulkeResimUrl;
    }

    public void setUlkeResimUrl(String ulkeResimUrl) {
        this.ulkeResimUrl = ulkeResimUrl;
    }

    public String getTelefonKodu() {
        return telefonKodu;
    }

    public void setTelefonKodu(String telefonKodu) {
        this.telefonKodu = telefonKodu;
    }

    public String getUlkeBaskent() {
        return ulkeBaskent;
    }

    public void setUlkeBaskent(String ulkeBaskent) {
        this.ulkeBaskent = ulkeBaskent;
    }

    public int getKurulusYili() {
        return kurulusYili;
    }

    public void setKurulusYili(int kurulusYili) {
        this.kurulusYili = kurulusYili;
    }
}

UlkeHolder.java üretiniz ;

package com.serifgungor.recyclerview_searchview.Holder;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.serifgungor.recyclerview_searchview.R;

public class UlkeHolder extends RecyclerView.ViewHolder {

    public ImageView ivResim;
    public TextView tvUlkeAdi,tvUlkeKodu,tvUlkeBaskent,tvUlkeKurulus;

    public UlkeHolder(View itemView) {
        super(itemView);

        tvUlkeAdi = itemView.findViewById(R.id.tvUlkeAdi);
        tvUlkeKodu = itemView.findViewById(R.id.tvTelKodu);
        tvUlkeBaskent = itemView.findViewById(R.id.tvBaskent);
        tvUlkeKurulus = itemView.findViewById(R.id.tvKurulus);
        ivResim = itemView.findViewById(R.id.ivUlkeBayrak);
    }

}

UlkeAdapter.java üretiniz;

package com.serifgungor.recyclerview_searchview.Adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;

import com.serifgungor.recyclerview_searchview.Holder.UlkeHolder;
import com.serifgungor.recyclerview_searchview.Model.Ulke;
import com.serifgungor.recyclerview_searchview.R;

import java.util.ArrayList;
import java.util.List;

public class UlkeAdapter extends RecyclerView.Adapter<UlkeHolder> implements Filterable {

    private List<Ulke> items;
    private List<Ulke> itemsFiltered;
    private Context context;

    public UlkeAdapter(){}

    public UlkeAdapter(List<Ulke> ulkeler,Context context){
        this.context = context;
        this.items = ulkeler;
        this.itemsFiltered = ulkeler;
    }


    @NonNull
    @Override
    public UlkeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.satir_goruntusu,null);

        return new UlkeHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull UlkeHolder holder, int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        int resId = holder
                .itemView
                .getResources()
                .getIdentifier(
                        itemsFiltered.get(position).getUlkeResimUrl(),
                        "drawable",
                        context.getPackageName()
                );
        holder.ivResim.setImageResource(resId);
        holder.tvUlkeKurulus.setText(""+itemsFiltered.get(position).getKurulusYili());
        holder.tvUlkeBaskent.setText(itemsFiltered.get(position).getUlkeBaskent());
        holder.tvUlkeKodu.setText(itemsFiltered.get(position).getTelefonKodu());
        holder.tvUlkeAdi.setText(itemsFiltered.get(position).getUlkeAdi());
    }

    @Override
    public int getItemCount() {
        return itemsFiltered.size();
    }

    @Override
    public Filter getFilter() {

        /*
        Var olan bir list içerisinde anlık değer aramak için şöyle bir yol izledik.
        Var olan listeyi kopyala, SearchView içerisinde anlık arama esnasında, Aynı tipte üretilen
        liste içerisine aranan kelime hangi elemanda bulunuyorsa,
        kopya isminde üretilen List içerisine aranan kelimeyi taşı.
         */
        
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                String query = charSequence.toString();

                List<Ulke> filtered = new ArrayList<>();

                if (query.isEmpty()) {
                    filtered = items;
                } else {
                    for (Ulke u : items) {
                        if (u.getUlkeAdi().toLowerCase().contains(query.toLowerCase())) {
                            filtered.add(u);
                        }
                    }
                }

                FilterResults results = new FilterResults();
                results.count = filtered.size();
                results.values = filtered;
                return results;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                itemsFiltered = (ArrayList<Ulke>) filterResults.values;
                notifyDataSetChanged();
            }
        };
    }
}

MainActivity.java için kodlar;

package com.serifgungor.recyclerview_searchview.Activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.serifgungor.recyclerview_searchview.Adapter.UlkeAdapter;
import com.serifgungor.recyclerview_searchview.Model.Ulke;
import com.serifgungor.recyclerview_searchview.R;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener{

    SearchView searchView;
    RecyclerView recyclerView;
    UlkeAdapter adapter;
    List<Ulke> ulkeler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recyclerView);
        ulkeler = new ArrayList<>();
        //String ulkeAdi, String ulkeResimUrl, String telefonKodu, String ulkeBaskent, int kurulusYili
        ulkeler.add(new Ulke("Spain","flag_spain","+34","Madrid",1469));
        ulkeler.add(new Ulke("Greece","flag_greece","+30","Atina",1821));
        ulkeler.add(new Ulke("Türkiye","flag_turkey","+90","Ankara",1923));
        ulkeler.add(new Ulke("Egypt","flag_egypt","+20","Kahire",3150));
        ulkeler.add(new Ulke("Algeria","flag_algeria","+213","Cezayir",1515));
        ulkeler.add(new Ulke("Norway","flag_norway","+47","Oslo",872));
        ulkeler.add(new Ulke("Liberia","flag_liberia","+231","Monrovia",1822));
        ulkeler.add(new Ulke("South Korea","flag_south_korea","+82","Seul",1948));

        adapter = new UlkeAdapter(ulkeler,getApplicationContext());
        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        recyclerView.setAdapter(adapter);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main,menu);

        MenuItem menuItem = menu.findItem(R.id.app_bar_search);
        searchView = (SearchView) menuItem.getActionView();
        searchView.setOnQueryTextListener(this);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        //Toast.makeText(getApplicationContext(),query,Toast.LENGTH_LONG).show();
        adapter.getFilter().filter(query);
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        adapter.getFilter().filter(newText);
        return false;
    }
}

iyi çalışmalar diliyorum, yararlı olması dileğimle.

Bu makaleyi paylaşın:

3 Comments

  1. Cihan Erdogan2019-07-12 15:25:33

    Tesekkurler cok isime yaradi :)

Bir yorum bırakın