2017-04-05 175 views
1

我想创建一个水平滑块的图像适合我的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> 

我不明白这个问题。请帮助。在此先感谢

+0

是viewpager是什么?它的幻灯片全片段'getItem' mehod – rafsanahmad007

+0

你的意思是你只需从swift中打开一个R.layout.layoutart的图像视图,而其他的则留在它的位置? – DmitryBorodin

+0

@DmitryBorodin我想要的是我只能移动image_view1,而不是整个布局。代码滑动整个布局。 –

回答

0

您可以使用布局来包装的ImageView,然后在横向滚动型包裹布局

+0

我已经将imageview包装到了framelayout中,但没有运气 –

+0

使用线性布局 –