Bu dersimizde recyclerview uygulamamıza devam ediyoruz. Bu derste her satırda bulunan eleman kopyala ve eleman sil butonlarına işlev ekleyeceğiz.
Bu işlemler için veri kaynağından verileri alıp viewa yani recyclerview’a yerleştiren Adapter sınıfında yapılacak.
Listeye yeni eleman eklemek veya eleman silmek için veri kaynağımıza basit bir şekilde add/remove diyebiliriz. Bu methodlara position değerini de parametre olarak verdiğimizde işlem gerçekleşir. Ama listemizin de anlık olarak güncellenmesini istiyorsak notify methodlarını kullanmamız gerekir. Eleman ekleme ve silme için ayrı ayrı notify methodlarını kullanabiliriz.
Burda unutmamız gereken durum, eleman ekle ve silme işlemlerinde listedeki elemanların index değerleri değiştiği için onu da ele almamız gerekir. Aksi taktirde listemiz düzgün çalışmaz. Bu durumu ele almak için notifyItemRangeChanged methodunu kullanırız.
Tüm değişimleri notifyDataSetChanged methoduyla da kontrol edebiliriz. Ama bu method çok kaynak tüketen ve ekleme/silme animasyonlarını de göremeyeceğiniz bir methoddur, o yüzden pek tavsiye etmiyorum.
Adapter sınıfının son halinin kodları aşağıdadır. Bir önceki derste yazılan kodlarını da aşağıda bulabilirsiniz. Uygulamanın bitmiş halini ve kullanılan resimleri indirmek için LÜTFEN BURAYI tıklayınız…..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
public class EmreAdapter extends RecyclerView.Adapter<EmreAdapter.MyViewHolder>{ ArrayList<Manzara> mDataList; LayoutInflater inflater; public EmreAdapter(Context context, ArrayList<Manzara> data){ //inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater=LayoutInflater.from(context); this.mDataList=data; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v=inflater.inflate(R.layout.list_item, parent,false); MyViewHolder holder=new MyViewHolder(v); return holder; } @Override public void onBindViewHolder(MyViewHolder holder, int position) { Manzara tiklanilanManzara=mDataList.get(position); holder.setData(tiklanilanManzara, position); } public void deleteItem(int position){ mDataList.remove(position); notifyItemRemoved(position); notifyItemRangeChanged(position,mDataList.size()); //Aşağıdaki method yukarıdaki iki satır kodun yaptığı işi yapar, //Ama çok kaynak tüketilir. ve de animasyonları göremeyiz. //notifyDataSetChanged(); } public void addItem(int position, Manzara kopyalanacakManzara){ mDataList.add(position, kopyalanacakManzara); notifyItemInserted(position); notifyItemRangeChanged(position,mDataList.size()); //Aşağıdaki method yukarıdaki iki satır kodun yaptığı işi yapar, //Ama çok kaynak tüketilir. ve de animasyonları göremeyiz. //notifyDataSetChanged(); } @Override public int getItemCount() { return mDataList.size(); } class MyViewHolder extends RecyclerView.ViewHolder{ TextView mManzaraBaslik, mManzaraAciklama; ImageView mManzaraResmi, mSilResmi, mKopyalaResmi; int tiklanilanOgeninPositionDegeri=0; Manzara kopyalanacakManzara; public MyViewHolder(View itemView) { super(itemView); mManzaraBaslik= (TextView) itemView.findViewById(R.id.tvManzaraBaslik); mManzaraAciklama= (TextView) itemView.findViewById(R.id.tvManzaraTanim); mManzaraResmi= (ImageView) itemView.findViewById(R.id.imgManzara); mSilResmi=(ImageView) itemView.findViewById(R.id.imgSil); mKopyalaResmi=(ImageView) itemView.findViewById(R.id.imgKopyala); mSilResmi.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("EMRE", "Silmeden önce position:"+tiklanilanOgeninPositionDegeri+" ElemanSayısı "+mDataList.size()); deleteItem(tiklanilanOgeninPositionDegeri); Log.d("EMRE", "Silmeden sonra position:"+tiklanilanOgeninPositionDegeri+" ElemanSayısı "+mDataList.size()); } }); mKopyalaResmi.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("EMRE", "Eklenmeden önce position: "+tiklanilanOgeninPositionDegeri+" ElemanSayısı "+mDataList.size()); addItem(tiklanilanOgeninPositionDegeri, kopyalanacakManzara); Log.d("EMRE", "Eklenmeden sonra position: "+tiklanilanOgeninPositionDegeri+" ElemanSayısı "+mDataList.size()); } }); } public void setData(Manzara tiklanilanManzara, int position) { this.mManzaraBaslik.setText(tiklanilanManzara.getBaslik()); this.mManzaraAciklama.setText(tiklanilanManzara.getTanim()); this.mManzaraResmi.setImageResource(tiklanilanManzara.getImageID()); this.tiklanilanOgeninPositionDegeri=position; this.kopyalanacakManzara=tiklanilanManzara; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
public class MainActivity extends AppCompatActivity { RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView= (RecyclerView) findViewById(R.id.recyclerview); EmreAdapter emreAdapter=new EmreAdapter(this, Manzara.getData()); recyclerView.setAdapter(emreAdapter); LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this); linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(linearLayoutManager); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.layout_menu, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { int id=item.getItemId(); switch (id){ case R.id.linearViewHorizontal: LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this); linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); recyclerView.setLayoutManager(linearLayoutManager); break; case R.id.linearViewVertical: LinearLayoutManager linearLayoutManagerVertical=new LinearLayoutManager(this); linearLayoutManagerVertical.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(linearLayoutManagerVertical); break; case R.id.gridView: GridLayoutManager mGridLayoutManager = new GridLayoutManager(this, 3); // (Context context, int spanCount) recyclerView.setLayoutManager(mGridLayoutManager); break; case R.id.staggeredViewHorizontal: StaggeredGridLayoutManager mStaggeredHorizontalLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.HORIZONTAL); // (int spanCount, int orientation) recyclerView.setLayoutManager(mStaggeredHorizontalLayoutManager); break; case R.id.staggeredViewVertical: StaggeredGridLayoutManager mStaggeredVerticalLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); // (int spanCount, int orientation) recyclerView.setLayoutManager(mStaggeredVerticalLayoutManager); break; } return super.onOptionsItemSelected(item); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/linearViewHorizontal" android:title="Linear View Horizontal" app:showAsAction="never"></item> <item android:id="@+id/linearViewVertical" android:title="Linear View Vertical" app:showAsAction="never"></item> <item android:id="@+id/gridView" android:title="Grid View" app:showAsAction="never"></item> <item android:id="@+id/staggeredViewHorizontal" android:title="Staggered View Horizontal" app:showAsAction="never"></item> <item android:id="@+id/staggeredViewVertical" android:title="Staggered View Vertical" app:showAsAction="never"></item> </menu> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
import java.util.ArrayList; public class Manzara { private int imageID; private String baslik; private String tanim; public int getImageID() { return imageID; } public String getBaslik() { return baslik; } public String getTanim() { return tanim; } public void setImageID(int imageID) { this.imageID = imageID; } public void setBaslik(String baslik) { this.baslik = baslik; } public void setTanim(String tanim) { this.tanim = tanim; } public static ArrayList<Manzara> getData(){ ArrayList<Manzara> dataList=new ArrayList<Manzara>(); int[] resimler= { R.drawable.thumb_1_0, R.drawable.thumb_1_1, R.drawable.thumb_1_2, R.drawable.thumb_1_3, R.drawable.thumb_1_4, R.drawable.thumb_1_5, R.drawable.thumb_1_6, R.drawable.thumb_1_7, R.drawable.thumb_1_8, R.drawable.thumb_1_9, R.drawable.thumb_2_0, R.drawable.thumb_2_1, R.drawable.thumb_2_2, R.drawable.thumb_2_3, R.drawable.thumb_2_4, R.drawable.thumb_2_5, R.drawable.thumb_2_6, R.drawable.thumb_2_7, R.drawable.thumb_2_8, R.drawable.thumb_2_9, R.drawable.thumb_3_0, R.drawable.thumb_3_1, R.drawable.thumb_3_2, R.drawable.thumb_3_3, R.drawable.thumb_3_4, R.drawable.thumb_3_5, R.drawable.thumb_3_6, R.drawable.thumb_3_7, R.drawable.thumb_3_8, R.drawable.thumb_3_9, R.drawable.thumb_4_0, R.drawable.thumb_4_1, R.drawable.thumb_4_2, R.drawable.thumb_4_3, R.drawable.thumb_4_4, R.drawable.thumb_4_5, R.drawable.thumb_4_6, R.drawable.thumb_4_7, R.drawable.thumb_4_8, R.drawable.thumb_4_9, R.drawable.thumb_5_0, R.drawable.thumb_5_1, R.drawable.thumb_5_2, R.drawable.thumb_5_3, R.drawable.thumb_5_4, R.drawable.thumb_5_5, R.drawable.thumb_5_6, R.drawable.thumb_5_7, R.drawable.thumb_5_8, R.drawable.thumb_5_9, R.drawable.thumb_6_0, R.drawable.thumb_6_1, R.drawable.thumb_6_2, R.drawable.thumb_6_3, R.drawable.thumb_6_4, R.drawable.thumb_6_5, R.drawable.thumb_6_6, R.drawable.thumb_6_7, R.drawable.thumb_6_8, R.drawable.thumb_6_9, R.drawable.thumb_7_0, R.drawable.thumb_7_1, R.drawable.thumb_7_2, R.drawable.thumb_7_3, R.drawable.thumb_7_4 }; for(int i=0; i<resimler.length; i++){ Manzara gecici=new Manzara(); gecici.setImageID(resimler[i]); gecici.setBaslik("Manzara "+i); gecici.setTanim("Tanım bilgisi "+i); dataList.add(gecici); } return dataList; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.emrealtunbilek.materialrecyclerviewornek.MainActivity"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recyclerview" > </android.support.v7.widget.RecyclerView> </RelativeLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView 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:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" app:cardCornerRadius="5dp" android:id="@+id/rootCardView" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white" android:orientation="horizontal" > <ImageView android:layout_width="60dp" android:layout_height="60dp" android:id="@+id/imgManzara" android:src="@drawable/thumb_1_0" /> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:orientation="vertical" android:layout_weight="1" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Manzara" android:textSize="18sp" android:id="@+id/tvManzaraBaslik" android:textStyle="bold" android:layout_margin="7dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Manzara Tanım" android:textSize="14sp" android:id="@+id/tvManzaraTanim" android:textStyle="italic" android:layout_marginLeft="7dp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:layout_width="20dp" android:layout_height="20dp" android:layout_margin="7dp" android:id="@+id/imgSil" android:tint="@color/colorAccent" android:src="@drawable/ic_delete"/> <ImageView android:layout_width="20dp" android:layout_height="20dp" android:layout_margin="7dp" android:id="@+id/imgKopyala" android:tint="@color/colorAccent" android:src="@drawable/ic_make_copy"/> </LinearLayout> </LinearLayout> </android.support.v7.widget.CardView> |
Merhaba hocam peki sadece seçtiğimiz satırı ayrı bir fragmant yad actionda açtırıp düzenleyip tekrar geri geldiğimizde sadece o satırı yenilemenin yöntemi nedir.