我正在设计类似功能的标签视图,但我还需要一个功能。
假设我有三个标签
如果按Tab2 - 将显示第二个标签的内容 tab3 - 将显示第三个内容 tab1 - tab1的内容将显示
并默认选择tab1。这工作正常。
现在,我需要在浏览tab1的内容时,我需要显示第二个选项卡的强大功能(但应选择第二个选项卡)..就像网页上的单页应用程序一样..
我不需要网络视图。任何人都指导我如何实现这一点,或者如果github上有任何示例代码可用。请
由于
答案 0 :(得分:1)
@AnkitaKashyap对不起伙计,我一直都忘记了,这是你的代码片段(在kotlin中),如果你需要帮助,请给我一个私人信息,如果我的想法正确,这就是你想要的gif demo : 使用带有recyclerview的tabbar并设置addOnTabSelectedListener:
productTabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
override fun onTabReselected(tab: TabLayout.Tab?) {
}
override fun onTabUnselected(tab: TabLayout.Tab?) {
}
override fun onTabSelected(tab: TabLayout.Tab?) {
productRv.scrollToPosition(tab?.position?:0)
}
})
并在您的recyclerview中设置:
productRv.addOnScrollListener(object : RecyclerView.OnScrollListener(){
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
var scrollPosition = layoutManager.findFirstVisibleItemPosition()
productTabs.getTabAt(scrollPosition)?.select()
}
override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
}
})
答案 1 :(得分:0)
您需要使用此StackOverflow帖子中显示的VerticalViewPager,并使用TabLayout。
来自给定SO链接的代码:
/**
* Uses a combination of a PageTransformer and swapping X & Y coordinates
* of touch events to create the illusion of a vertically scrolling ViewPager.
*
* Requires API 11+
*
*/
public class VerticalViewPager extends ViewPager {
public VerticalViewPager(Context context) {
super(context);
init();
}
public VerticalViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
// The majority of the magic happens here
setPageTransformer(true, new VerticalPageTransformer());
// The easiest way to get rid of the overscroll drawing that happens on the left and right
setOverScrollMode(OVER_SCROLL_NEVER);
}
private class VerticalPageTransformer implements ViewPager.PageTransformer {
@Override
public void transformPage(View view, float position) {
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 1) { // [-1,1]
view.setAlpha(1);
// Counteract the default slide transition
view.setTranslationX(view.getWidth() * -position);
//set Y position to swipe in from top
float yPosition = position * view.getHeight();
view.setTranslationY(yPosition);
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
}
/**
* Swaps the X and Y coordinates of your touch event.
*/
private MotionEvent swapXY(MotionEvent ev) {
float width = getWidth();
float height = getHeight();
float newX = (ev.getY() / height) * width;
float newY = (ev.getX() / width) * height;
ev.setLocation(newX, newY);
return ev;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev){
boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
swapXY(ev); // return touch coordinates to original reference frame for any child views
return intercepted;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return super.onTouchEvent(swapXY(ev));
}
}