我根据这里的一篇文章Recyclerview实例:https://blog.51cto.com/u_15359293/3908682?articleABtest=0 写了个程序:存在错误,麻烦高手帮我看看哪里出错,谢谢!以下是我根据上面文章写的源码:
https://files.cnblogs.com/files/blogs/782609/RecyclerView2.zip?t=1723108873&download=true
你上几年级?你连基础都没掌握呢,先别看recyclerView了,先从Java看起吧,连基础的导包都弄懂。
MainActivity.java
package com.example.recyclerview2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.DefaultItemAnimator;
import android.os.Bundle;
import android.widget.Toast;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List<Bitmap> mDatas;
private MyRecyclerAdapter recycleAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 确保这是正确的布局文件名
recyclerView = findViewById(R.id.recy_list);
initData();
recycleAdapter = new MyRecyclerAdapter(this, mDatas); // 使用 MainActivity.this 作为上下文
// 使用 GridLayoutManager
recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
recyclerView.setAdapter(recycleAdapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recycleAdapter.setOnItemClickListener(new MyRecyclerAdapter.OnItemClickListener() {
@Override
public void onLongClick(int position) {
Toast.makeText(MainActivity.this, "onLongClick事件您点击了第:" + position + "个Item", Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(int position) {
Toast.makeText(MainActivity.this, "onClick事件您点击了第:" + position + "个Item", Toast.LENGTH_SHORT).show();
}
});
}
private void initData() {
mDatas = new ArrayList<>();
mDatas.add(BitmapFactory.decodeResource(getResources(), R.mipmap.a1));
mDatas.add(BitmapFactory.decodeResource(getResources(), R.mipmap.a2));
mDatas.add(BitmapFactory.decodeResource(getResources(),R.mipmap.a3));
mDatas.add(BitmapFactory.decodeResource(getResources(),R.mipmap.a4));
mDatas.add(BitmapFactory.decodeResource(getResources(),R.mipmap.a5));
mDatas.add(BitmapFactory.decodeResource(getResources(),R.mipmap.a6));
// 添加更多 Bitmap
}
}
MyRecyclerAdapter.java
package com.example.recyclerview2;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> {
private OnItemClickListener mOnItemClickListener;
private List<Bitmap> mDatas;
private Context mContext;
private LayoutInflater inflater;
public MyRecyclerAdapter(Context context, List<Bitmap> datas){
this. mContext=context;
this. mDatas=datas;
inflater=LayoutInflater. from(mContext);
}
@Override
public int getItemCount() {
return mDatas.size();
}
//重写onCreateViewHolder方法,返回一个自定义的ViewHolder
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_home,parent, false);
MyViewHolder holder= new MyViewHolder(view);
return holder;
}
class MyViewHolder extends RecyclerView.ViewHolder{
ImageView iv;
public MyViewHolder(View view) {
super(view);
iv=(ImageView) view.findViewById(R.id.iv_item_adapter);
}
}
public interface OnItemClickListener{
void onClick( int position);
void onLongClick( int position);
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener ){
this.mOnItemClickListener=onItemClickListener;
}
//填充onCreateViewHolder方法返回的holder中的控件
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
holder.iv.setImageBitmap(mDatas.get(position));
if (mOnItemClickListener != null) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnItemClickListener.onClick(holder.getBindingAdapterPosition());
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
mOnItemClickListener.onLongClick(holder.getBindingAdapterPosition());
return false;
}
});
}
}
}
item_home.xml
<?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"
android:padding="16dp">
<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/descriptionTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp" />
<ImageView
android:id="@+id/iv_item_adapter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp" />
</LinearLayout>
谢谢,我知道import是导包,问题是不知缺什么包,没提示。以前用vs 时,按ctrl+enter 会自动导入包,as 没提示
@tea2007: 怎么会没有提示,一般android studio都有输入提示吧,未引入的包也会提示引入建议包啊?