我正在尝试从ArrayList显示图像.ArrayList中的图像URL是从Firebase中检索的。我想在点击txt_btntamam时看到带有ViewPager的图像。我正在创建自己的SwipeAdapter类并且我检索了图像在Firebase的questActivity类中。 如果,如何使用ViewPager查看questActivity中的图像?
SwipeAdapter类
public class SwipeAdapter extends PagerAdapter {
private LayoutInflater layoutInflater;
private Context context;
public ArrayList<String> imgArrayList=new ArrayList<>();
public ImageView imgpage;
public SwipeAdapter(Context context){
this.context=context;
notifyDataSetChanged();
}
@Override
public int getCount() {
return imgArrayList.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view==object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
layoutInflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView=layoutInflater.inflate(R.layout.page_fragment_layout,container,false);
imgpage=itemView.findViewById(R.id.img_pageImgs);
Picasso.get().load(imgArrayList.get(position)).into(imgpage);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}
当我点击txt_btntamam时,我应该能够逐个看到arrayList中的图像。 的 questActivity.class
public class questActivity extends AppCompatActivity {
ViewPager viewPager;
SwipeAdapter swipeAdapter;
TextView txttamam;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quest);
init();
bundleProcesses();
swipeAdapter=new SwipeAdapter(this);
viewPager.setAdapter(swipeAdapter);
viewPager.beginFakeDrag();
getImagesMethod();
txttamam.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(getItem(+1),true);
}
});
}
private void getImagesMethod(){
DatabaseReference ref= FirebaseDatabase.getInstance().getReference().child("level").child("1");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
swipeAdapter.imgArrayList.add(String.valueOf(snapshot.getValue()));
//Log.d("tag", String.valueOf(snapshot.getValue()));
}
Log.d("ArrayListSize", String.valueOf(swipeAdapter.imgArrayList.size()));
swipeAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private int getItem(int i) {
return viewPager.getCurrentItem() + i;
}
private void init(){
viewPager=findViewById(R.id.viewPager);
txttamam=findViewById(R.id.txt_btntamam);
}
private void bundleProcesses(){
Bundle extras = getIntent().getExtras();
int btnid = extras.getInt("sendLevelid");
Toast.makeText(this,String.valueOf(btnid), Toast.LENGTH_SHORT).show();
}
}
感谢您的帮助。