编程语言
首页 > 编程语言> > java-在TabLayout中以编程方式夹住选项卡

java-在TabLayout中以编程方式夹住选项卡

作者:互联网

我是android编程的新手,当用户单击位于tab1中的按钮时,找不到在TabLayout中的tab1和tab2之间切换的方法.
这可能很简单,但是我在第一个应用程序中一无所知.
首先,我尝试了以下方法:

TabLayout.Tab tab = tabLayout.getTabAt(1);
tab.select();

它曾经可以工作,但是我更改了代码,在某些时候,它不再起作用了.
我也尝试过tab.getCustomView().setSelected(true);,但是我得到了NullPointerException.因此,我在if语句中检查了tab是否为null,而事实并非如此.
然后我尝试

tabLayout.setScrollPosition(1,0f,true);
ViewPager viewPager = new ViewPager(mainView.getContext());
viewPager.setCurrentItem(1);

但是,以上任何一种解决方案都不适合我.
这是我的代码:

View view;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    try {
        view = inflater.inflate(R.layout.fragment_main, container, false);
        Button GoB = view.findViewById(R.id.GoB);
        final EditText USETV = view.findViewById(R.id.USETV);
        final EditText commandEV = view.findViewById(R.id.CommandTV);
        final SqlHelper db = new SqlHelper(getContext(), "myDatabase", null, 1);
        GoB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    String a = "";
                    if (USETV.getText().toString().length() > 0) {
                        a += "USE " + USETV.getText().toString() + " ;";
                    }
                    a += commandEV.getText().toString();
                    String[][] c = db.SqlQuery(a);
                    LayoutInflater factory = getLayoutInflater();
                    View resultView = factory.inflate(R.layout.fragment_result, null);
                    TableLayout tableLayout = resultView.findViewById(R.id.ResultContainer);
                    tableLayout.removeAllViews();
                    View mainView = factory.inflate(R.layout.main_activity,null);
                    TabLayout tabLayout = mainView.findViewById(R.id.tabs);
                    if (c[0].length > 0 && c[1].length > 0) {
                        TabLayout.Tab tab = tabLayout.getTabAt(1);
                        //tab.select();
                        //tab.getCustomView().setSelected(true);
                        //tabLayout.setScrollPosition(1,0f,true);
                        //ViewPager viewPager = new ViewPager(mainView.getContext());
                        //viewPager.setCurrentItem(1);
                        //
                        // do some stuff
                    }
                } catch (Exception e) {
                    showException(e);
                }
            }
        });
    } catch (Exception e) {
        showException(e);
    }
    return view;
}

解决方法:

TabLayout和ViewPager属于您的Activity,因此在Fragment中,您必须调用Activity函数来切换Tab

这是一个例子:

public class MyActivity extends Activity {
    private TabLayout mTabLayout;
    private ViewPager mViewPager;

    @Override
    public onCreate(...) {
        MyFrament myFragment = new MyFragment(this);
        // Add fragment to Viewpager ...
        // Attach ViewPager to TabLayout ...
    }

    public void switchTab(int index) {
        // Check index ...
        mViewPager.setCurrentItem(index);
    }
}

然后

public class MyFragment extends Fragment {
    private Context mContext;
    private Button mButton;

    public MyFragment(Context context) {
        mContext = context;
    }

    @Override
    public View onCreateView(...) {
        ...

        mButton.setOnClickListener(v->{
            ((MyActivity)mContext).switchTab(1);
        });

        ...
    }
}

希望对您有所帮助!

标签:android-tablayout,java,android
来源: https://codeday.me/bug/20191025/1925787.html