如何使用Viewpager添加幻灯片图像

时间:2018-11-14 14:03:54

标签: androidimageslider android-studio-3.3

我正在努力寻找一种方法来滑动图像。 我已经从可绘制图像导入了图像,现在我要滑动以查看下一张图像, 这是我的MainActivity:

public class MainActivity extends AppCompatActivity {

List<imagefull> lstBook;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lstBook = new ArrayList<>();
    lstBook.add(new imagefull(R.drawable.thevigitarian));
    lstBook.add(new imagefull(R.drawable.thewildrobot));
    lstBook.add(new imagefull(R.drawable.mariasemples));
    lstBook.add(new imagefull(R.drawable.themartian));
    lstBook.add(new imagefull(R.drawable.hediedwith));
    lstBook.add(new imagefull(R.drawable.thevigitarian));
    lstBook.add(new imagefull(R.drawable.thewildrobot));
    lstBook.add(new imagefull(R.drawable.mariasemples));
    lstBook.add(new imagefull(R.drawable.themartian));
    lstBook.add(new imagefull(R.drawable.hediedwith));

    RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
    RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(this,lstBook);
    recyclerView.setLayoutManager(new GridLayoutManager(this,3));
    recyclerView.setAdapter(recyclerViewAdapter);
 }
}

这是我的activitymain xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:padding="8dp">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>

这是我的RecyclerViewAdapter

公共类RecyclerViewAdapter扩展    RecyclerView.Adapter {     私有上下文mContext;     私有列表mData;

public RecyclerViewAdapter(Context mContext, List<imagefull> mData) {
    this.mContext = mContext;
    this.mData = mData;
}


@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view;
    LayoutInflater minflater =LayoutInflater.from(mContext);
    view = minflater.inflate(R.layout.card_view, parent, false);
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder( MyViewHolder Holder, final int position) {
    Holder.imageView.setImageResource(mData.get(position).getThumbnail());
    Holder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String action;
            Intent intent = new Intent(mContext,FullScreenImagView.class);
            intent.putExtra("Thumbnail",mData.get(position).getThumbnail());
            mContext.startActivity(intent);
        }
    });
}

@Override
public int getItemCount() {
    return mData.size();
}

public static class  MyViewHolder extends RecyclerView.ViewHolder
{
    ImageView imageView;
    CardView cardView ;

    public MyViewHolder(View itemView) {
        super(itemView);

        imageView = (ImageView) itemView.findViewById(R.id.img_view);
        cardView = (CardView)itemView.findViewById(R.id.cardview);
    }
 }
}

这是我的Fullscreenimageview活动,其中是用户单击时以全尺寸显示的图片

public class FullScreenImagView extends AppCompatActivity {

ImageView full_img_view;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_full_screen_imag_view);

    full_img_view = findViewById(R.id.full_img_view);

    Intent intent = getIntent();
    int image = intent.getExtras().getInt("Thumbnail");
    full_img_view.setImageResource(image);

 }
}

您的帮助将得到赞赏,我希望您可以滑动图像

1 个答案:

答案 0 :(得分:0)

您可以在设置recyclerview的布局时尝试

RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);

recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false));

RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(this,lstBook);

recyclerView.setAdapter(recyclerViewAdapter);

在您的item_view.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:orientation="vertical">

  <ImageView
     android:id="@+id/item_image"
     android:layout_width="match_parent"
     android:layout_height="match_parent"/>

</LinearLayout>

无论如何,请务必根据需要调整ImageView的高度和宽度。我没有建立这个,但我认为我会工作。