写点什么

【Java 转 Android】58,讲的太清楚了

用户头像
Android架构
关注
发布于: 刚刚

import java.util.Random;


public class MainActivity extends AppCompatActivity {


private DrawerLayout mDrawerLayout;


private Fruit[] fruits = {new Fruit("Apple", R.drawable.apple), new Fruit("Banana", R.drawable.banana),


new Fruit("Orange", R.drawable.orange), new Fruit("Watermelon", R.drawable.watermelon),


new Fruit("Pear", R.drawable.pear), new Fruit("Grape", R.drawable.grape),


new Fruit("Pineapple", R.drawable.pineapple), new Fruit("Strawberry", R.drawable.strawberry),


new Fruit("Cherry", R.drawable.cherry), new Fruit("Mango", R.drawable.mango)};


private List<Fruit> fruitList = new ArrayList<>();


private FruitAdapter adapter;


@Override


protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);


setContentView(R.layout.activity_main);


Toolbar toolbar = findViewById(R.id.toolbar);


setSupportActionBar(toolbar);


mDrawerLayout = findViewById(R.id.drawer_layout);


NavigationView navView = findViewById(R.id.nav_view);


ActionBar actionBar = getSupportActionBar();


if (actionBar != null) {


actionBar.setDisplayHomeAsUpEnabled(true);


actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);


}


navView.setCheckedItem(R.id.nav_call);


navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {


@Override


public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {


mDrawerLayout.closeDrawers();


return true;


}


});


FloatingActionButton fab = findViewById(R.id.fab);


fab.setOnClickListener(new View.OnClickListener() {


@Override


public void onClick(View v) {


Snackbar.make(v,"FAB data deleted",Snackbar.LENGTH_SHORT)


.setAction("Undo", new View.OnClickListener() {


@Override


public void onClick(View v) {


Toast.makeText(MainActivity.this, "FAB Undo", Toast.LENGTH_SHORT).show();


}


}).show();


}


});


initFruits();


RecyclerView recyclerView = findViewById(R.id.recycler_view);


GridLayoutManager layoutManager = new GridLayoutManager(this, 2);


recyclerView.setLayoutManager(layoutManager);


adapter = new FruitAdapter(fruitList);


recyclerView.setAdapter(adapter);


}


private void initFruits() {


fruitList.clear();


for (int i = 0; i < 50; i++) {


Random random = new Random();


int index = random.nextInt(fruits.length);


fruitList.add(fruits[index]);


}


}


@Override


public boolean onCreateOptionsMenu(Menu menu) {


getMenuInflater().inflate(R.menu.toolbar,menu);


return true;


}


@Override


public boolean onOptionsItemSelected(MenuItem item) {


switch (item.getItemId()) {


case android.R.id.home:


mDrawerLayout.openDrawer(GravityCompat.START);


break;


case R.id.item1:


Toast.makeText(this, "You clicked Backup", Toast.LENGTH_SHORT).show();


break;


case R.id.item2:


Toast.makeText(this, "You clicked Delete", Toast.LENGTH_SHORT).show();


break;


case R.id.item3:


Toast.makeText(this, "You clicked Settings", Toast.LENGTH_SHORT).show();


break;


default:


}


return true;


}


}


package aystudio.nopi.materialdesign;


public class Fruit {


private String name;


private int imageId;


public Fruit(String name, int imageId) {


this.name = name;


this.imageId = imageId;


}


public String getName() {


return name;


}


public int getImageId() {


return imageId;


}


}


package aystudio.nopi.materialdesign;


import android.content.Context;


import android.content.Intent;


import android.support.v7.widget.CardView;


import android.support.v7.widget.RecyclerView;


import android.view.LayoutInflater;


import android.view.View;


import android.view.ViewGroup;


import android.widget.ImageView;


import android.widget.TextView;


import com.bumptech.glide.Glide;


import java.util.List;


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder>{


private static final String TAG = "FruitAdapter";


private Context mContext;


private List<Fruit> mFruitList;


static class ViewHolder extends RecyclerView.ViewHolder {


CardView cardView;


ImageView fruitImage;


TextView fruitName;


public ViewHolder(View view) {


super(view);


cardView = (CardView) view;


fruitImage = (ImageView) view.findViewById(R.id.fruit_image);


fruitName = (TextView) view.findViewById(R.id.fruit_name);


}


}


public FruitAdapter(List<Fruit> fruitList) {


mFruitList = fruitList;


}


@Override


public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


if (mContext == null) {


mContext = parent.getContext();


}


View view = LayoutInflater.from(mContext).inflate(R.layout.fruit_item, parent, false);


final ViewHolder holder = new ViewHolder(view);


holder.cardView.setOnClickListener(new View.OnClickListener() {


@Override


public void onClick(View v) {


}


});


return holder;


}


@Override


public void onBindViewHolder(ViewHolder holder, int position) {


Fruit fruit = mFruitList.get(position);

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
【Java转Android】58,讲的太清楚了