我有一个ViewPager工具栏标签。
我必须知道有多少用户点击了Tab,以及有多少用户滑动并选择了一个页面。对于Google Analytics跟踪
我正在使用ViewPager.OnPageChangeListener()来实现此目的。
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override public void onPageSelected(int position) {
// Here i am sending the GA event
}
@Override public void onPageScrollStateChanged(int state) {
}
});
对页面的点击和滑动调用OnPageSelected。 如何区分所选页面是单击选项卡还是从轻扫Viewpager?
答案 0 :(得分:2)
这是我的解决方案。我基于单个变量。
public class MainActivity extends AppCompatActivity {
// remember last action
private Action lastAction = Action.RESET;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ...
mViewPager = findViewById(R.id.viewPager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
// No-op
}
@Override
public void onPageSelected(int i) {
if (lastAction == Action.RESET) {
lastAction = Action.SWIPE;
Log.d(TAG, "onPageSelected: SWIPED");
} else {
lastAction = Action.RESET;
}
}
@Override
public void onPageScrollStateChanged(int i) {
// No-op
}
});
mTabLayout = findViewById(R.id.tabLayout);
mTabLayout.setupWithViewPager(mViewPager);
mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
if (lastAction == Action.RESET) {
lastAction = Action.SELECT;
Log.d(TAG, "onPageSelected: SELECTED");
} else {
lastAction = Action.RESET;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
// No-op
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
// No-op
}
});
}
}
限制:
在添加侦听器之前,必须先加载viewPager的内容,因为此解决方案基于有序调用(onTabSelected,onPageSelected)。
预览:
答案 1 :(得分:2)
在此示例中,我将检查用户是否通过滑动或点击标签来选择索引1处的页面:
注意:您可以使用tabLayout.getChildAt(0)来获取主要的滑动布局,并且
((ViewGroup) tabLayout.getChildAt(0)).getChildAt(desiredPosition) //to get the tab at desired position
使用此方法,我们将onClickListener添加到所需的选项卡,然后单击它,将首先调用其onClick()方法,然后再调用TabLayout的TabSelectedListener的onTabSelected()方法。
private Boolean tabClicked = false; //variable which determines after entering the onTabSelected() method, if onClick was called or not
((ViewGroup) tabLayout.getChildAt(0)).getChildAt(1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tabClicked = true;
}
});
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
if(tab.getPosition()==1){
if(tabClicked){
//your tab was clicked, do work here
}
else{
//your tab was swiped, do some work here
}
tabClicked = false;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {}
@Override
public void onTabReselected(TabLayout.Tab tab) {}
});
答案 2 :(得分:0)
您使用的tabview是一个textview。所以这个textview会有onclicklistener。您可以跟踪选项卡单击onClick()方法的监听器!
答案 3 :(得分:0)
尝试一下
fun onTabClickedListener(callback: String.() -> Unit){
tabContainer?.let { tabContainer->
for (i in 0 until tabContainer.tabCount) {
val text = tabContainer.getTabAt(i)?.text.toString() ?: ""
(tabContainer.getChildAt(0) as ViewGroup).getChildAt(i).tag = text
(tabContainer.getChildAt(0) as ViewGroup).getChildAt(i).setOnClickListener {
callback(text)
}
}
}
}```