編輯:關於Android編程
按照官方給出的示例,我寫出來的代碼是這樣的
布局代碼
java代碼
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewPager viewPager = (ViewPager) findViewById(R.id.vp_pager); MyAdapter myAdapter = new MyAdapter(); viewPager.setAdapter(myAdapter); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.addTab(tabLayout.newTab().setText("Tab 1")); tabLayout.addTab(tabLayout.newTab().setText("Tab 2")); tabLayout.addTab(tabLayout.newTab().setText("Tab 3")); tabLayout.addTab(tabLayout.newTab().setText("Tab 4")); tabLayout.setupWithViewPager(viewPager); } class MyAdapter extends PagerAdapter { @Override public int getCount() { return 4; } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView imageView = new ImageView(MainActivity.this); imageView.setBackgroundResource(R.drawable.ic_launcher); container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } } }
然而,看到運行結果的我一臉懵逼.
踏馬達我的字去哪了,我首先想到的就是,是不是ViewPager布局太高了,把Tab蓋住了,然而減少了的ViewPager的高度還是不行,
是不是顏色不對。。。
是不是字號不對…
然後,我又嘗試著注釋掉了這行代碼
tabLayout.setupWithViewPager(viewPager);
雖然Tab和 ViewPagre 失去了關聯,但是最起碼按我的想法展示出來了.
現在已經找到了罪魁禍首,就是 tabLayout.setupWithViewPager(viewPager); 搞的鬼,然後我點進去查看一下源碼,
/** * The one-stop shop for setting up this {@link TabLayout} with a {@link ViewPager}. * *
This method will link the given ViewPager and this TabLayout together so that any * changes in one are automatically reflected in the other. This includes adapter changes, * scroll state changes, and clicks. The tabs displayed in this layout will be populated * from the ViewPager adapter's page titles.
* *After this method is called, you will not need this method again unless you want * to change the linked ViewPager.
* *If the given ViewPager is non-null, it needs to already have a * {@link PagerAdapter} set.
* * @param viewPager The ViewPager to link, or {@code null} to clear any previous link. */ public void setupWithViewPager(@Nullable final ViewPager viewPager) { if (mViewPager != null && mPageChangeListener != null) { // If we've already been setup with a ViewPager, remove us from it mViewPager.removeOnPageChangeListener(mPageChangeListener); } if (viewPager != null) { final PagerAdapter adapter = viewPager.getAdapter(); if (adapter == null) { throw new IllegalArgumentException("ViewPager does not have a PagerAdapter set"); } mViewPager = viewPager; // Add our custom OnPageChangeListener to the ViewPager if (mPageChangeListener == null) { mPageChangeListener = new TabLayoutOnPageChangeListener(this); } mPageChangeListener.reset(); viewPager.addOnPageChangeListener(mPageChangeListener); // Now we'll add a tab selected listener to set ViewPager's current item setOnTabSelectedListener(new ViewPagerOnTabSelectedListener(viewPager)); // Now we'll populate ourselves from the pager adapter setPagerAdapter(adapter, true); } else { // We've been given a null ViewPager so we need to clear out the internal state, // listeners and observers mViewPager = null; setOnTabSelectedListener(null); setPagerAdapter(null, true); } } 貌似沒神魔問題,再看看 setPagerAdapter private void setPagerAdapter(@Nullable final PagerAdapter adapter, final boolean addObserver) { if (mPagerAdapter != null && mPagerAdapterObserver != null) { // If we already have a PagerAdapter, unregister our observer mPagerAdapter.unregisterDataSetObserver(mPagerAdapterObserver); } mPagerAdapter = adapter; if (addObserver && adapter != null) { // Register our observer on the new adapter if (mPagerAdapterObserver == null) { mPagerAdapterObserver = new PagerAdapterObserver(); } adapter.registerDataSetObserver(mPagerAdapterObserver); } // Finally make sure we reflect the new adapter populateFromPagerAdapter(); }貌似沒神魔問題,再看看 setPagerAdapter
private void setPagerAdapter(@Nullable final PagerAdapter adapter, final boolean addObserver) {
if (mPagerAdapter != null && mPagerAdapterObserver != null) {
// If we already have a PagerAdapter, unregister our observer
mPagerAdapter.unregisterDataSetObserver(mPagerAdapterObserver);
}
mPagerAdapter = adapter;
if (addObserver && adapter != null) {
// Register our observer on the new adapter
if (mPagerAdapterObserver == null) {
mPagerAdapterObserver = new PagerAdapterObserver();
}
adapter.registerDataSetObserver(mPagerAdapterObserver);
}
// Finally make sure we reflect the new adapter
populateFromPagerAdapter();
}
貌似也沒什麼問題啊,此時我萬念俱灰,又抱著一絲希望點開看看 populateFromPagerAdapter();
private void populateFromPagerAdapter() {
removeAllTabs();
if (mPagerAdapter != null) {
final int adapterCount = mPagerAdapter.getCount();
for (int i = 0; i < adapterCount; i++) {
addTab(newTab().setText(mPagerAdapter.getPageTitle(i)), false);
}
// Make sure we reflect the currently set ViewPager item
if (mViewPager != null && adapterCount > 0) {
final int curItem = mViewPager.getCurrentItem();
if (curItem != getSelectedTabPosition() && curItem < getTabCount()) {
selectTab(getTabAt(curItem));
}
}
} else {
removeAllTabs();
}
}
握草,
removeAllTabs();
就只這行代碼,竟然在這兒 remove 掉了我之前設置的所有 Tab ,我真是想日了個狗啊,這在官方文檔上沒一點提示啊.
然後我又修改了一下代碼
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.vp_pager);
MyAdapter myAdapter = new MyAdapter();
viewPager.setAdapter(myAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 4"));
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).setText("Tab1");
tabLayout.getTabAt(1).setText("Tab2");
tabLayout.getTabAt(2).setText("Tab3");
tabLayout.getTabAt(3).setText("Tab4");
}
class MyAdapter extends PagerAdapter {
@Override
public int getCount() {
return 4;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setBackgroundResource(R.drawable.ic_launcher);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}
官方參考鏈接: https://developer.android.com/reference/android/support/design/widget/TabLayout.html?utm_campaign=io15&utm_source=dac&utm_medium=blog
我們知道Intent的應用,可以啟動別一個Activity,那麼是否可以啟動別外的一個應用程序呢,答案是可以的。1、首先我們新建一個Android應用,名為Another
我們聊聊我們常寫的登錄界面,這個界面我相信很多人都寫過,而且也沒什麼難度,但是如果要實現比較不一般的效果,那就要花點心思了,先看看項目的效果吧:我一直都不知道怎麼在編輯框
大家看到微信首頁切換效果有沒有覺得很炫,滑動切換,點擊底部bar瞬間切換,滑動切換漸變效果,線上效果圖:之前也在博客上看到別人的實現,再次基礎上,我做了些優化。首先說下實
本教程為大家分享了Android PopupWindow彈出菜單的具體代碼,供大家參考,具體內容如下項目代碼:http://xiazai.jb51.net/201611/