美文网首页
RecyclerView

RecyclerView

作者: 昨天剩下的一杯冷茶 | 来源:发表于2018-11-02 11:43 被阅读1次

    布局文件fruit_item

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <ImageView
            android:id="@+id/fruit_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <TextView
            android:id="@+id/fruit_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp"/>
    </LinearLayout>
    
    

    activity_main布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.example.hzx.recyclerview.MainActivity">
        
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycle_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
        
    </LinearLayout>
    
    

    创建一个水果的类

    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;
        }
    }
    
    

    创建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 = (ImageView)view.findViewById(R.id.fruit_image);
                fruitName = (TextView)view.findViewById(R.id.fruit_name);
            }
        }
    
        public FruitAdapter(List<Fruit> fruitList){
            mFruitList = fruitList;
        }
    
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.fruit_item,parent,false);
            ViewHolder holder = new ViewHolder(view);
            return holder;
        }
        public void onBindViewHolder(ViewHolder holder ,int position){
            Fruit fruit = mFruitList.get(position);
            holder.fruitImage.setImageResource(fruit.getImageId());
            holder.fruitName.setText(fruit.getName());;
        }
        public int getItemCount(){
            return mFruitList.size();
        }
    }
    
    
    

    MainActivity

    public class MainActivity extends AppCompatActivity {
    
        private List<Fruit> fruitList = new ArrayList<>();
        private RecyclerView recyclerView;
        private FruitAdapter adapter;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            initFruits();
            recyclerView = (RecyclerView) findViewById(R.id.recycle_view);
    
            LinearLayoutManager layoutManager = new LinearLayoutManager(this);
            recyclerView.setLayoutManager(layoutManager);
    
            adapter = new FruitAdapter(fruitList);
            recyclerView.setAdapter(adapter);
        }
    
        private void initFruits() {
            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);
            }
        }
    
        private String getRandomLengthName(String name) {
            return name;
        }
    }
    
    
    

    注意:
    1、 需要在app build.gradle添加
    compile 'com.android.support:recyclerview-v7:24.2.1'
    效果


    image.png

    相关文章

      网友评论

          本文标题:RecyclerView

          本文链接:https://www.haomeiwen.com/subject/osiqxqtx.html