这是我的活动中onCreate函数的实现.我需要动态创建标签.为简单起见,我制作了两个标签.
public class ActivityProductList extends AppCompatActivity { private android.support.v7.widget.SearchView searchView = null; private RecyclerView recyclerView; MyAdapter adapter; List<ParentListItem> parentListItems = new ArrayList<>(); List<ParentListItem> originalProductList = new ArrayList<>(); String query = null; int categoryId = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_products_final); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setTitle("PRODUCTS"); getSupportActionBar().setDisplayHomeAsUpEnabled(true); //ViewPager viewPager = (ViewPager) findViewById(R.id.pager); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.addTab(tabLayout.newTab().setText("Tab 1")); tabLayout.addTab(tabLayout.newTab().setText("Tab 2")); //setupViewPager(viewPager); //tabLayout.setupWithViewPager(viewPager); //viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); searchView = (android.support.v7.widget.SearchView) findViewById(R.id.searchView); searchView.setIconified(false); searchView.onActionViewExpanded(); searchView.clearFocus(); searchView.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { if (adapter != null) filterData(query); return false; } @Override public boolean onQueryTextChange(String query) { if (adapter != null) filterData(query); return false; } }); } }
我想在点击选项卡时更改编辑文本值. editText处于同一活动中.
如何在活动中获得Tab click事件?
解决方法
您可以使用onTabSelected侦听器
tabLayout.addOnTabSelectedListener(new OnTabSelectedListener() { @Override public void onTabSelected(Tab tab) { switch(tab.getPosition()) { case 0: .... } }
附:不推荐使用setOnTabSelectedListener,请改用addOnTabSelectedListener