使View对ViewPager上的第一张图片不可见,其余为onClickListener可见?

时间:2016-02-24 20:07:33

标签: java android android-fragments

我有一个ViewPager设置,其中包含要在其间滑动的图像数组。在底部,我有一个按钮,允许用户选择最适合他们的图像。我想让按钮在数组的第一个图像上不可见,然后在第二个图像被刷到后按钮变为可见。我尝试了许多不同的解决方案,但我目前很难过。我可以通过许多不同的路径使按钮不可见,例如将其可见性设置为在xml文件中不可见,以及通过onCreate中的if ... if else语句,并得出结论我可能需要某种onClickListener。这是我目前的代码

public class MainActivity extends AppCompatActivity {

ViewPager viewPager;

// Method for "That's Me" button, stores selected motivator choice
public void thatsMeClick(View view) {

    Intent intent = new Intent(MainActivity.this, PhysicalCondition.class);
    startActivity(intent);

    SharedPreferences sharedPreferences = this.getSharedPreferences("com.elevenfifty.vali.challengeaccepted",
        Context.MODE_PRIVATE);

    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt("motivator", viewPager.getCurrentItem());
    editor.apply();

    int myIntValue = sharedPreferences.getInt("motivator", -1);

    Log.i("motivator", Integer.toString(myIntValue));

}

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    // Allows images to fill entire screen including the notification bar area
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

    setContentView(R.layout.activity_main);

    ImageAdapter adapter = new ImageAdapter(this);

    viewPager = (ViewPager) findViewById(R.id.view_pager);
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(0);
    viewPager.setPageTransformer(true, new ZoomOutPageTransformer());

    PageIndicator circleIndicator = (CirclePageIndicator) findViewById(R.id.circle_indicator);
    circleIndicator.setViewPager(viewPager);

}

}

我尝试将此添加到onCreate,该按钮用于将按钮设置为不可见,但一旦用户滑动到第二个图像,它就不会再回到可见状态。我觉得onClickListener会解决这个问题,但我不知道如何让它正常工作,我尝试了几种不同的东西。

Button thatsMe = (Button) findViewById(R.id.thats_me_button);

    if (viewPager.getCurrentItem() == 0) {

        thatsMe.setVisibility(View.INVISIBLE);

    } else if (viewPager.getCurrentItem() > 0) {

        thatsMe.setVisibility(View.VISIBLE);

    }

我尝试为onClickListener做这样的事情,但是它不起作用,希望对如何解决这个问题有所帮助,谢谢!

public void onClick (View view) {

    final Button thatsMe = (Button) findViewById(R.id.thats_me_button);

    thatsMe.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (viewPager.getCurrentItem() != 0) {

                thatsMe.setVisibility(View.VISIBLE);

            }
        }
    });
}

以下是sagits要求我添加的ImageAdapter代码。

public class ImageAdapter extends PagerAdapter {

Context context;

// Array of images used for Viewpager
private int[] images = new int[]{R.drawable.mot, R.drawable.mogy,
    R.drawable.hife, R.drawable.loot, R.drawable.roel,
    R.drawable.crish, R.drawable.surk};

ImageAdapter(Context context) {
    this.context = context;
}

@Override
public int getCount() {
    return images.length;
}

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

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

    ImageView imageView = new ImageView(context);
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    imageView.setImageResource(images[position]);

    container.addView(imageView, 0);

    return imageView;
}

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

1 个答案:

答案 0 :(得分:1)

利用OnPageChangeListener。只是在OnCreate中设置将无法正常工作,因为只有在页面滚动时才会调用它。

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if(position>0){
            thatsMe.setVisibility(View.VISIBLE);
        }
        else {

            thatsMe.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
});