如何在Android中的viewpager末尾启用按钮?

时间:2015-10-02 22:38:34

标签: android android-viewpager viewpagerindicator android-pageradapter

我是Android新手。我试图在视图分页器末尾的前视图中实现一个按钮。滑动图像时按钮将保持隐藏状态,如果我在viewpager的末尾,则按钮将可见。如何更正?

我已经尝试获取图像的位置并插入if语句,该按钮将可见,但它不起作用。请帮帮我......

public class GuestActivity extends Activity {

ViewPager viewPagerguest;
MyPagerAdapter myPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.guestactivity);
    viewPagerguest = (ViewPager)findViewById(R.id.myviewpager);
    myPagerAdapter = new MyPagerAdapter();
    viewPagerguest.setAdapter(myPagerAdapter);

}

private class MyPagerAdapter extends PagerAdapter {

    int NumberOfPages = 5;

    int[] res = {
            R.drawable.meme1,
            R.drawable.meme2,
            R.drawable.meme4,
            R.drawable.meme3,
            R.drawable.meme5
    };

    int[] backgroundcolor = {
            0xFF101010,
            0xFF202020,
            0xFF303030,
            0xFF404040,
            0xFF505050};

    @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) {


        TextView textView = new TextView(GuestActivity.this);
        textView.setTextColor(Color.WHITE);
        textView.setTextSize(30);
        textView.setTypeface(Typeface.DEFAULT_BOLD);
        textView.setText(String.valueOf(position));
        textView.setGravity(View.TEXT_ALIGNMENT_CENTER);

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

        RelativeLayout layout = new RelativeLayout(GuestActivity.this);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layout.setBackgroundColor(backgroundcolor[position]);
        layout.setLayoutParams(layoutParams);
        layout.addView(textView);
        layout.addView(imageView);

        final int page = position;
        if (page == 3){
            Button btnjoin = (Button) findViewById(R.id.btnJoin);
            btnjoin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intentjoin = new Intent(GuestActivity.this, LoginActivity.class);
                    startActivity(intentjoin);
                }
            });
        }
        layout.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

            }});

        container.addView(layout);
        return layout;
    }

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

}

}

1 个答案:

答案 0 :(得分:-1)

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
    RelativeLayout layout = new RelativeLayout(GuestActivity.this);
    ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    layout.setLayoutParams(layoutParams);
    if (position == 0 || position == NumberOfPages) {
        Button btnjoin = (Button) findViewById(R.id.btnJoin);
            btnjoin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intentjoin = new Intent(GuestActivity.this, LoginActivity.class);
                    startActivity(intentjoin);
                }
            });
        layout.addView(btnjoin);
    } else {
        position++;
        TextView textView = new TextView(GuestActivity.this);
        textView.setTextColor(Color.WHITE);
        textView.setTextSize(30);
        textView.setTypeface(Typeface.DEFAULT_BOLD);
        textView.setText(String.valueOf(position));
        textView.setGravity(View.TEXT_ALIGNMENT_CENTER);

        ImageView imageView = new ImageView(GuestActivity.this);
        imageView.setImageResource(res[position]);
        ViewGroup.LayoutParams imageParams = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        imageView.setLayoutParams(imageParams);
        layout.setBackgroundColor(backgroundcolor[position]);
        layout.addView(textView);
        layout.addView(imageView);

        final int page = position;

        layout.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

            }});
    }



    container.addView(layout);
    return layout;
}

但最适合此use RecycleView