我想創建一個水平滑塊的圖像適合我的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>
我不明白這個問題。請幫助。在此先感謝
是viewpager是什麼?它的幻燈片全片段'getItem' mehod – rafsanahmad007
你的意思是你只需從swift中打開一個R.layout.layoutart的圖像視圖,而其他的則留在它的位置? – DmitryBorodin
@DmitryBorodin我想要的是我只能移動image_view1,而不是整個佈局。代碼滑動整個佈局。 –