在viewpager布局上向不同视图添加按钮/功能?

时间:2017-03-12 20:00:51

标签: android android-layout android-viewpager

我对其中显示的浏览器/不同布局感到有点绝望。

总结:切换不同的布局非常有效。现在我想在不同的布局上实现按钮/功能,这是不起作用的。

我有以下viewpager,它运行良好。它遍历布局列表并显示不同的布局。

我的活动

public static final String LOG_TAG = MainActivity.class.getSimpleName();

private ViewPager viewPager;
private ViewPagerAdapter viewPagerAdapter;
private LinearLayout dotsLayout;
private TextView[] dots;
private int[] layouts;
private Button btnSkip, btnNext;

@
Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_swipe);

    viewPager = (ViewPager) findViewById(R.id.view_pager);
    dotsLayout = (LinearLayout) findViewById(R.id.layoutDots);
    //btnSkip = (Button) findViewById(R.id.btn_skip);
    btnNext = (Button) findViewById(R.id.btn_next);

    layouts = new int[] {
        R.layout.activity_mmg_description_start,
            R.layout.activity_mmg_question_1,
            R.layout.activity_mmg_question_2,
            R.layout.activity_mmg_question_3,
            R.layout.activity_mmg_question_4,
            R.layout.activity_mmg_question_5,
            R.layout.activity_mmg_question_6,
            R.layout.activity_mmg_question_7,
            R.layout.activity_mmg_question_8,
            R.layout.activity_mmg_question_9,
            R.layout.activity_mmg_question_10,
            R.layout.activity_mmg_question_11,
            R.layout.activity_mmg_question_12,
            R.layout.activity_mmg_question_13,
            R.layout.activity_mmg_question_14
    };

    // adding bottom dots
    addBottomDots(0);

    viewPagerAdapter = new ViewPagerAdapter();
    viewPager.setAdapter(viewPagerAdapter);
    viewPager.addOnPageChangeListener(viewPagerPageChangeListener);


}

@
Override
public void onBackPressed() {}

public void btnNextClick(View v) {
    // checking for last page
    // if last page home screen will be launched
    int current = getItem(1);
    if (current < layouts.length) {
        // move to next screen
        viewPager.setCurrentItem(current);
    } else {
        launchHomeScreen();
    }
}

ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {

    @
    Override
    public void onPageSelected(int position) {
        addBottomDots(position);

        // changing the next button text 'NEXT' / 'GOT IT'
        if (position == layouts.length - 1) {
            // last page. make button text to GOT IT
            btnNext.setText(getString(R.string.start_measurement_manual));
            //btnSkip.setVisibility(View.GONE);
        } else {
            // still pages are left
            btnNext.setText(getString(R.string.next));
            //btnSkip.setVisibility(View.VISIBLE);
        }
    }

    @
    Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    @
    Override
    public void onPageScrollStateChanged(int arg0) {

    }
};


private void addBottomDots(int currentPage) {
    dots = new TextView[layouts.length];

    dotsLayout.removeAllViews();
    for (int i = 0; i < dots.length; i++) {
        dots[i] = new TextView(this);
        dots[i].setText(Html.fromHtml("&#8226;"));
        dots[i].setTextSize(35);
        dots[i].setTextColor(getResources().getColor(R.color.dot_inactive));
        dotsLayout.addView(dots[i]);
    }

    if (dots.length > 0)
        dots[currentPage].setTextColor(getResources().getColor(R.color.dot_active));
}


private int getItem(int i) {
    return viewPager.getCurrentItem() + i;
}

private void launchHomeScreen() {
    startActivity(new Intent(this, ManualActivity.class));
    finish();
}

public class ViewPagerAdapter extends PagerAdapter {
    private LayoutInflater layoutInflater;


    public ViewPagerAdapter() {

    }

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


        //Inflate the correct layout
        layoutInflater = (LayoutInflater) getSystemServ**Option 1: starting with layout activity_mmg_question_1**ice(Context.LAYOUT_INFLATER_SERVICE);

        //layouts[] is an int[] that points to resources such as R.layout.start_page
        View inflatedView = layoutInflater.inflate(layouts[position], container, false);

        ((ViewPager) container).addView(inflatedView, 0);

        switch (position) {
            case R.layout.activity_mmg_description_start:
                Log.d(LOG_TAG, "QD " + "Firstviewcame");

            case R.layout.activity_mmg_question_1:
                //LinearLayout linearLayout_mmg_1 = (LinearLayout)inflatedView.findViewById(R.id.linearlayout_mmg_1);

                Button testButton = (Button) findViewById(R.id.testbutton);

                testButton.setOnClickListener(new View.OnClickListener() {

                    @
                    Override
                    public void onClick(View v) {
                        Log.d(LOG_TAG, "QD " + "Swipebuttonclicked");
                    }
                });


        }

        return inflatedView;
    }

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

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


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

我想为不同的布局添加功能。在布局上:activity_mmg_question_1是一个按钮(R.id.testbutton),它应该写一条日志消息。

我尝试了一些选择。

选项1:从布局activity_mmg_question_1

开始

在开头取消注释R.layout.activity_mmg_description_start:

    layouts = new int[]{
            //R.layout.activity_mmg_description_start,
            R.layout.activity_mmg_question_1,

以下代码有效:

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


        //Inflate the correct layout
        layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        //layouts[] is an int[] that points to resources such as R.layout.start_page
        View inflatedView = layoutInflater.inflate(layouts[position], container, false);

        ((ViewPager)container).addView(inflatedView,0);

        LinearLayout linearLayout_mmg_1 = (LinearLayout)inflatedView.findViewById(R.id.linearlayout_mmg_1);

        Button testButton = (Button)findViewById(R.id.testbutton);

        testButton.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Log.d (LOG_TAG, "QD " + "Swipebuttonclicked");
                    }
                });

        return inflatedView;
    }

完全可以理解:它使布局膨胀,找到按钮,起作用。

选项2:从布局activity_mmg_description_start开始

从开头的R.layout.activity_mmg_description_start开始:

    layouts = new int[]{
            R.layout.activity_mmg_description_start,
            R.layout.activity_mmg_question_1,

最终以

结束
FATAL EXCEPTION: main
[...]
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

我想我也得到了这个:在第一个布局(R.layout.activity_mmg_description_start)上它没有找到按钮,因此它指向null,并显示错误。

如何防止这种情况?这让我想到了:

选项3:开关案例

显示不同的布局有效,但按钮的日志消息不起作用。

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


    //Inflate the correct layout
    layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    //layouts[] is an int[] that points to resources such as R.layout.start_page
    View inflatedView = layoutInflater.inflate(layouts[position], container, false);

    ((ViewPager)container).addView(inflatedView,0);

    switch (position){
    case R.layout.activity_mmg_description_start:
        Log.d (LOG_TAG, "QD " + "Firstviewcame");

    case R.layout.activity_mmg_question_1:
        //LinearLayout linearLayout_mmg_1 = (LinearLayout)inflatedView.findViewById(R.id.linearlayout_mmg_1);

        Button testButton = (Button)findViewById(R.id.testbutton);

        testButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.d (LOG_TAG, "QD " + "Swipebuttonclicked");
        }
        });


    }

    return inflatedView;
}

有人有想法或解决方案吗?

最好并提前致谢,

tigercode

1 个答案:

答案 0 :(得分:1)

我认为你应该使用Fragments

在调用((ViewPager)容器)之前尝试setOnClickListener .addView(inflatedView,0);

public Object instantiateItem(ViewGroup container, int position) {


//Inflate the correct layout
layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

//layouts[] is an int[] that points to resources such as R.layout.start_page
View inflatedView = layoutInflater.inflate(layouts[position], container, false);


switch (position){
case R.layout.activity_mmg_description_start:
    Log.d (LOG_TAG, "QD " + "Firstviewcame");

case R.layout.activity_mmg_question_1:
    //LinearLayout linearLayout_mmg_1 = (LinearLayout)inflatedView.findViewById(R.id.linearlayout_mmg_1);

    Button testButton = (Button)findViewById(R.id.testbutton);

    testButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Log.d (LOG_TAG, "QD " + "Swipebuttonclicked");
    }
    });


}

((ViewPager)container).addView(inflatedView,0);

return inflatedView;

}