在ViewPager中打开新视图 [英] Opening new view inside ViewPager
问题描述
在我的Android应用中使用 Devlight NavigationTabBar .这是整个代码:
Using Devlight NavigationTabBar on my android app. Here's the whole code:
@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);
}
如您所见,每当我选择一个标签时,它都会在视图分页器中打开视图.
As you can see, whenever I choose a tab it opens the view inside the view pager.
但是,我不知道如何在单击按钮时打开另一个视图.我试图有意识地开始一个全新的活动,但是它崩溃了,这并不是我真正想要的.
However, I have no idea how to open another view when I click on a button. I tried to start a whole new activity with intent but it crashes and it's not really what I want.
我想要在viewpager上打开视图 ViewPager viewPager =(ViewPager)findViewById(R.id.vp_horizontal_ntb);
What I want is to open the view on the 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
}
我该如何进行?
推荐答案
要在单击某个按钮时打开新视图,您可以执行以下操作:
To open a new view on click of some button you can do this:
假设您想在第一个按钮上有一个新视图,然后在PagerAdapter的 instantiateItem
方法中,您可以检查位置是否为0.
Suppose that you want to have a new view on the first button, then in the method instantiateItem
of PagerAdapter you can check if position is 0 or not.
如果位置为0,则可以为要显示的布局充气.
If the position if 0, then you can inflate that layout which you want to display.
类似这样的东西:
@Override
public Object instantiateItem(final ViewGroup container, final int position) {
if (position == 0)
{
final View view = LayoutInflater.from(
getBaseContext()).inflate(R.layout.your_layout, null, false);
final TextView txtPage = (TextView) view.findViewById(R.id.txt_vp_item_page);
txtPage.setText(String.format("Page #%d", position));
container.addView(view);
return view;
}
else if (position == 1)
{
final View view = LayoutInflater.from(
getBaseContext()).inflate(R.layout.item_vp, null, false);
final TextView txtPage = (TextView) view.findViewById(R.id.txt_vp_item_page);
txtPage.setText(String.format("Page #%d", position));
container.addView(view);
return view;
}
else
{
final View view = LayoutInflater.from(
getBaseContext()).inflate(R.layout.item_vp, null, false);
final TextView txtPage = (TextView) view.findViewById(R.id.txt_vp_item_page);
txtPage.setText(String.format("Page #%d", position));
container.addView(view);
return view;
}
}
要使代码更具模块化,您可以使用make自定义片段以及它们在适配器中的方式.
To make your code more modular you can use make your custom fragments and and them in your adapter.
希望这会有所帮助
这篇关于在ViewPager中打开新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!