Mobil qurilmalar fanidan yakuniy savollariga javoblar


Androidda ViewHolder ga namuna keltiring? Nima uchun biz undan foydalanishimiz kerak?



Yüklə 194,29 Kb.
səhifə51/62
tarix01.12.2023
ölçüsü194,29 Kb.
#170899
1   ...   47   48   49   50   51   52   53   54   ...   62
Mobil qurilmalar fanidan yakuniy savollariga javoblar-fayllar.org

86. Androidda ViewHolder ga namuna keltiring? Nima uchun biz undan foydalanishimiz kerak?
"ViewHolder" Android-dagi namunalar orqali optimallashtirish uchun foydalaniladi. U o'z ichiga olgan view'larni saqlab qoladi va ularni qayta ishlatish imkonini beradi, shuningdek, listni skroll qilishda animatsiyalarni kamaytiradi. Biz uni "RecyclerView"da foydalanishimiz kerak bo'lgan holda ishlatamiz, chunki u ularning komponentlarini saqlab qolish va tiklashni tezroq amalga oshiradi.
ViewHolder element ko'rinishini va uning RecyclerView ichidagi o'rni haqidagi metama'lumotlarni tavsiflaydi.


xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/rvAnimals"
android:layout_width="match_parent"
android:layout_height="match_parent"/>


xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
android:id="@+id/tvAnimalName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>

public class MyRecyclerViewAdapter extends RecyclerView.Adapter {


private List mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
// data is passed into the constructor
MyRecyclerViewAdapter(Context context, List data) {
this.mInflater = LayoutInflater.from(context);
this.mData = data;
}
// inflates the row layout from xml when needed
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.recyclerview_row, parent, false);
return new ViewHolder(view);
}
// binds the data to the TextView in each row
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String animal = mData.get(position);
holder.myTextView.setText(animal);
}
// total number of rows
@Override
public int getItemCount() {
return mData.size();
}
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView myTextView;
ViewHolder(View itemView) {
super(itemView);
myTextView = itemView.findViewById(R.id.tvAnimalName);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
// convenience method for getting data at click position
String getItem(int id) {
return mData.get(id);
}
// allows clicks events to be caught
void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}

public class MainActivity extends AppCompatActivity implements MyRecyclerViewAdapter.ItemClickListener {

MyRecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// data to populate the RecyclerView with
ArrayList animalNames = new ArrayList<>();
animalNames.add("Horse");
animalNames.add("Cow");
animalNames.add("Camel");
animalNames.add("Sheep");
animalNames.add("Goat");
// set up the RecyclerView
RecyclerView recyclerView = findViewById(R.id.rvAnimals);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new MyRecyclerViewAdapter(this, animalNames);
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
}
@Override
public void onItemClick(View view, int position) {
Toast.makeText(this, "You clicked " + adapter.getItem(position) + " on row number " + position, Toast.LENGTH_SHORT).show();
}


https://stackoverflow.com/questions/40584424/simple-android-recyclerview-example



Yüklə 194,29 Kb.

Dostları ilə paylaş:
1   ...   47   48   49   50   51   52   53   54   ...   62




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin