基于图像滑动执行片段

时间:2017-01-18 07:04:51

标签: java android android-layout android-fragments

我有一个简单的滚动条,其中包含不同的图像,希望根据滚动中间的图像(选定)执行某些碎片。 我正在使用PageAdapter并且已经创建了片段。

以下是主要活动的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
tools:context="com.example.androidviewpagerapp.MainActivity" >



<LinearLayout
    android:id="@+id/ll2"
    android:layout_width="match_parent"
    android:layout_height="20dp">
    <ImageView
        android:id="@+id/image1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/arrow"
        android:paddingLeft="10dp"/>



</LinearLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/myviewpager"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_alignParentBottom="true"
    android:layout_gravity="center"
    android:layout_marginBottom="200dp"
    android:clipToPadding="false"
    android:gravity="center"
    android:paddingLeft="46dp"
    android:paddingRight="46dp"
    android:layout_marginLeft="175dp"
    android:layout_below="@+id/ll2" />

主要活动的Java代码:

公共类MainActivity扩展了ActionBarActivity {

FragmentPagerAdapter adapterViewPager;
ViewPager viewPager;
MyPagerAdapter myPagerAdapter;
TextView textMsg;
Button btnToFirst, btnToLast;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   // textMsg = (TextView)findViewById(R.id.msg);
    //textMsg.setMovementMethod(new ScrollingMovementMethod());

    viewPager = (ViewPager) findViewById(R.id.myviewpager);
    //setupViewPager(viewPager);
    myPagerAdapter = new MyPagerAdapter();
    viewPager.setAdapter(myPagerAdapter);

    viewPager.setOnPageChangeListener(myOnPageChangeListener);

    ViewPager vpPager = (ViewPager) findViewById(R.id.myviewpager);
    vpPager.setClipToPadding(false);
    vpPager.setPageMargin(1);

 /*   ViewPager vpPager = (ViewPager) findViewById(R.id.myviewpager);
    adapterViewPager = new MyPagerAdapter(getSupportFragmentManager());
    vpPager.setAdapter(adapterViewPager);*/

}

OnPageChangeListener myOnPageChangeListener =
        new OnPageChangeListener(){

            @Override
            public void onPageScrollStateChanged(int state) {
                //Called when the scroll state changes.
            }

            @Override
            public void onPageScrolled(int position,
                                       float positionOffset, int positionOffsetPixels) {
                //This method will be invoked when the current page is scrolled,
                //either as part of a programmatically initiated smooth scroll
                //or a user initiated touch scroll.
            }

            @Override
            public void onPageSelected(int position) {
                //This method will be invoked when a new page becomes selected.
               // textMsg.append("onPageSelected:" + position + "\n");
            }};


private class MyPagerAdapter extends PagerAdapter {
    int NumberOfPages = 9;

    int[] res = { R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5 , R.drawable.a6 , R.drawable.a7 , R.drawable.a8 , R.drawable.a9 };

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

       // textMsg.append("instantiateItem:" + position + "\n");


        ImageView imageView = new ImageView(MainActivity.this);
        imageView.setImageResource(res[position]);
        LayoutParams imageParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        imageView.setLayoutParams(imageParams);

        LinearLayout layout = new LinearLayout(MainActivity.this);
        layout.setOrientation(LinearLayout.HORIZONTAL);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        layout.setLayoutParams(layoutParams);
        layout.addView(imageView);

        container.addView(layout);
        return layout;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout) object);

        //textMsg.append("destroyItem:" + position + "\n");
    }
  }

}

我想找出一种通过onPageSelected()方法访问优选的方法。

1 个答案:

答案 0 :(得分:0)

您需要在onPageScroll方法中添加一些代码

  @Override
        public void onPageScrolled(int position,
                                   float positionOffset, int positionOffsetPixels) {
            //Execute your code here while swiping

        }

代码示例检查 - Swipe pager