发布于 2017-09-13 21:16:01 | 16 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要为大家详细介绍了RecyclerView的简单使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

RecyclerView的功能很强大,本文为大家分享RecyclerView的简单使用方法。

效果图:

activity_main.xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <android.support.v7.widget.RecyclerView
  android:id="@+id/recycler_view"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>

</LinearLayout>

fruit_item.xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_margin="5dp">

 <ImageView
  android:id="@+id/fruit_image"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"/>

 <TextView
  android:id="@+id/fruit_name"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="left"
  android:layout_marginTop="10dp"/>

</LinearLayout>

Fruit实体类:


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 void setName(String name) {
  this.name = name;
 }

 public int getImageId() {
  return imageId;
 }

 public void setImageId(int imageId) {
  this.imageId = imageId;
 }
}

FruitAdapter:


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

 private List<Fruit> mFruitList;

 static class ViewHolder extends RecyclerView.ViewHolder{
  ImageView fruitImage;
  TextView fruitName;
  public ViewHolder(View view){
   super(view);
   fruitImage = view.findViewById(R.id.fruit_image);
   fruitName = view.findViewById(R.id.fruit_name);
  }
 }

 public FruitAdapter(List<Fruit> fruitList){
  mFruitList = fruitList;
 }

 @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.fruitImage.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
    Fruit fruit = mFruitList.get(holder.getAdapterPosition());
    Toast.makeText(view.getContext(),"you clicked view "+fruit.getName(),Toast.LENGTH_SHORT).show();
   }
  });
  return holder;
 }

 @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 AppCompatActivity {

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

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  initFruit();
  RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
  StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(3,
    StaggeredGridLayoutManager.VERTICAL);
  recyclerView.setLayoutManager(manager);
  FruitAdapter adapter = new FruitAdapter(fruitList);
  recyclerView.setAdapter(adapter);
 }

 public void initFruit(){
  for (int i = 0; i < 2; i++) {
   Fruit apple = new Fruit(getRandomLengthName("Apple"), R.drawable.apple_pic);
   fruitList.add(apple);
   Fruit banana = new Fruit(getRandomLengthName("Banana"), R.drawable.banana_pic);
   fruitList.add(banana);
   Fruit orange = new Fruit(getRandomLengthName("Orange"), R.drawable.orange_pic);
   fruitList.add(orange);
   Fruit watermelon = new Fruit(getRandomLengthName("Watermelon"), R.drawable.watermelon_pic);
   fruitList.add(watermelon);
   Fruit pear = new Fruit(getRandomLengthName("Pear"), R.drawable.pear_pic);
   fruitList.add(pear);
   Fruit grape = new Fruit(getRandomLengthName("Grape"), R.drawable.grape_pic);
   fruitList.add(grape);
   Fruit pineapple = new Fruit(getRandomLengthName("Pineapple"), R.drawable.pineapple_pic);
   fruitList.add(pineapple);
   Fruit strawberry = new Fruit(getRandomLengthName("Strawberry"), R.drawable.strawberry_pic);
   fruitList.add(strawberry);
   Fruit cherry = new Fruit(getRandomLengthName("Cherry"), R.drawable.cherry_pic);
   fruitList.add(cherry);
   Fruit mango = new Fruit(getRandomLengthName("Mango"), R.drawable.mango_pic);
   fruitList.add(mango);
  }
 }


 public String getRandomLengthName(String name){
  Random random = new Random();
  int length = random.nextInt(20)+1;
  StringBuilder builder = new StringBuilder();
  for(int i = 0; i < length; i++){
   builder.append(name);
  }
  return builder.toString();
 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHPERZ。



相关阅读 :
RecyclerView的简单使用
android应用开发之spinner控件的简单使用
解析Android中string-array数据源的简单使用
条件数据库Android:sqllite的简单使用
Android开发中类加载器DexClassLoader的简单使用讲解
android AlertDialog的简单使用实例
Android ViewFlipper的简单使用
Android学习之Broadcast的简单使用
Android开发之CheckBox的简单使用与监听功能示例
Android开发之RadioGroup的简单使用与监听示例
Android Glide的简单使用
Android使用SQLite数据库的简单实例
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务