ImageView滑块水平

时间:2017-04-05 18:03:50

标签: java android android-studio imageview android-imageview

我正在尝试在我的主布局文件中创建一个适合我的imageview中的图像的水平滑块。但是代替在触摸时滑动的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>

这是用于滑动的片段布局:

<?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>

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

1 个答案:

答案 0 :(得分:0)

您可以使用布局来包装imageview,然后将布局包装在水平滚动视图中