如何从选项卡式布局中的特定选项卡获取按钮

时间:2018-09-24 23:21:17

标签: java android android-studio tabs

我创建了DynamicTabbedLayout并在我使用的片段中添加了4个toogleButton,但是我遇到了无法使用onCheckedChanged()的问题,因为它在所有选项卡中都会更改。

我可以使用开关来检查选项卡,然后设置onCheckedChanged(),但是我有太多的选项卡。

是否有更好的方法可以将onCheckedChange()转到每个选项卡?

有我的代码:

MainActivity

public class MainActivity extends AppCompatActivity {

private ViewPager viewPager;
private TabLayout mTabLayout;

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

private void initViews(){

    viewPager = findViewById(R.id.viewpager);
    mTabLayout =  findViewById(R.id.tabs);
    viewPager.setOffscreenPageLimit(5);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
    mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

    setDynamicFragmentToTabLayout();

}

private void setDynamicFragmentToTabLayout() {
    for (int i = 1; i <11 ; i++) {//listaQuestoes.size()+1

        mTabLayout.addTab(mTabLayout.newTab().setText("test " + i));
    }
    SimuladoFragmentAdapter mDynamicFragmentAdapter = new SimuladoFragmentAdapter(getSupportFragmentManager(), mTabLayout.getTabCount());
    viewPager.setAdapter(mDynamicFragmentAdapter);
    viewPager.setCurrentItem(0);


}

片段

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

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.simulado_fragment, container, false);
    initViews(view);
    toggleButton1 = view.findViewById(R.id.customToggleButton);
    toggleButton2 = view.findViewById(R.id.customToggleButton2);
    toggleButton3 = view.findViewById(R.id.customToggleButton3);
    toggleButton4 = view.findViewById(R.id.customToggleButton4);

    toggleButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


            Toast.makeText(getContext(), "Changed Test", Toast.LENGTH_SHORT).show();
        }
    });
    return view;
}

private void initViews(View view) {

    TextView textView=view.findViewById(R.id.textViewSimuladoFrag);
    textView.setText(String.valueOf("Category :  "+getArguments().getInt("position")));
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
}

@Override
public void onPause() {
    super.onPause();
}

@Override
public void onResume() {
    super.onResume();
}

@Override
public void onStop() {
    super.onStop();
}

@Override
public void onDestroyView() {
    super.onDestroyView();
}

@Override
public void onDestroy() {
    super.onDestroy();
}
}

FragmentAdapter

private int mNumOfTabs;

public SimuladoFragmentAdapter(FragmentManager fm, int NumOfTabs) {
    super(fm);
    this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {
    Bundle b = new Bundle();
    b.putInt("position", position);
    Fragment frag = SimuladoFragment.newInstance();
    frag.setArguments(b);
    return frag;
}

@Override
public int getCount() {
    return mNumOfTabs;
}
}

0 个答案:

没有答案