public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List<Map<String,Object>> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView =(RecyclerView)this.findViewById(R.id.recycler);
//2初始化数据
data= new ArrayList<>();
Map<String,Object> map1 = new HashMap<>();
map1.put("pic",R.drawable.dz);
map1.put("name","斗地主斗地主斗地主斗地主斗地主斗地主斗地主斗地主斗地主斗地主斗地主斗地主斗地主");
data.add(map1);
Map<String,Object> map2 = new HashMap<>();
map2.put("pic",R.drawable.jd);
map2.put("name","京东京东京东京东京东京东京东京东京东京东京东京东京东京东京东京东京东京东京东京东京东京东京东京东京东京东京东京东");
data.add(map2);
Map<String,Object> map3 = new HashMap<>();
map3.put("pic",R.drawable.tm);
map3.put("name","天猫天猫天猫天猫天猫天猫天猫天猫天猫天猫天猫天猫天猫天猫天猫天猫天猫天猫天猫天猫天猫天猫");
data.add(map3);
Map<String,Object> map4 = new HashMap<>();
map4.put("pic",R.drawable.dz);
map4.put("name","斗地主斗地主斗地主斗地主斗地主斗地主斗地主斗地主斗地主斗地主斗地主斗地主斗地主");
data.add(map4);
RecyclerView.LayoutManager manager1 = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
RecyclerView.LayoutManager manager2 = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,true);
RecyclerView.LayoutManager manager3 = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
RecyclerView.LayoutManager manager4 = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,true);
RecyclerView.LayoutManager manager5 = new GridLayoutManager(this,2);
RecyclerView.LayoutManager manager6 = new StaggeredGridLayoutManager(2,RecyclerView.VERTICAL);
recyclerView.setLayoutManager(manager6);
recyclerView.setAdapter(new HomeAdapter());
}
class HomeAdapter extends RecyclerView.Adapter{
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyViewHolder holder = new MyViewHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.item,null));
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
TextView tv=(TextView)holder.itemView.findViewById(R.id.tv);
ImageView iv=(ImageView)holder.itemView.findViewById(R.id.iv);
tv.setText(data.get(position).get("name").toString());
iv.setImageResource((int)data.get(position).get("pic"));
}
@Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView tv;
ImageView iv;
public MyViewHolder(View itemView) {
super(itemView);
}
}
}
}
网友评论