在我的android應用上使用Devlight NavigationTabBar。這裏是整個代碼:在ViewPager中打開新視圖
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_horizontal_ntb);
initUI();
}
private void initUI() {
final ViewPager viewPager = (ViewPager) findViewById(R.id.vp_horizontal_ntb);
viewPager.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
return 5;
}
@Override
public boolean isViewFromObject(final View view, final Object object) {
return view.equals(object);
}
@Override
public void destroyItem(final View container, final int position, final Object object) {
((ViewPager) container).removeView((View) object);
}
@Override
public Object instantiateItem(final ViewGroup container, final int position) {
final View viewNews = LayoutInflater.from(
getBaseContext()).inflate(R.layout.item_vp_list, null, false);
final View ViewSol = LayoutInflater.from(
getBaseContext()).inflate(R.layout.activity_sol, null, false);
final View viewProfile = LayoutInflater.from(
getBaseContext()).inflate(R.layout.activity_profile, null, false);
final View viewContact = LayoutInflater.from(
getBaseContext()).inflate(R.layout.activity_contact, null, false);
View finalView = null;
if (position == 0) {
final RecyclerView recyclerView = (RecyclerView) viewNews.findViewById(R.id.rv);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(
getBaseContext(), LinearLayoutManager.VERTICAL, false
)
);
recyclerView.setAdapter(new RecycleAdapter());
container.addView(viewNews);
finalView = viewNews;
} else if (position == 1) {
container.addView(viewSol);
finalView = viewSol;
} else if (position == 2) {
container.addView(viewProfile);
finalView = viewProfile;
} else if (position == 3){
container.addView(viewContact);
finalView = viewContact;
}
return finalView;
}
});
final String[] colors = getResources().getStringArray(R.array.default_preview);
final NavigationTabBar navigationTabBar = (NavigationTabBar) findViewById(R.id.ntb_horizontal);
final ArrayList<NavigationTabBar.Model> models = new ArrayList<>();
models.add(
new NavigationTabBar.Model.Builder(
getResources().getDrawable(R.drawable.ic_library),
Color.parseColor(colors[0]))
.selectedIcon(getResources().getDrawable(R.drawable.ic_library))
.title("News")
.badgeTitle("+10")
.build()
);
models.add(
new NavigationTabBar.Model.Builder(
getResources().getDrawable(R.drawable.ic_error_black_24dp),
Color.parseColor(colors[1]))
// .selectedIcon(getResources().getDrawable(R.drawable.ic_eighth))
.title("Solicitation")
//.badgeTitle("with")
.build()
);
models.add(
new NavigationTabBar.Model.Builder(
getResources().getDrawable(R.drawable.ic_person_black_24dp),
Color.parseColor(colors[3]))
// .selectedIcon(getResources().getDrawable(R.drawable.ic_eighth))
.title("My Account")
//.badgeTitle("icon")
.build()
);
models.add(
new NavigationTabBar.Model.Builder(
getResources().getDrawable(R.drawable.ic_phone_black_24dp),
Color.parseColor(colors[2]))
.selectedIcon(getResources().getDrawable(R.drawable.ic_phone_black_24dp))
.title("Contact")
//.badgeTitle("state")
.build()
);
navigationTabBar.setModels(models);
navigationTabBar.setViewPager(viewPager, 2);
navigationTabBar.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(final int position, final float positionOffset, final int positionOffsetPixels) {
}
@Override
public void onPageSelected(final int position) {
navigationTabBar.getModels().get(position).hideBadge();
}
@Override
public void onPageScrollStateChanged(final int state) {
}
});
navigationTabBar.postDelayed(new Runnable() {
@Override
public void run() {
for (int i = 0; i < navigationTabBar.getModels().size(); i++) {
final NavigationTabBar.Model model = navigationTabBar.getModels().get(i);
navigationTabBar.postDelayed(new Runnable() {
@Override
public void run() {
//model.showBadge();
}
}, i * 100);
}
}
}, 500);
}
正如你所看到的,每當我選擇一個標籤,它就會在視圖尋呼機中打開視圖。
但是,我不知道如何打開另一個視圖,當我點擊一個按鈕。我試圖用意圖開始一個全新的活動,但它崩潰,並不是我想要的。
我要的是打開視圖上viewpager ViewPager viewPager = (ViewPager) findViewById(R.id.vp_horizontal_ntb);
如果我實現這個:
public void openNewView(View view) {
//the code to open a view inside the viewpager should go here
}
我如何進行?