0

我正在尽我所能找到一种方法如何滑动图像。我已经从 draw-able 导入了图像,现在我想滑动查看下一张图像,这是我的 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 { private Context 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 Activity 用户单击时全尺寸图像的位置

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);

 }
}

您的帮助将不胜感激,我只是想滑动图像 thx

4

1 回答 1

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 的高度和宽度。我没有建造这个,但我想我会工作。

于 2018-11-14T19:23:47.950 回答