[英]How can I disable click on TabLayout in Android
我想用TabLayout
在我使用的應用程序fragment
和ViewPager
。
但我想禁用點擊TabLayout
在fragment
之間切換,只需滑動即可在fragment
之間fragmenst
。
我寫了下面的代碼,但對我不起作用,當點擊TabLayout
項目時,轉到那個fragment
!
主要活動 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>
主活動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;
}
});
}
我該如何解決這個問題? 謝謝大家 <3
您在 setupWithViewPager 之前訪問選項卡,這就是您的代碼不起作用的原因。 所以首先設置標簽然后設置touchlistener代碼。
試試這個:
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;
}
});
}
對於ViewPager2 (最低 v1.1.0),您可以在TabLayoutMediator
完成此TabLayoutMediator
。
new TabLayoutMediator(tabLayout, pager,
(tab, position) -> {
tab.view.setClickable(false);
}
).attach();
在build.gradle
:
com.google.android.material:material:1.1.0-rc02
可觸摸數組列表將解決此問題。
申報活動
var touchableList: ArrayList<View>? = ArrayList()
禁用所有選項卡
touchableList = tabLayout?.touchables
touchableList?.forEach { it.isEnabled = false }
啟用選項卡
touchableList?.get(0)?.isEnabled = true
無需使用線性布局,您只需將其添加到活動中即可:
private void setUntouchableTab () {
tablayout.setupWithViewPager(viewpager, true);
tablayout.clearOnTabSelectedListeners();
for (View v : tablayout.getTouchables()) {
v.setEnabled(false);
}
}
tabLayout.clearOnTabSelectedListeners();
這對我有用:
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
}
上面的答案在這里,當您已經完成填充您的選項卡項或已經在 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>
但是,如果您有不同的情況,在使用 Observable 數據或動態 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 片段后禁用其單擊。
如果有人對此有更好的方法,請編輯我的答案
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.