写一个简单的RecyclerView用法
private Object[][] data = new Object[][]{
{"绘制三角形", TriangleActivity.class.getName()}
};
RecyclerView recyclerView = findViewById(R.id.recycler_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.addItemDecoration(new DividerItemDecoration(MainActivity.this, LinearLayout.VERTICAL));
recyclerView.setAdapter(new CustomAdapter(MainActivity.this, data));
private class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private Context mContext;
private Object[][] data;
public CustomAdapter(Context context, Object[][] data) {
this.mContext = context;
this.data = data;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
private View itemView;
public ViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(android.R.id.text1);
this.itemView = itemView;
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(mContext).inflate(android.R.layout.simple_list_item_1, null));
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.textView.setText(data[position][0].toString());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String clzName = data[holder.getAdapterPosition()][1].toString();
try {
Class clz = Class.forName(clzName);
mContext.startActivity(new Intent(mContext, clz));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
}
@Override
public int getItemCount() {
return data.length;
}
}