java - ImageView 水平 slider

标签 java android android-studio imageview android-imageview

我正在尝试在主布局文件的 ImageView 中创建一个水平图像 slider 。但代码不是在触摸时滑动 imageview,而是水平移动整个布局。

以下是我的滑动Java代码:

    public class Artprofile extends FragmentActivity {
        static final int NUM_ITEMS = 5;
        ImageFragmentPagerAdapter imageFragmentPagerAdapter;
        ViewPager viewPager;
        public static final String[] IMAGE_NAME = {"a", "b", "c", "d", "e",};

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fragment_page);
            imageFragmentPagerAdapter = new ImageFragmentPagerAdapter(getSupportFragmentManager());
            viewPager = (ViewPager) findViewById(R.id.pager);
            viewPager.setAdapter(imageFragmentPagerAdapter);
        }

       public static class ImageFragmentPagerAdapter extends FragmentPagerAdapter {
        public ImageFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return NUM_ITEMS;
        }

        @Override
        public Fragment getItem(int position) {
            SwipeFragment fragment = new SwipeFragment();
            return SwipeFragment.newInstance(position);
        }
   }

        public static class SwipeFragment extends Fragment {
        @Override

            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View swipeView = inflater.inflate(R.layout.layoutart, container, false);
            ImageView imageView = (ImageView) swipeView.findViewById(R.id.image_view1);
            Bundle bundle = getArguments();
            int position = bundle.getInt("position");
            String imageFileName = IMAGE_NAME[position];
            int imgResId = getResources().getIdentifier(imageFileName, "drawable", "com.example.user.myapplication1");
             imageView.setImageResource(imgResId);
             return swipeView;
          }

           static SwipeFragment newInstance(int position) {
            SwipeFragment swipeFragment = new SwipeFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("position", position);
            swipeFragment.setArguments(bundle);
            return swipeFragment;
           }
       }
   }

以下是我的主要布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/mobileback"
     android:layout_width="wrap_content"
   android:layout_height="wrap_content" >


  <ImageView android:id="@+id/image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:maxWidth="150dp"
    android:maxHeight="150dp"
    android:scaleType="fitCenter"
    android:layout_marginRight="50dp"
    android:layout_marginTop="30dp"
    android:layout_marginLeft="400dp"
    android:src="@drawable/profilewindow1"
    android:layout_marginEnd="58dp"
    android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="330dp"
    android:layout_alignParentStart="true"
    android:maxWidth="150dp"
    android:maxHeight="150dp"
    android:layout_below="@+id/image_view"
    android:id="@+id/layout_mobile1">



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


    />
  </FrameLayout>

</RelativeLayout>

这是用于滑动的Fragment布局:

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" >


<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v4.view.ViewPager>

我不明白这个问题。请帮忙。提前致谢

最佳答案

可以使用布局来包裹imageview,然后将布局包裹在水平 ScrollView 中

关于java - ImageView 水平 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43238522/

相关文章:

java - Android 密码 XML 属性 - 已弃用

java - 如何在/info 执行器端点中获取 Spring Boot 的版本

java - 包含 Apache POI 时的 Android 重复类错误

java - 使用 LWJGL 避免闪烁

java - 我可以使用NotificationCompat.BigTextStyle而不会在旧设备上崩溃吗?

android - 根据应用程序状态停止和启动服务

java - 运行单元测试后,SQLite 似乎自动清除,但 SharedPreferences 没有。这样对吗?

升级到 1.5 后,Android studio IDE 布局发生了变化

java - 如何使用 Asterisk 调用 Skype Out 电话?

java - 字符串文字未正确执行