1

我在使视图在 viewpager 中可见和不可见时遇到问题。我的要求是我想在单击按钮或图像时使视图不可见,但是此操作发生在下一个连续页面上。这是我尝试过的适配器类

public ViewPagerAdapter(MainActivity mainActivity, int noofsize) {
    // TODO Auto-generated constructor stub
    size = noofsize;
    act = mainActivity;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return size;
}

@Override
public Object instantiateItem(View container, int position) {
    // TODO Auto-generated method stub
    LayoutInflater inflater = (LayoutInflater) act
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    layout = inflater.inflate(R.layout.viewpages, null);
    click = (Button) layout.findViewById(R.id.click);
    text = (TextView) layout.findViewById(R.id.text);
    click.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            text.setVisibility(View.INVISIBLE);
        }
    });
    ((ViewPager) container).addView(layout, 0);
    return layout;
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);
}

@Override
public Parcelable saveState() {
    return null;
}

而Activity类如下

public class MainActivity extends Activity {

int noofsize = 4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ViewPagerAdapter adapter = new ViewPagerAdapter(MainActivity.this,
            noofsize);
    ViewPager myPager = (ViewPager) findViewById(R.id.reviewpager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);
}
4

0 回答 0