1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.os.Bundle;
public class MainActivity extends AppCompatActivity { private int[] foodIcon = new int[]{ R.mipmap.dangao,R.mipmap.cha,R.mipmap.hanbao,R.mipmap.jitui,R.mipmap.kafei,R.mipmap.kaixinguo,R.mipmap.lanmei, R.mipmap.ningmeng,R.mipmap.niupai,R.mipmap.pijiu,R.mipmap.pisa,R.mipmap.qiyiguo,R.mipmap.regou,R.mipmap.sanmingzhi, R.mipmap.shengnvguo,R.mipmap.shutiao,R.mipmap.tusi,R.mipmap.yimian,R.mipmap.yingtao,R.mipmap.zhangyuxiaowanzi }; private String[] foodName = new String[]{ "蛋糕","茶","汉堡","鸡腿","咖啡","开心果","蓝莓","柠檬","牛排","啤酒","披萨","奇异果","热狗","三明治","圣女果","薯条","吐司","意面","樱桃","章鱼小丸子" }; private RecyclerView recycler; private TextView listTitle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
recycler = findViewById(R.id.recycler); listTitle = findViewById(R.id.listTitle);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2);
LinearLayoutManager layoutManager = new LinearLayoutManager(this,RecyclerView.VERTICAL,false);
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
recycler.setLayoutManager(staggeredGridLayoutManager);
MyAdapter myAdapter = new MyAdapter(foodName,foodIcon,this);
recycler.setAdapter(myAdapter); } }
|