بابهت: فێركاری چۆنیهتی بهكارهێنانی RecyclerView له ئهندرۆید
لهم فێركاریهدا ههوڵ ئهدهین چۆنیهتی بهكارهێنانی RecyclerView ڕوون بكهینهوه كه یهكێكه لهو View انهیی زۆر بهكاردێت بۆ پیشاندانی زانیاری بهشێوهی لیست لهگهڵ بهكارهێنانی custom layout بهئاسانترین شێوه، ههروهها فێرئهبین چۆن adapter بنوسین، وه كۆنتڕۆڵی خانهكان بكهین كه كلیكی لهسهر ئهكرێت، له كۆتایی فێركاریهكه وێنهیهكی وهك ئهوهی خوارهوه دروست دهكهین

با دهست پێبكهین:
سهرهتا Android studio بكهوه و پرۆژهیهكی نوێ دروست بكه بهم شێوهیه File ⇒ New Project دواتر بهم شێوهیه پڕی بكهوه



بۆ ئهوهی بتوانین RecyclerView بهكاربهێنین پێویسته بچینه بهشی module یی پرۆژهكهت و ئهم دوو dependency زیاد بكهین
implementation 'com.android.support:cardview-v7:27.0.2'
implementation 'com.android.support:recyclerview-v7:27.0.2'
دڵنیابه بهم شێوهیه و دواتر synce بكه
ئێستا بچۆره سهرپهڕگهی activity_main.xml
لهم پهڕگهدا پێویسته RecyclerView زیاد بكهین بهم شێوهیه
<?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"
tools:context="kdev.kurdishnews.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</LinearLayout>ئێستا بچۆره سهر فایلی colors.xml بهم شێوهی لێكه
<resources>
<color name="colorPrimary">#8643ea</color>
<color name="colorPrimaryDark">#673aa6</color>
<color name="colorAccent">#8565bc</color>
</resources>زۆرباشه ئێستا فایلێك دروست ئهكهین تایبهت RecycleView
لهم فایلهدا دیزاینی یهك خانه دهكهین، كلیك ڕاست لهسهر فۆڵدهری Layout بكه بهم شێوهیه


فایلهكه بكهوه و بهم شێوهیهیی لێكه
<?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="200dp"
android:layout_margin="10dp"
app:cardUseCompatPadding="true"
app:cardElevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/image_view"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="5dp"
android:background="#fcfcfc"
android:scaleType="centerCrop" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="News Title"
android:textColor="@color/colorPrimaryDark"
android:textSize="21sp" />
<TextView
android:id="@+id/short_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:ellipsize="end"
android:maxLines="3"
android:text="Detail"
android:textColor="@android:color/black"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>ڕوونكردنهوه: سهرهتا Component یی CardView بهكارهێنراوه بۆ ڤیوی سهرهكی، وه ئهو attrubite كه گرنگن له خوارهوه ڕوونكراوهتهوه
١- orientation: بهكاردێت بۆ ڕیزكردنی view كان بهدوو شێواز، Horizontal واته ئاسۆیی ، Vertical ستونی
٢- scaleType: بهكاردێت له ImageView كهكاری ڕێكخستنی وێنهی ناو ڤیوهكهیه به چهند شێوازێك وهك fitCenter, fitXY ....
٣- layout_margin: بهكاردێت بۆ پێدانی بۆشایی لهچوارچێوهی View، وه ٤ جۆر ههیه
layout_marginStart: پێدانی بۆشایی بۆ لای چهپی View بهپێی شێوازی LTR وهبه پێچهوانهوه بۆ RTL
layout_marginEnd: پێدانی بۆشایی بۆ لای ڕاستی View بهپێی شێوازی LTR وهبه پێچهوانهوه بۆ RTL
layout_marginTop: پێدانی بۆشایی بۆ لای سهرهوهی View
layout_marginBottom: پێدانی بۆشایی بۆ لای خوارهوهی View
٤- textSize: بۆ دیاریكردنی قهبارهی نوسین به sp
٥- maxLines: دیاریكردنی زۆرترین دێڕ كه دهربكهوێ
٦- ellipsize: بهكاردێت بۆ پێدانی شێوازێكی جوان به نوسینهكانت كاتێك نوسینهكه زۆرتر دهبێت لهسكرینی شاشه بهمهش ئهتوانین له كۆتایی نوسینهكه چهند نوكتهیهك دهربكهوێ وه گرنگترین جۆریان ئهمانهن
start: دانانی چهند نوكتهیهك لهسهرهتای نوسینهكه.
end: دانانی چهند نوكتهیهك لهكۆتایی نوسینهكه.
٧- layout_weight: یهكێكه له گرنگترین attr كهبهكاردێت بۆ پێدانی قهبارهی View لهگهڵ layout_width و layout_height. بههۆی layout_weight دهتوانین قهبارهی View بهڕێژهی دابنێین ئهمهش كاتێك دهبێت یهكێك له layout_height یان layout_width به 0dp پێناسه كرابێت.
ئێستا كاتی ئهوهیه بچینه سهر كۆدهكانی جاڤا، ههموو ئهو ههنگاوانهی خوارهوه ئهنجام بدهوه


كڵاسی NewsModel بكهو ئهمانهی تیا بنوسه، كه ئهم كلاسه بۆ دروست كردنی مۆدیلێك له داتا كه پێك دێت له id, resourceId, title, detail
package kdev.kurdishnews;
/**
* Created by reba on 2/27/18.
*/
public class NewsModel {
private int id;
private int resourceId;
private String title;
private String shortDetail;
public NewsModel(int id,int resourceId, String title, String shortDetail){
this.id = id;
this.resourceId = resourceId;
this.title = title;
this.shortDetail = shortDetail;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getShortDetail() {
return shortDetail;
}
public void setShortDetail(String shortTitle) {
this.shortDetail = shortTitle;
}
public int getResourceId(){
return resourceId;
}
public void setResourceId(int resourceId) {
this.resourceId = resourceId;
}
}دواتر كڵاسێكی تر دروست بكه بهناوی NewsData كه ههندێ داتای ئامادهكراوی تێدایه
ئهم كڵاسه پێك هاتوه له یهك method كه لیستێك له داتا ئهگهڕێنێتهوه له جۆری NewsModel
package kdev.kurdishnews;
import java.util.ArrayList;
/**
* Created by reba on 2/27/18.
*/
public class NewsData {
NewsData(){}
public ArrayList<NewsModel> getNewsData(){
ArrayList<NewsModel> models = new ArrayList<>();
models.add(new NewsModel(1,R.drawable.i1,"Apple is creating medical clinics to offer employees healthcare and test products","Apple is preparing to open medical clinics to provide healthcare for its employees and their families in Cupertino this spring, according to a new report by CNBC. The tech company has launched a website for the clinics, which will operate under the name AC Wellness. "));
models.add(new NewsModel(2,R.drawable.i3,"Sony’s new A7 III is a $2,000 full-frame mirrorless camera that should terrify Canon and Nikon","Sony spent 2017 releasing pricey powerhouse cameras like the A9, but today the company announced one that sounds almost as good at half the price. The new full-frame A7 III splits the difference between the pixel-packed A7R III and the A7S II, the company’s low light and video king. It boasts big-time speed and just about everything else you could ask for, all for $1,999 (body only) when it ships in April."));
models.add(new NewsModel(3,R.drawable.i6, "EU says 'massacre' in Syria's Eastern Ghouta 'must stop now'","EU foreign policy chief Federica Mogherini called for an immediate ceasefire and aid deliveries in the rebel-held Syrian enclave of Eastern Ghouta after more than 400 civilians were killed there."));
models.add(new NewsModel(3,R.drawable.i2,"The Galaxy S9’s dual-aperture camera is great marketing","Samsung’s Galaxy S9 launch event here at Mobile World Congress 2018 was a weird one. The company went through familiar motions, but the usual buzz and excitement for its new flagship phone weren’t really there."));
models.add(new NewsModel(5,R.drawable.i4, "Turkey condemns Czech release of Syrian Kurd leader","Turkey on Tuesday (February 27) accused the Czech judicial authorities of backing “terror” after the release of prominent Syrian Kurdish leader Salih Muslim, warning that ties between Ankara and Prague would be harmed."));
models.add(new NewsModel(6,R.drawable.i5, "Iraqi dinar exchange rates (February 27)","The U.S. dollar to Iraqi dinar (IQD) exchange rate remained down in the Kurdistan Region’s currency markets on Tuesday (February 27)."));
models.add(new NewsModel(7,R.drawable.i7, "Iraq extends air blockade of Kurdistan by three months: airport official","Iraq has extended by three months a ban on international flights to the Kurdistan Region, a senior official at Erbil airport in the capital of Kurdistan said on Monday (February 26)."));
models.add(new NewsModel(8,R.drawable.i2,"The Galaxy S9’s dual-aperture camera is great marketing","Samsung’s Galaxy S9 launch event here at Mobile World Congress 2018 was a weird one. The company went through familiar motions, but the usual buzz and excitement for its new flagship phone weren’t really there."));
models.add(new NewsModel(9,R.drawable.i3,"Sony’s new A7 III is a $2,000 full-frame mirrorless camera that should terrify Canon and Nikon","Sony spent 2017 releasing pricey powerhouse cameras like the A9, but today the company announced one that sounds almost as good at half the price. The new full-frame A7 III splits the difference between the pixel-packed A7R III and the A7S II, the company’s low light and video king. It boasts big-time speed and just about everything else you could ask for, all for $1,999 (body only) when it ships in April."));
models.add(new NewsModel(10,R.drawable.i4, "Turkey condemns Czech release of Syrian Kurd leader","Turkey on Tuesday (February 27) accused the Czech judicial authorities of backing “terror” after the release of prominent Syrian Kurdish leader Salih Muslim, warning that ties between Ankara and Prague would be harmed."));
models.add(new NewsModel(11,R.drawable.i5, "Iraqi dinar exchange rates (February 27)","The U.S. dollar to Iraqi dinar (IQD) exchange rate remained down in the Kurdistan Region’s currency markets on Tuesday (February 27)."));
models.add(new NewsModel(12,R.drawable.i6, "EU says 'massacre' in Syria's Eastern Ghouta 'must stop now'","EU foreign policy chief Federica Mogherini called for an immediate ceasefire and aid deliveries in the rebel-held Syrian enclave of Eastern Ghouta after more than 400 civilians were killed there."));
models.add(new NewsModel(13,R.drawable.i4, "Turkey condemns Czech release of Syrian Kurd leader","Turkey on Tuesday (February 27) accused the Czech judicial authorities of backing “terror” after the release of prominent Syrian Kurdish leader Salih Muslim, warning that ties between Ankara and Prague would be harmed."));
models.add(new NewsModel(14,R.drawable.i5, "Iraqi dinar exchange rates (February 27)","The U.S. dollar to Iraqi dinar (IQD) exchange rate remained down in the Kurdistan Region’s currency markets on Tuesday (February 27)."));
return models;
}
}بۆ ئهوهی بتوانین كۆنترۆڵی ئهوه بكهین كاتێك كلیك كرا function لۆد بێت پێویسته interface دروست بكهین بهم شێوهیه
public interface ClickListener {
public void onItemClicked(int id, String title);
}ئێستا adapter دروست ئهكهین بۆ ئهوهی ئهو داتایهیی ههمانه پیشانی بدهین، سهرهتا كڵاسێك دروست ئهكهین بهناوی NewsAdapter وه extends ئهكهین له جۆری
RecyclerView.Adapter<?>
لێرهدا گهر دیقهت بدهن نیشانهی پرسیار دانراوه له نێوان ئهمه <>
پێویسته نیشانهی ؟ بگۆڕین بهناوی ئهو كڵاسهی كه كۆدهكهمان بۆ ئهبهستێتهوه به دیزاینهكهمان كه ئهمهش كڵاسێكی دیكهیه كه لهناو ههمان كڵاسدا پێناسهی ئهكهین و extends یی RecyclerView.ViewHolder
بۆیه ئێستا كڵاسی NewsAdapter بهم شێوهی لێدێت.
package kdev.kurdishnews;
import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by reba on 2/27/18.
*/
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ItemHolder> {
private Activity activity;
private ArrayList<NewsModel> newsModels;
private ClickListener listener;
public NewsAdapter(Activity activity, ArrayList<NewsModel> newsModels, ClickListener listener){
this.activity = activity;
this.newsModels = newsModels;
this.listener = listener;
}
@Override
public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View customView = LayoutInflater.from(activity).inflate(R.layout.news_cell, null);
return new ItemHolder(customView);
}
@Override
public void onBindViewHolder(ItemHolder holder, int position) {
NewsModel model = newsModels.get(position);
holder.title.setText(model.getTitle());
holder.shortDetail.setText(model.getShortDetail());
holder.image.setImageResource(model.getResourceId());
}
@Override
public int getItemCount() {
return newsModels.size();
}
class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
ImageView image;
TextView title, shortDetail;
public ItemHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.image_view);
title = itemView.findViewById(R.id.title);
shortDetail = itemView.findViewById(R.id.short_detail);
image.setOnClickListener(this);
title.setOnClickListener(this);
shortDetail.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int selectedPosition = getAdapterPosition();
if(selectedPosition == RecyclerView.NO_POSITION){
return;
}
if(listener != null){
NewsModel model = newsModels.get(selectedPosition);
listener.onItemClicked(model.getId(), model.getTitle());
}
}
}
}سهرهتا construct یی كڵاسهكه ٣ گۆڕاو وهردهگرێت
یهكهم: activity بۆ ئهوهی بتوانین فایلی xml لۆد بكهین
دووهم: ArrayList<NewsModel> newsModels كه بریتیه لهو داتایهیی دهمانهوێت پیشانی بدهین
سێیهم: ClickListener كه ئهمهش interface ه بۆ ئهوهی كاتێك كلیك كرا له ئایتمهكان بانگی function بكهین
ئهو method یی كه لهلایهن super class لۆد دهبن:
یهكهم:
@Override
public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View customView = LayoutInflater.from(activity).inflate(R.layout.news_cell, null);
return new ItemHolder(customView);
}ئهم فهنكشنه ههموو جارێك پێویست بكات لۆدی دیزاینهكه دهكات كه دروستمان كردوه به xml و دواتر دهیخاته ناو كڵاسی ItemHolder
دووهم:
@Override
public void onBindViewHolder(ItemHolder holder, int position) {
NewsModel model = newsModels.get(position);
holder.title.setText(model.getTitle());
holder.shortDetail.setText(model.getShortDetail());
holder.image.setImageResource(model.getResourceId());
}ههموو جارێك بانگی ئهم فهنكشنه دهكرێت كه خانهكه پیشان ئهدرێت بهپێی position، بههۆی position زانیاری دیاری كراو دههێنین له ArrayList دواتر نرخهكان پیشان ئهدهین
سێیهم:
@Override
public int getItemCount() {
return newsModels.size();
}ئهم فهنكشنه له سهرهتایی دروست كردنی adapter بانگ دهكرێت و قهبارهی داتاكهمان بهشێوهی int دهگێرینهوه
ناو كڵاسی ItemHolder تهنها دیزاین و كۆد دهبهستینهوه بهیهكهوه ههركات كلیك كرا له ههر ئایتمێك زانیاری دیاری كراو دهگهڕێنینهوه
ئێستا ههموو كارهكان تهواو بوو تهنها ئهوه ماوه ئهم كڵاسه پێناسه بكهین و داخڵی ناو RecyclerView بكهین بهم شێوهیه
package kdev.kurdishnews;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements ClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NewsData data = new NewsData();
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
NewsAdapter adapter = new NewsAdapter(this, data.getNewsData(), this);
recyclerView.setAdapter(adapter);
}
@Override
public void onItemClicked(int id, String title) {
Toast.makeText(getApplicationContext(), "Item ID:"+ id+
"\nTitle:" + title,Toast.LENGTH_LONG).show();
}
}LayoutManager: گرنگه چونكه شێوازی RecyclerView ڕێك دهخات، وهچهند شێوهی تری ههیه وهك GridManager
بههیوای ئهوهی سوودی ههبوو بێت
ههرپرسیارێكتان ههبوو لهخزمهتدام
دهتوانن لێرهوه بهشێوازی Pdf دایبهزێنن
https://goo.gl/KGqdbH