andriod学习 RecyclerView (瀑布流 waterfall)

Oria ·
更新时间:2024-11-14
· 635 次阅读

教程:

简书 Tyhoo_Wu RecyclerView 总结教程

andriod RecyclerView 官方教程

需要item.xml、对象类、对象适配器Adapter类、以及此项目用到的展示类Fragement.

 

 1.item.xml 设计自定义格式

  

2.对象适配器Adapter类 package com.example.example1.mywechat; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.List; public class gameAdapter extends RecyclerView.Adapter { private List list; private Context context; private View inflater; public gameAdapter(List gameList) { list = gameList; } @NonNull @Override public gameAdapter.myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { inflater = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false); myViewHolder myViewHolder = new myViewHolder(inflater); return myViewHolder; } @Override public void onBindViewHolder(@NonNull gameAdapter.myViewHolder holder, int position) { Game game = list.get(position); holder.playerImage.setImageResource(game.getImageid()); holder.playerName.setText(game.getName()); } @Override public int getItemCount() { return list.size(); } class myViewHolder extends RecyclerView.ViewHolder{ View playerView; ImageView playerImage; TextView playerName; public myViewHolder(@NonNull View itemView) { super(itemView); playerView = itemView; playerImage = itemView.findViewById(R.id.imageView); playerName = itemView.findViewById(R.id.textViewPlayerName); } } } 3. 展示类Fragement package com.example.example1.mywechat; import android.os.Bundle; import android.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.StaggeredGridLayoutManager; import java.util.ArrayList; import java.util.List; /** * A simple {@link Fragment} subclass. */ public class gameFragment extends Fragment { private List gameList = new ArrayList(); public gameFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.tab01, container, false); RecyclerView recyclerView = view.findViewById(R.id.recycler_view); initPayers(); StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL); recyclerView.setLayoutManager(manager); gameAdapter adapter = new gameAdapter(gameList); recyclerView.setAdapter(adapter); return view; } private void initPayers() { for (int i = 0; i < 10; i++) { Game player_bi = new Game(i,R.drawable.game_1,"risk of rain 2"); gameList.add(player_bi); Game player_kb = new Game(i,R.drawable.game_2,"prison"); gameList.add(player_kb); Game player_lbj = new Game(i,R.drawable.game_3,"饥荒"); gameList.add(player_lbj); Game player_sc = new Game(i,R.drawable.game_4,"植物僵尸"); gameList.add(player_sc); Game player_kd = new Game(i,R.drawable.game_5,"消消乐"); gameList.add(player_kd); Game player_dr = new Game(i,R.drawable.game_6,"建岛"); gameList.add(player_dr); Game player_ga = new Game(i,R.drawable.game_7,"分手厨房"); gameList.add(player_ga); Game player_jh = new Game(i,R.drawable.game_8,"谁是卧底"); gameList.add(player_jh); Game player_ad = new Game(i,R.drawable.game_9,"大型魔幻"); gameList.add(player_ad); } } }  4.项目结果

5.项目源码 

github 源码


作者:Not-Df



recyclerview 瀑布 瀑布流

需要 登录 后方可回复, 如果你还没有账号请 注册新账号