alltolove 发表于 2017-9-28 11:27:48

android编程3.6.1

关闭之前的项目,新建一个android7.0的项目,双击文件列表里build.gradle在里边dependencies标签里添加依赖项compile 'com.android.support:recyclerview-v7:26.+'
activity_main.xml文件修改为<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.xinwei.myrecycleviewactivity.MainActivity">

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

</android.support.constraint.ConstraintLayout>
layout目录下新建fruit_item.xml文件<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <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" />
</LinearLayout>
在mainactivity.java文件旁边新建Fruit.java文件package com.example.xinwei.myrecycleviewactivity;

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;
    }
}
还是把原来的图片复制到drawable目录下,然后把mainactivity.java修改为:package com.example.xinwei.myrecycleviewactivity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private 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);
      LinearLayoutManager layoutManager = new LinearLayoutManager(this);
      recyclerView.setLayoutManager(layoutManager);
      FruitAdapter adapter = new FruitAdapter(fruitList);
      recyclerView.setAdapter(adapter);
    }
    private void initFruit() {
      for (int i=0;i<2;i++){
            Fruit a = new Fruit("樱桃",R.drawable.a);
            fruitList.add(a);
            Fruit b = new Fruit("草莓",R.drawable.b);
            fruitList.add(b);
            Fruit c = new Fruit("小樱桃",R.drawable.c);
            fruitList.add(c);
            Fruit d = new Fruit("苹果",R.drawable.d);
            fruitList.add(d);
            Fruit e = new Fruit("桃子",R.drawable.e);
            fruitList.add(e);
            Fruit f = new Fruit("青柠檬",R.drawable.f);
            fruitList.add(f);
            Fruit g = new Fruit("柠檬",R.drawable.g);
            fruitList.add(g);
            Fruit h = new Fruit("葡萄",R.drawable.h);
            fruitList.add(h);
            Fruit i1 = new Fruit("梨",R.drawable.i);
            fruitList.add(i1);
            Fruit j = new Fruit("香蕉",R.drawable.j);
            fruitList.add(j);
      }
    }
}




再新建FruitAdapter.java文件:
package com.example.xinwei.myrecycleviewactivity;

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 java.util.List;

/**
* Created by xinwei on 2017/9/28.
*/

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

    @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();
    }
}

这个每页shi默认只显示一张图,明天继续完善,还有一些其他功能,效果图:
页: [1]
查看完整版本: android编程3.6.1