java - 图片没有被滑动?

标签 java android xml android-studio

我制作了一个画廊,在这个中,当我在全 View 中打开图像(例如图像1),然后向右滑动时,仅图像1旁边的图像(即图像2)出现,而图像2后面的图像不会显示,并且类似的事情发生在我按图像 1 的相反方向单击。 enter image description here

FullimageActivity.java

package com.example.android.helloapp;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;

public class Full_ImageActivity extends AppCompatActivity {
    float startXValue = 1;
    ImageView fullImageView;
    int position;

    private Integer[] mImageIds = { R.drawable.gallery_one,R.drawable.gallery_two,R.drawable.gallery_three,R.drawable.gallery_four,R.drawable.gallery_five,
            R.drawable.gallery_six,R.drawable.gallery_seven,R.drawable.gallery_eight,R.drawable.gallery_nine,R.drawable.gallery_ten,
            R.drawable.gallery_eleven,R.drawable.gallery_twelve,R.drawable.gallery_thirteen,R.drawable.gallery_fourteen,R.drawable.gallery_fifteen,
            R.drawable.gallery_sixteen,R.drawable.gallery_seventeen,R.drawable.gallery_eighteen,R.drawable.gallery_nineteen,
            R.drawable.gallery_twenty,R.drawable.gallery_twenty_one,R.drawable.gallery_twenty_two,R.drawable.gallery_twenty_three,R.drawable.gallery_twenty_four,
            R.drawable.gallery_twenty_five};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getResources().getConfiguration().orientation ==
                Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.layout.activity_full__image);
        } else {
            setContentView(R.layout.activity_full__image);
        }

        Intent i=getIntent();
        position=i.getExtras().getInt("id");
        ImageAdapter imageAdapter=new ImageAdapter(this);
        fullImageView=(ImageView)findViewById(R.id.fullImageView);
        fullImageView.setImageResource(imageAdapter.images[position]);
    }

    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        int orientation;
        if (getResources().getConfiguration().orientation ==
                Configuration.ORIENTATION_PORTRAIT) {
            orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            // or = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        }else {
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
        }
        // Add code if needed
        setRequestedOrientation(orientation);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float endXValue=0;
        float x1=event.getAxisValue(MotionEvent.AXIS_X);
        int action= MotionEventCompat.getActionMasked(event);
        switch (action){
            case (MotionEvent.ACTION_DOWN):
                startXValue=event.getAxisValue(MotionEvent.AXIS_X);
                return true;

            case (MotionEvent.ACTION_UP):
                endXValue = event.getAxisValue(MotionEvent.AXIS_X);
                if (endXValue > startXValue) {
                    if (endXValue - startXValue > 100) {
                        System.out.println("Left-Right");
                        if(position-1!=-1)
                        fullImageView.setImageResource(mImageIds[position-1]);
                    }
                }else {
                    if (startXValue -endXValue> 100) {
                        System.out.println("Right-Left");
                        if(position+1!=mImageIds.length)
                        fullImageView.setImageResource(mImageIds[position+1]);
                    }
                }
                return true;

            default:
                return super.onTouchEvent(event);
        }
    }
}

fullimage.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_full__image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.android.helloapp.Full_ImageActivity">

    <include layout="@layout/tool_bar"></include>

    <ImageView
        android:id="@+id/fullImageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerInside"
        android:layout_gravity="center"
        />
</LinearLayout>

ImageAdapter.java

package com.example.android.helloapp;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    private Context context;
    public Integer[] images={
            R.drawable.gallery_one,R.drawable.gallery_two,R.drawable.gallery_three,R.drawable.gallery_four,R.drawable.gallery_five,
            R.drawable.gallery_six,R.drawable.gallery_seven,R.drawable.gallery_eight,R.drawable.gallery_nine,R.drawable.gallery_ten,
            R.drawable.gallery_eleven,R.drawable.gallery_twelve,R.drawable.gallery_thirteen,R.drawable.gallery_fourteen,R.drawable.gallery_fifteen,
            R.drawable.gallery_sixteen,R.drawable.gallery_seventeen,R.drawable.gallery_eighteen,R.drawable.gallery_nineteen,
            R.drawable.gallery_twenty,R.drawable.gallery_twenty_one,R.drawable.gallery_twenty_two,R.drawable.gallery_twenty_three,R.drawable.gallery_twenty_four,
            R.drawable.gallery_twenty_five
    };
    public ImageAdapter(Context c){
        context=c;
    }

    @Override
    public int getCount() {
        return images.length;
    }

    @Override
    public Object getItem(int position) {
        return images[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView=new ImageView(context);
        imageView.setImageResource(images[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setLayoutParams(new GridView.LayoutParams(240,240));
        return imageView;
    }
}

最佳答案

打开任何图像后,我们假设位置为3

所以向右走并显示 position+1 图像,但position的值仍然是3,所以下次再次滑动position+1意味着将显示第四张图像

解决方案:将新的位置值重新分配给position变量或使用一元运算符,即position--position++

关于java - 图片没有被滑动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42714248/

相关文章:

java - Android 是否有相当于 string.canBeConvertedToStringEncoding(encoding) 的函数

java - 匿名 Handler 或 Runnable 会造成内存泄漏吗?

java - 如何在xslt中使用java

c# - 如何在 XML 文档中包含 DTD

c# - 删除 xml :base attribute from an XDocument

java - android - 折叠工具栏和 fragment 布局不能一起工作

java - 从一项 Activity 转移到另一项 Activity 时如何保留 editText 输入?

java.lang.NoSuchFieldError : FAIL_ON_SYMBOL_HASH_OVERFLOW] in elasticsearch Watcher plugin 错误

java - 安卓编程。将数组从一个 Activity 传递到另一个 Activity

android - android 数据库的大小会影响设备的电池效率吗?