在viewpager滑动时重置togglebutton

时间:2013-11-16 23:39:59

标签: android android-fragments android-viewpager togglebutton

情况:我正在制作一个包含3个片段的应用,用户可以使用viewpager在其间滑动。在每个片段上,他们都有一个显示/隐藏答案的切换按钮。

问题:当您单击第一个片段上的切换按钮时,它会打开。然后当您滑动到第二个片段并向后滑动到第一个片段时,切换按钮仍然会检查ON(它仍然显示答案)。当你向前滑动然后向后滑动时,我怎么能这样做,它会重置按钮状态。

我认为将它放在片段

中会很简单
public void onResume(){
    super.onResume();

    showHideButton.setChecked(false);
}

这没有用。我还看了一堆其他帖子similer到这个但没有什么工作。我很抱歉我今晚无法回到这里回答问题,因为我在工作,我只是短暂休息一下,问这个问题。

编辑:

的活动:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
public class Activity10_1 extends FragmentActivity {

Fragment10_1 frag = new Fragment10_1();

ViewPager pager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //Set up pager and fragment manager.
    pager = (ViewPager) findViewById(R.id.pager);
    FragmentManager fm = getSupportFragmentManager();
    MyFragmentPageAdapter pagerAdapter = new MyFragmentPageAdapter(fm);
    pager.setAdapter(pagerAdapter);
    pageChange();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity10_1, menu);
    return true;
}
public void pageChange(){
    pager.setOnPageChangeListener(new OnPageChangeListener(){

                @Override
                public void onPageScrollStateChanged(int position) {}
                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {}
                @Override
                public void onPageSelected(int position) {

                    switch(position){
                    case 0:
                        frag.getShowHideButton().setChecked(false);
                        break;
                    case 1:
                        frag.getShowHideButton().setChecked(false);
                        break;
                    case 2:
                        frag.getShowHideButton().setChecked(false);
                        break;
                    default:
                        frag.getShowHideButton().setChecked(false);
                    }
                }

          });

}

}

寻呼机适配器:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class MyFragmentPageAdapter extends FragmentPagerAdapter {

DataHolder10_1 dh = new DataHolder10_1();

final int PAGE_COUNT = 3;

public MyFragmentPageAdapter(FragmentManager fm) {
    super(fm);
    // TODO Auto-generated constructor stub
}

// method will be involked when a page is requested to create
@Override
public Fragment getItem(int arg0){
    //Create bundle.
    Bundle data = new Bundle();

    //Instantiate fragment.
    Fragment10_1 myFragment = new Fragment10_1();



    //Add data to bundle.
    data.putInt("pageNumberData", arg0 + 1);
    data.putString("questionData", dh.getQuestions()[arg0]);
    data.putString("answerData", dh.getAnswers()[arg0]);

    //Send bundle to fragment.
    myFragment.setArguments(data);

    return myFragment;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub

    return PAGE_COUNT;
}

}

片段:

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

public class Fragment10_1 extends Fragment {

private int pageNumber;
private String question;
private String answer;

//declare TextViews.
private TextView pageNumberTextView;
private TextView questionTextView;
private TextView answerTextView;

private String origMessage;// saves message of answer for switching between show and hide.

//colors for switching between show and hide.
private int showColor;
private int hideColor;

//declare toggle button.
private ToggleButton showHideButton;

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

    //Get bundle data.
    Bundle data = getArguments();

    // Get integer data of the key current page from the bundle
    pageNumber = data.getInt("pageNumberData", 0);
    question = data.getString("questionData");
    answer = data.getString("answerData");



}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState){


    View view = inflater.inflate(R.layout.fragment10_1, container, false);

    pageNumberTextView = (TextView)view.findViewById(R.id.pageCount1);
    questionTextView = (TextView)view.findViewById(R.id.question1);
    answerTextView = (TextView)view.findViewById(R.id.answer1);
    showHideButton = (ToggleButton)view.findViewById(R.id.toggleButton1);



    showHideButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean on){

            if(on){
                //change color and text if button is set to show answer.
                answerTextView.setTextColor(showColor);
                answerTextView.setText(answer);
            }else{
                //default answer to hidden properties.
                answerTextView.setTextColor(hideColor);
                answerTextView.setText(origMessage);
            }
        }
    });

    //grab colors from text views for switching between show and hide.
    showColor = questionTextView.getTextColors().getDefaultColor();
    hideColor = answerTextView.getTextColors().getDefaultColor();

    //set original message.
    origMessage = getString(R.string.answer);



    //create Text views and button down here.
    pageNumberTextView.setText("Page: " + pageNumber + "/3");
    questionTextView.setText(question);

    return view;
}
public ToggleButton getShowHideButton(){

    return showHideButton;
}
}

现在我正在尝试使用OnPageChangeListener。应用程序很好但是当我滑动时它会导致它崩溃。

编辑2:此处未创建小部件

private TextView pageNumberTextView;
private TextView questionTextView;
private TextView answerTextView;
private ToggleButton showHideButton;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState){


    View view = inflater.inflate(R.layout.fragment10_1, container, false);

    pageNumberTextView = (TextView)view.findViewById(R.id.pageCount1);
    questionTextView = (TextView)view.findViewById(R.id.question1);
    answerTextView = (TextView)view.findViewById(R.id.answer1);
    showHideButton = (ToggleButton)view.findViewById(R.id.toggleButton1);

1 个答案:

答案 0 :(得分:0)

使用ViewPage.OnPageChangeListener。设置监听器并实现onPageSelected()。

在onPageSelected()中,您需要获取当前活动的页面。有不同的方法来做到这一点。一种方法是保留自己的片段数组。我将举一个简短的例子:

将此添加到MyFragmentPagerAdapter:

public Fragment findFragmentByPosition(int position) {
    FragmentPagerAdapter fragmentPagerAdapter = getFragmentPagerAdapter();
    return getSupportFragmentManager().findFragmentByTag(
            "android:switcher:" + getViewPager().getId() + ":"
                    + fragmentPagerAdapter.getItemId(position));
}

在onPageSelected(int position)中,执行此操作(并删除现有代码):

Fragment10_1 curFrag = pagerAdapter.findFragmentByPosition (position);
curFrag.getShowHideButton().setChecked(false);