Reputation: 9994
I want to have Press
event listener for my tabLayout.
Based on the tabLayout item which is pressed I want to highlight another view in my layout.
I know we have addOnTabSelectedListener
but I want to have PressedListener
.
Is there any solution?
Upvotes: 1
Views: 338
Reputation: 1093
Get each tab views and set touch listener on each, didnt try it my self pls try and let me knw
val tabStrip = tab.getChildAt(0)
if (tabStrip is ViewGroup) {
val childCount = tabStrip.childCount
for (i in 0 until childCount) {
val tabView = tabStrip.getChildAt(i)
tabView.setOnTouchListener { v, event ->
if (event.action == MotionEvent.ACTION_DOWN){
Log.d("touchEvent","touched $i")
}
return@setOnTouchListener true
}
}
}
in java
final View tabStrip = ((TabLayout) findViewById(R.id.tab)).getChildAt(0);
int childCount = ((ViewGroup) tabStrip).getChildCount();
for (int i = 0; i < childCount; i++) {
View tabView = ((ViewGroup) tabStrip).getChildAt(i);
final int finalI = i;
tabView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Log.d("onTouch", "touched " + finalI);
}
return false;
}
});
Upvotes: 1