我正在使用tablayout并在onTabSelected()中,我试图操纵同一活动中存在的视图的可见性,但它不起作用。
以下是代码:
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
Log.v("tabSelectCount","count: "+ tab.getPosition());
if(tab.getPosition()==0) {
viewDetail.setVisibility(View.VISIBLE);
viewWishlist.setVisibility(View.GONE);
viewSwap.setVisibility(View.GONE);
} else if(tab.getPosition()==1){
viewDetail.setVisibility(View.GONE);
viewWishlist.setVisibility(View.VISIBLE);
viewSwap.setVisibility(View.GONE);
} else {
viewDetail.setVisibility(View.GONE);
viewWishlist.setVisibility(View.GONE);
viewSwap.setVisibility(View.VISIBLE);
}
}
答案 0 :(得分:0)
运行下面的代码,打开导航抽屉,在里面你会看到两个按钮,在导航抽屉打开时更改标签,并按照内部按钮可见性的变化进行操作。
Demo12:------------
public class Demo12 extends AppCompatActivity {
private ViewPager vp;
private TabLayout tl;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout dl;
private TabPagerAdapter adapter;
private Button b;
private Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo12);
initialize();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}else {
return super.onOptionsItemSelected(item);
}
}
private void initialize() {
tl = (TabLayout) findViewById(R.id.tl);
tl.addTab(tl.newTab().setIcon(
android.R.drawable.ic_dialog_email));
tl.addTab(tl.newTab().setIcon(
android.R.drawable.ic_dialog_dialer));
tl.addTab(tl.newTab().setIcon(
android.R.drawable.ic_dialog_alert));
vp =
(ViewPager) findViewById(R.id.vp);
adapter = new TabPagerAdapter(getSupportFragmentManager(), tl.getTabCount(), this);
vp.setAdapter(adapter);
vp.setOffscreenPageLimit(1);
b = (Button) findViewById(R.id.b);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_LONG).show();
}
});
b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Clicked1",Toast.LENGTH_LONG).show();
}
});
vp.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tl));
tl.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
vp.setCurrentItem(tab.getPosition(), true);
if(tab.getPosition() == 0) {
b.setVisibility(View.VISIBLE);
b1.setVisibility(View.VISIBLE);
}else if(tab.getPosition() == 1){
b.setVisibility(View.VISIBLE);
b1.setVisibility(View.GONE);
}else{
b.setVisibility(View.GONE);
b1.setVisibility(View.VISIBLE);
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
dl = (DrawerLayout) findViewById(R.id.dl);
mDrawerToggle = new ActionBarDrawerToggle(this, dl, R.string.open,R.string.close) {
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu();
}
public void onDrawerClosed(View view) {
invalidateOptionsMenu();
}
};
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle.setDrawerIndicatorEnabled(true);
dl.addDrawerListener(mDrawerToggle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
TabPagerAdapter:---------------
public class TabPagerAdapter extends FragmentStatePagerAdapter {
private int tabCount;
private FragmentManager mFragmentManager;
private Context context;
public TabPagerAdapter(FragmentManager fm) {
super(fm);
}
public TabPagerAdapter(FragmentManager fm, int numberOfTabs , Context context) {
super(fm);
mFragmentManager = fm;
this.tabCount = numberOfTabs;
this.context = context;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return fragment_i.newInstance();
case 1:
return fragment_i.newInstance();
case 2:
return fragment_i.newInstance();
default:
return null;
}
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
return super.instantiateItem(container, position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}
@Override
public int getCount() {
return tabCount;
}
}
fragment_i:---------
public class fragment_i extends Fragment {
public fragment_i() {
}
public static fragment_i newInstance() {
return new fragment_i();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_i, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
fragment_i.xml:------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light"
android:orientation="horizontal">
</LinearLayout>
demo12.xml:----------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tl"
android:layout_width="match_parent"
android:layout_height="50dp"
app:tabMode="fixed"
app:tabGravity="fill"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:tabTextColor="#FFF"
app:tabSelectedTextColor="@color/colorAccent"
/>
<android.support.v4.widget.DrawerLayout
android:id="@+id/dl"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
<RelativeLayout
android:layout_width="240dp"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_light"
android:layout_gravity="start">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
android:textSize="30sp"
android:layout_centerInParent="true"
android:background="@android:color/transparent"
android:textColor="@android:color/holo_blue_light"
android:id="@+id/b"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click1"
android:textSize="30sp"
android:layout_centerInParent="true"
android:background="@android:color/transparent"
android:textColor="@android:color/holo_blue_light"
android:layout_below="@id/b"
android:id="@+id/b1"/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>