目前,我正在尝试将带图标的SlidingTab实现为tabtitle,然后在选择相应的标签时突出显示图标。
我在以下教程中使用了Google的SlidingTabLayout,并且也制作了图标而不是tabtitle。 https://www.youtube.com/watch?v=WSaSNX5QI-E
我知道在哪里可以通过实现OnPageChangeListener获取当前标签位置,但我不知道应该在public void onPageSelected(int position){}中添加什么 这是我目前的状态和代码,请提供建议,如果您尽可能详细地保留说明,我们将不胜感激,因为我是一名非常初学的Android开发人员。
感谢
MainActivity.java
.centralwidget
}
ViewPagerAdapter.java
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
// Declaring Your View and Variables
private Toolbar toolbar;
private ViewPager mPager;
private SlidingTabLayout mTabs;
private ViewPagerAdapter mAdapter;
private static Context context;
int brightIcons[]={R.drawable.abc_btn_switch_to_on_mtrl_00012,R.drawable.abc_btn_switch_to_on_mtrl_00012,R.drawable.abc_btn_switch_to_on_mtrl_00012,R.drawable.abc_btn_switch_to_on_mtrl_00012,R.drawable.abc_btn_switch_to_on_mtrl_00012,};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPager = (ViewPager) findViewById(R.id.pager);
mAdapter = new ViewPagerAdapter(getSupportFragmentManager(), getApplicationContext());
mPager.setAdapter(mAdapter);
mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
mTabs.setOnPageChangeListener(this);
mTabs.setCustomTabView(R.layout.custom_tab_view, R.id.tabText);
mTabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabsScrollColor);
}
});
mTabs.setViewPager(mPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
Toast.makeText(this, Integer.toString(position), Toast.LENGTH_LONG).show();
}
@Override
public void onPageScrollStateChanged(int state) {
}
}