Android開發:Recycler的使用流程總結——基於《第一行程式碼》
阿新 • • 發佈:2018-12-21
- app/build.gradle檔案:
//dependencies閉包中新增
compile 'com.android.support:recyclerview-v7:24.2.1'
- 新增RecyclerView:
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
- 建立子項:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/fruit_image" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/fruit_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp"/> </LinearLayout>
- 建立實體類:
public class Fruit{
//屬性
private String name;
private int imageId;
//建構函式
public Fruit(String name, int imageId){
this.name = name;
this.imageId = imageId;
}
//getter和setter 略
}
- 建立介面卡:
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder>{ private List<Fruit> mFruitList; static class ViewHolder extends RecyclerView.ViewHolder{ View fruitView; ImageView fruitImage; TextView fruitName; public ViewHolder(View view){ super(view); fruitView = view; fruitImage = view.findViewById(R.id.fruit_image); fruitName = view.findViewById(R.id.fruit_name); } } public FruitAdapter(List<Fruit> fruitList){ mFruitList = fruitList; } //用於建立ViewHolder例項 @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){ View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item, parent, false); final ViewHolder holder = new ViewHolder(view); holder.fruitView.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ int position = holder.getAdapterPosition(); Fruit fruit = mFruitList.get(position); Toast.makeText(v.getContext(), "you clicked view " + fruit.getName(), Toast.LENGTH_SHORT).showw(); } }); holder.fruitImage.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ int position = holder.getAdapterPosition(); Fruit fruit = mFruitList.get(position); Toast.makeText(v.getContext(), "you clicked image " + fruit.getName(), Toast.LENGTH_SHORT).showw(); } }) return holder; } //對RecyclerView子項的資料進行賦值的,會在每個子項被滾動到螢幕內的時候執行 @Override public void onBindViewHolder(ViewHolder holder, int position){ Fruit fruit = mFruitList.get(position); holder.fruitImage.setImageResource(fruit.getImageId()); holder.fruitName.setText(fruit.getName()); } @Override public int getItemCount(){ return mFruitList.size(); } }
- MainActivity:
public class MainActivity extends AppCompatAcitivy{ private List<Fruit> fruitList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(sacedInstanceState); setContentView(R.layout.activity_main); initFruits();//初始化fruitList資料。方法略 RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); //LinearLayoutManager是線性佈局的意思,可以實現和ListView類似的效果。 LinearLayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); FruitAdapter adapter = new FruitAdapter(fruitList); recyclerView.setAdatper(adapter); } }