使用ViewPager内的按钮按钮、ViewPager

2023-09-06 13:25:26 作者:辞臣

我试图用一个ViewPager,用户可以在每个窗格中互动。对于这个ViewPager,我创建了我的用户展示轻扫水平切换窗格中的教程。当用户到达最后窗格,被显示给在那里他们将被导航到仪表盘的用户的按钮。我读了几篇文章,试图了解一个按钮的工作原理ViewPager内,却无法真正使多大意义。

点击一个ViewPager内的窗格中的一个按钮时,什么code应该做的是,仪表盘活动已启动。

下面是pagerAdapter的全code:

 类MyPagerAdapter扩展PagerAdapter {

 公众诠释getCount将(){

    返回5;
 }

公共对象instantiateItem(查看收集,INT位置){

    视图V =新景(collection.getContext());

    LayoutInflater充气=(LayoutInflater)collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    INT渣油= 0;
    开关(位置){
    情况下0:
        渣油= R.layout.tutorial_step1;
    打破;

    情况1:
        渣油= R.layout.tutorial_step2;

    打破;

    案例2:
        渣油= R.layout.tutorial_step3;
    打破;

    案例3:
        渣油= R.layout.tutorial_step4;

    打破;

    壳体4:
        渣油= R.layout.tutorial_complete;
        V = inflater.inflate(R.layout.tutorial_complete,空,假);
        的LinearLayout L =(的LinearLayout)v.findViewById(R.id.llComplete);
        Button按钮=(按钮)l.findViewById(R.id.bTutorialComplete);
        button.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                //创建一个启动一个不同的活动意图
            }
        });
        打破;
    }
    ((ViewPager)集合).addView(V,0);

    返回伏;

}

@覆盖
公共无效destroyItem(查看为arg0,INT ARG1,ARG2对象){
    ((ViewPager)为arg0).removeView((查看)ARG2);

}

@覆盖
公共布尔isViewFromObject(查看为arg0,对象ARG1){
    返回将arg0 ==((查看)ARG1);

}

@覆盖
公共Parcelable saveState和(){
    返回null;
 }
}
 

这是我所看到的在文章中,code我应该是工作是:

 公共对象instantiateItem(查看收集,INT位置){

    视图V =新景(collection.getContext());

    LayoutInflater充气=(LayoutInflater)collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    INT渣油= 0;
    开关(位置){
    情况下0:
        渣油= R.layout.tutorial_step1;
    打破;

    情况1:
        渣油= R.layout.tutorial_step2;

    打破;

    案例2:
        渣油= R.layout.tutorial_step3;
    打破;

    案例3:
        渣油= R.layout.tutorial_step4;

    打破;

    壳体4:
        渣油= R.layout.tutorial_complete;
        V = inflater.inflate(R.layout.tutorial_complete,空,假);
        的LinearLayout L =(的LinearLayout)v.findViewById(R.id.llComplete);
        Button按钮=(按钮)l.findViewById(R.id.bTutorialComplete);
        button.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                //创建一个启动一个不同的活动意图
            }
        });
        打破;
    }
    ((ViewPager)集合).addView(V,0);

    返回伏;

}
 
Android开发 控件基础 十 ViewPager 实现页面的滑动切换

只有案例4:开关语句,我用,因为这有而一个按钮范围内工作其它布局只包含图像和文本其不需要的相互作用。

我一直在这无数个小时,现在仍不能想出一个办法来解决这个问题。我会很感激,如果你能帮助我与此有关。

解决方案

 最后上下文语境= collection.getContext();
    button.setOnClickListener(新OnClickListener(){

        @覆盖
        公共无效的onClick(视图v){
            context.startActivity(新意图(背景下,Dashboard.class));
        }
    });
    打破;
 

P.S。你不能让集合 最后,因为该方法 instantiateItem 是一个 @覆盖

提示:如果你想是不同的页面,你可以把你的code。在开关(位置)

  LayoutInflater吹气=(LayoutInflater)collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE)
视图V = NULL;
开关(位置){
    情况下0:
        V = inflater.inflate(R.layout.somelayout);
        按钮BTN =(按钮)v.findViewById(R.id.somebutton);
        btn.setText(BTN);
    打破;

    情况1:
        V = inflater.inflate(R.layout.someotherlayout);
        TextView的电视=(按钮)v.findViewById(R.id.sometextview);
        tv.setText(BTN);
    打破;
}

((ViewPager)集合).addView(V,0);
返回伏;
 

I am trying to use a ViewPager where users can interact within each pane. For this ViewPager, I am creating a tutorial where I show users to swipe horizontally to switch pane. When the user reaches the last pane, a button is displayed to the user where they will be navigated to the dashboard. I have read a few articles trying to understand how a button works within a ViewPager, but could not really make much sense.

What the code should do is when a button within a pane within a ViewPager is clicked, the dashboard activity is started.

Here is the full code of the pagerAdapter:

class MyPagerAdapter extends PagerAdapter {

 public int getCount() {

    return 5;
 }

public Object instantiateItem(View collection, int position) {

    View v = new View(collection.getContext());

    LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    int resId = 0;
    switch (position) {
    case 0:
        resId = R.layout.tutorial_step1;
    break;

    case 1:
        resId = R.layout.tutorial_step2;

    break;

    case 2:
        resId = R.layout.tutorial_step3;
    break;

    case 3:
        resId = R.layout.tutorial_step4;

    break;

    case 4:
        resId = R.layout.tutorial_complete;
        v = inflater.inflate(R.layout.tutorial_complete, null, false);
        LinearLayout l = (LinearLayout) v.findViewById(R.id.llComplete);
        Button button=(Button)l.findViewById(R.id.bTutorialComplete);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //Create an intent that starts a different activity
            }
        });
        break;
    }
    ((ViewPager) collection).addView(v, 0);

    return v;

}

@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;
 }
}

From what I have seen in articles, the code I should be working with is:

public Object instantiateItem(View collection, int position) {

    View v = new View(collection.getContext());

    LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    int resId = 0;
    switch (position) {
    case 0:
        resId = R.layout.tutorial_step1;
    break;

    case 1:
        resId = R.layout.tutorial_step2;

    break;

    case 2:
        resId = R.layout.tutorial_step3;
    break;

    case 3:
        resId = R.layout.tutorial_step4;

    break;

    case 4:
        resId = R.layout.tutorial_complete;
        v = inflater.inflate(R.layout.tutorial_complete, null, false);
        LinearLayout l = (LinearLayout) v.findViewById(R.id.llComplete);
        Button button=(Button)l.findViewById(R.id.bTutorialComplete);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //Create an intent that starts a different activity
            }
        });
        break;
    }
    ((ViewPager) collection).addView(v, 0);

    return v;

}

It is only case 4: within the switch statement that I am working with as this has a button whereas the other layouts only contain images and text which requires no interaction.

I have been working on this for countless hours now and still couldn't figure out a way to resolve this. I would be very grateful if you can help me with this.

解决方案

    final Context context = collection.getContext();
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            context.startActivity(new Intent(context, Dashboard.class));
        }
    });
    break;

P.s. you cannot make collection final since the method instantiateItem is an @Override

Hint: if you want to make pages that are different you can put your code in switch(position):

LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE)
View v = null;
switch (position) {
    case 0:
        v = inflater.inflate(R.layout.somelayout);
        Button btn = (Button) v.findViewById(R.id.somebutton);
        btn.setText("btn");
    break;

    case 1:
        v = inflater.inflate(R.layout.someotherlayout);
        TextView tv = (Button) v.findViewById(R.id.sometextview);
        tv.setText("btn");
    break;
}

((ViewPager) collection).addView(v, 0);
return v;