|
| 1 | +package com.jayfeng.lesscode.app.activity.adapterless; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.support.v7.widget.LinearLayoutManager; |
| 6 | +import android.support.v7.widget.RecyclerView; |
| 7 | +import android.widget.TextView; |
| 8 | + |
| 9 | +import com.jayfeng.lesscode.app.R; |
| 10 | +import com.jayfeng.lesscode.app.model.Person; |
| 11 | +import com.jayfeng.lesscode.core.AdapterLess; |
| 12 | +import com.jayfeng.lesscode.core.ViewLess; |
| 13 | + |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +public class RecyclerAdapterActivity extends Activity { |
| 18 | + |
| 19 | + List<Person> list; |
| 20 | + RecyclerView recyclerView; |
| 21 | + private RecyclerView.LayoutManager layoutManager; |
| 22 | + RecyclerView.Adapter<AdapterLess.RecyclerViewHolder> adapter; |
| 23 | + |
| 24 | + @Override |
| 25 | + protected void onCreate(Bundle savedInstanceState) { |
| 26 | + super.onCreate(savedInstanceState); |
| 27 | + setContentView(R.layout.activity_adapter_recycler); |
| 28 | + recyclerView = ViewLess.$(this, R.id.recycler); |
| 29 | + |
| 30 | + layoutManager = new LinearLayoutManager(this); |
| 31 | + recyclerView.setLayoutManager(layoutManager); |
| 32 | + |
| 33 | + initData(); |
| 34 | + |
| 35 | + adapter = AdapterLess.$recycler(this, list, |
| 36 | + R.layout.activity_main_list_item, |
| 37 | + new AdapterLess.RecyclerCallBack<Person>() { |
| 38 | + |
| 39 | + @Override |
| 40 | + public void onBindViewHolder(int position, AdapterLess.RecyclerViewHolder recyclerViewHolder, Person person) { |
| 41 | + TextView nameView = recyclerViewHolder.$view(R.id.name); |
| 42 | + nameView.setText(person.getName()); |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + recyclerView.setAdapter(adapter); |
| 47 | + } |
| 48 | + |
| 49 | + private void initData() { |
| 50 | + list = new ArrayList<>(); |
| 51 | + list.add(new Person("header")); |
| 52 | + list.add(new Person("jay")); |
| 53 | + list.add(new Person("bee")); |
| 54 | + list.add(new Person("header")); |
| 55 | + list.add(new Person("chras")); |
| 56 | + list.add(new Person("nichid")); |
| 57 | + } |
| 58 | + |
| 59 | +} |
0 commit comments