我想在recyclerview中显示数组中的图像,所以我将数组传递给适配器,但是recyclerview不会绑定到适配器,因为我在“ onBindViewHolder”中使用了“ Log.d” 但是Log没有显示消息。我的代码有什么问题?还有其他解决方案吗?
片段中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_plan, container, false);
paths[0] = "/storage/emulated/0/demonuts_upload_gallery/1543839716755.jpg";
paths[1] = "/storage/emulated/0/demonuts_upload_gallery/1543839716755.jpg";
paths[2] = "/storage/emulated/0/demonuts_upload_gallery/1543839716755.jpg";
paths[3] = "/storage/emulated/0/demonuts_upload_gallery/1543839716755.jpg";
paths[4] = "/storage/emulated/0/demonuts_upload_gallery/1543839716755.jpg";
adapter = new ImagesBodyAdapter(view.getContext());
LinearLayoutManager layoutManager = new LinearLayoutManager(view.getContext(),LinearLayoutManager.HORIZONTAL, false);
recyclerView = view.findViewById(R.id.recycler);
recyclerView.setLayoutManager(layoutManager);
adapter.addItems(paths);
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
适配器类为:
public class ImagesBodyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private String[] PATHS = new String[5];
private Context context;
public ImagesBodyAdapter(Context context){
this.context = context;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.image_body_item,viewGroup,false);
return new ViewHolderItemDetail(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
//ViewHolderItemDetail holder = (ViewHolderItemDetail) viewHolder;
Log.d("adapter","recycler binded!");
for (int counter=0; counter<PATHS.length; counter++)
{
File imageFile = new File(PATHS[i]);
if(imageFile.exists()) {
((ViewHolderItemDetail) viewHolder).imageBody.setImageBitmap(BitmapFactory.decodeFile(imageFile.getAbsolutePath()));
}
}
}
public void addItems(String[] paths){
for (int i=0; i<paths.length;i++){
this.PATHS[i] = paths[i];
notifyItemChanged(i);
// notifyDataSetChanged();
}
}
private class ViewHolderItemDetail extends RecyclerView.ViewHolder{
ImageView imageBody;
ViewHolderItemDetail(View v) {
super(v);
imageBody = v.findViewById(R.id.imageBody);
}
}
谢谢
答案 0 :(得分:0)
使用此库:Picasso Library
并参考此链接以将图像加载到ImageView中(在您的适配器BindViewHolder中)。这简单 : Picasso Use Case
编辑 样例代码
Picasso.get().load("your/url/or/file/path").into(imageView);
编辑2: 您一定不能在adapter内迭代。该适配器负责一个ViewHolder,仅负责将要加载的一幅图像。
答案 1 :(得分:0)
Stavro Xhardha建议使用一个库将图像加载到ImageView中(尽管我个人偏爱Glide https://github.com/bumptech/glide)
但是,您的问题之一是没有调用onBindViewHolder。我可能会误会,但是在设置适配器上的数据之前,我总是会先在RecylerView上设置适配器。所以代替;
# -*- coding: utf-8 -*
import serial
import math
import time
import RPi.GPIO as GPIO
servoPIN = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(servoPIN, GPIO.OUT)
fire_gun = GPIO.PWM(servoPIN, 50) #GPIO 5 for PWM with 50Hz
fire_gun.start(2.5) #Initialization
"""
def gunRight():
#fire_gun = GPIO.PWM(servoPIN, 50) #GPIO 5 for PWM with 50Hz
#fire_gun.start(2.5) #Initialization
print "Shot Fire The First Net Gun"
fire_gun.ChangeDutyCycle(4.9)
time.sleep(0.4)
fire_gun.ChangeDutyCycle(7.2)
time.sleep(0.4)
print "Servo Motor Back To Rest Position"
fire_gun.stop()
"""
ser = serial.Serial("/dev/ttyUSB0", 115200)
def getTFminiData():
while True:
count = ser.in_waiting
if count > 8:
recv = ser.read(9)
ser.reset_input_buffer()
if recv[0] == 'Y' and recv[1] == 'Y': # 0x59 is 'Y'
low = int(recv[2].encode('hex'), 16)
high = int(recv[3].encode('hex'), 16)
distance = low + high * 256
#My unit conversion starts here
dist = float(distance) /100
print(distance)
print "Distance to object is:",dist,"Meters"
#GPIO.setup(TRIG, GPIO.OUT)
#GPIO.setup(ECHO, GPIO.IN)
#if dist <= 0.70:
#GPIO.output(5, True)
try:
if ser.is_open == False:
ser.open()
for i in range(500):
getTFminiData()
if dist <= 0.70:
fire_gun.ChangeDutyCycle(4.9)
time.sleep(0.4)
GPIO.cleanup()
except KeyboardInterrupt: # Ctrl+C
if ser != None:
#GPIO.cleanup()
ser.close()
您将拥有;
recyclerView = view.findViewById(R.id.recycler);
recyclerView.setLayoutManager(layoutManager);
adapter.addItems(paths);
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
答案 2 :(得分:0)
您可以使用滑行库或piccaso库 前往https://github.com/bumptech/glide滑行 访问https://github.com/square/picasso以获得piccaso库 因此,现在您只需轻松制作适配器即可。
首先,您应该在类中为图像设置一个参数; 例如,您创建了一个Person Class;
所以你的课程是:
public class Person{
String name ;
String lastName;
String imgUrl;
//define setter, getter and constactive
}
在bindview的适配器中,您只需在毕加索中设置此图像,例如下面的代码
public void onBindViewHolder(final ViewHolder holder,final int position){
Piccaso.with(contect).load(list.get(position).getImageUrl()).into(holder.imageview);
}
答案 3 :(得分:-1)
在片段中,您可以将上下文作为 getActivity(),因此尝试将代码更改为:
adapter = new ImagesBodyAdapter(view.getContext());
至
adapter = new ImagesBodyAdapter(getActivity());