我希望在我的应用程序中使用TabLayout
以使用fragment
和ViewPager
。
但我希望禁用点击TabLayout
以在fragment
之间切换,只需轻扫即可在fragmenst
之间切换。
我在下面写了代码,但没有工作,当点击TabLayout
项时,请转到fragment
!
MainActivity XML:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/app_namefull"
android:textSize="23sp" />
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="72dp"
app:tabGravity="fill"
app:tabIndicatorColor="@color/white"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
MainActivity java:
public class Main_Page extends AppCompatActivity {
private CollapsingToolbarLayout mCollapsingToolbarLayout;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main__page);
mCollapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);
//mCollapsingToolbarLayout.setTitle(getResources().getString(R.string.app_name));
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
LinearLayout tabStrip = ((LinearLayout)tabLayout.getChildAt(0));
for(int i = 0; i < tabStrip.getChildCount(); i++) {
tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
}
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
/**
* Adding custom view to tab
*/
private void setupTabIcons() {
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setText(R.string.free_fragment_title);
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_download_image, 0, 0);
tabLayout.getTabAt(0).setCustomView(tabOne);
TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabTwo.setText(R.string.paid_fragment_title);
tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_paid_download_image, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);
TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabThree.setText(R.string.pdf_fragment_title);
tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_pdf_icon, 0, 0);
tabLayout.getTabAt(2).setCustomView(tabThree);
}
/**
* Adding fragments to ViewPager
* @param viewPager
*/
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new free_fragment(), "رایگان ها");
adapter.addFrag(new paid_fragment(), "پرداختی ها");
adapter.addFrag(new pdf_fragment(), "مقالات");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
禁用点击TabLayout
我使用此代码:
LinearLayout tabStrip = ((LinearLayout)tabLayout.getChildAt(0));
for(int i = 0; i < tabStrip.getChildCount(); i++) {
tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
}
我怎么能解决这个问题呢?谢谢所有&lt; 3
答案 0 :(得分:36)
您在setupWithViewPager之前访问选项卡,这就是您的代码无法正常工作的原因。所以首先设置标签然后设置settouchlistener代码。
试试这个:
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
LinearLayout tabStrip = ((LinearLayout)mTabLayout.getChildAt(0));
for(int i = 0; i < tabStrip.getChildCount(); i++) {
tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
}
答案 1 :(得分:9)
对于ViewPager2(最低v1.1.0),您可以在TabLayoutMediator
中完成。
new TabLayoutMediator(tabLayout, pager,
(tab, position) -> {
tab.view.setClickable(false);
}
).attach();
在build.gradle
中:
com.google.android.material:material:1.1.0-rc02
答案 2 :(得分:4)
tabLayout.clearOnTabSelectedListeners();
答案 3 :(得分:2)
无需使用线性布局,您可以将其添加到活动中:
private void setUntouchableTab () {
tablayout.setupWithViewPager(viewpager, true);
tablayout.clearOnTabSelectedListeners();
for (View v : tablayout.getTouchables()) {
v.setEnabled(false);
}
}
答案 4 :(得分:0)
The above answer here,在您完成填充选项卡项目或已在xmls上定义选项卡时,禁用了选项卡项目单击。
例如:
<android.support.design.widget.TabLayout
android:id="@+id/tabla_quiz_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:text="1"
android:layout_height="wrap_content"/>
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:text="2"
android:layout_height="wrap_content"/>
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:text="3"
android:layout_height="wrap_content"/>
</android.support.design.widget.TabLayout>
但是,如果情况不同,则使用可观察数据或动态Tabitem。例如:
// QuizFragment.kt
quizListLive.observe(this@QuizFragment, Observer { quizList ->
quizList?.takeIf { list -> list.isNotEmpty() }?.let {
// this is where new fragment added
it.forEachIndexed { j, quiz ->
mViewPagerAdapter?.addFragment(
fragment = QuizFragmentSub.newInstance(quiz = quiz),
title = (j + 1).toString()
)
}
// notify the viewpager, I'm using kotlin extension to call widget id
vp_quiz_content.adapter?.notifyDataSetChanged()
// then, childCount will have size > 0
// disable tablayout click
val tabStrip = tabla_quiz_navigation.getChildAt(0) as LinearLayout
for (i in 0 until tabStrip.childCount) {
tabStrip.getChildAt(i).setOnTouchListener { _, _ -> true }
}
}
})
====
// quiz_fragment.xml
<android.support.design.widget.TabLayout
android:id="@+id/tabla_quiz_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.design.widget.TabLayout>
该方法的调用时间略有不同,您只需要设置Tabitem即可在添加完所有viewpager片段后禁用其单击。
如果有人对此有更好的处理方法,请修改我的答案
答案 5 :(得分:0)
可触摸数组列表将解决此问题。
宣布活动
var touchableList: ArrayList<View>? = ArrayList()
禁用所有标签
touchableList = tabLayout?.touchables
touchableList?.forEach { it.isEnabled = false }
启用标签
touchableList?.get(0)?.isEnabled = true
答案 6 :(得分:0)
这对我有用:
class LockableTabLayout : TabLayout {
var swipeable: Boolean = true
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
override fun onInterceptTouchEvent(event: MotionEvent): Boolean = !swipeable
}