1. 程式人生 > >RecyclerView 實現瀑布流及點選事件,含點選回撥的介面

RecyclerView 實現瀑布流及點選事件,含點選回撥的介面

RecyclerView 實現瀑布流及點選事件

public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
    private List<Fruit> mFruitList;


    /**
     *自定義內部類,構造傳入條目view
     */
    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView fruitImage;
        TextView fruitName;
        View fruitView;

        public
ViewHolder(View itemView) { super(itemView); fruitView=itemView; fruitImage= (ImageView) itemView.findViewById(R.id.fruit_image); fruitName= (TextView) itemView.findViewById(R.id.fruit_name); } } //Adapter的構造傳入陣列 public FruitAdapter(List<Fruit> mFruitList) { this
.mFruitList = mFruitList; } //傳入佈局,繫結佈局 @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.fruitView.setOnClickListener(new
View.OnClickListener() { @Override public void onClick(View view) { int position=holder.getAdapterPosition(); Fruit fruit=mFruitList.get(position); Toast.makeText(view.getContext(),"你想吃"+fruit.getName()+"的水果",Toast.LENGTH_SHORT).show(); } }); holder.fruitImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int position=holder.getAdapterPosition(); Fruit fruit=mFruitList.get(position); Toast.makeText(view.getContext(),"你想吃圖片的"+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 implements View.OnClickListener {

    private Button button;
    private ProgressBar pb_test;
    private RecyclerView recycler_view;
    private List<Fruit> fruitList=new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFruits();//初始化陣列
        initView();
    }

    private void initView() {
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
        pb_test = (ProgressBar) findViewById(R.id.pb_test);

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

        //LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        //layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); //橫向滾動  GridLayoutManager  StaggeredGridLayoutManager 瀑布流
        StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
        recycler_view.setLayoutManager(layoutManager);
        FruitAdapter adapter=new FruitAdapter(fruitList);
        recycler_view.setAdapter(adapter);
        //recycler_view.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:

                break;
        }
    }

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

水果類

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

}