使用recyclerview需要导入.jar包
在bulid.gradle中加入一行
implementation 'androidx.recyclerview:recyclerview:1.1.0'
activity_main.xml中设置列表样式
在recyclerview_list.xml中设置列表中每一项的样式
在MyAdapter.java中创建适配器,将数据传递给recyclerview,并实现监听器
package com.example.myapplication; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.baseAdapter; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.List; public class MyAdapter extends RecyclerView.Adapter{ private List data; private Context context; public MyAdapter(List data, Context context) { this.data = data; this.context = context; } @NonNull @Override public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = View.inflate(context,R.layout.recyclerview_list,null);//得到每一个子模块的样式 return new MyViewHolder(view); } @Override public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) { holder.tv.setText(data.get(position).getName()); } @Override public int getItemCount() { return data==null?0:data.size(); } public class MyViewHolder extends RecyclerView.ViewHolder { private TextView tv; public MyViewHolder(@NonNull View itemView) { super(itemView); tv = itemView.findViewById(R.id.tv); itemView.setonClickListener(new View.onClickListener(){ @Override public void onClick(View view) { if(onRecyclerItemClickListener!=null){ onRecyclerItemClickListener.onRecyclerItemClick(getAdapterPosition()); } } }); } } private onRecyclerItemClickListener onRecyclerItemClickListener; public void setonRecyclerItemClickListener(onRecyclerItemClickListener listener){ onRecyclerItemClickListener = listener; } public interface OnRecyclerItemClickListener{ void onRecyclerItemClick(int position); } }
在bean.java中设置数据类型
package com.example.myapplication; public class Bean { String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
在MainActivity.java中创建列表中每一项的信息,并创建监听器
package com.example.myapplication; import androidx.appcompat.app.alertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import androidx.core.app.NotificationCompat; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.StaggeredGridLayoutManager; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.DialogInterface; import android.content.Intent; import android.graphics.BitmapFactory; import android.graphics.Color; import android.media.audiofx.DynamicsProcessing; import android.nfc.Tag; import android.os.Build; import android.os.Bundle; import android.provider.FontsContract; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.PopupMenu; import android.widget.PopupWindow; import android.widget.ProgressBar; import android.widget.TextView; import java.util.ArrayList; import java.util.List; import javax.security.auth.login.LoginException; public class MainActivity extends AppCompatActivity { private Listdata = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);//R文件自动生成,不可修改,创建的资源都会在R文件中生成一个索引 for(int i=0;i<100;i++) { Bean bean=new Bean(); bean.setName("zhang"+i); data.add(bean); } RecyclerView recyclerView = findViewById(R.id.rv);//recyclerView得到界面 // LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);//给recyclerView创建布局 // recyclerView.setLayoutManager(linearLayoutManager); GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3); recyclerView.setLayoutManager(gridLayoutManager); //StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, LinearLayout.VERTICAL); //recyclerView.setLayoutManager(staggeredGridLayoutManager);//不建议用 MyAdapter myAdapter = new MyAdapter(data,this);//把数据传给myAdapter recyclerView.setAdapter(myAdapter);//通过myAdapter把数据传给recyclerView myAdapter.setonRecyclerItemClickListener(new MyAdapter.onRecyclerItemClickListener() { @Override public void onRecyclerItemClick(int position) { Log.e("lala", "onRecyclerItemClick: "+position ); } }); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)