我使用ADT为Android 4+版本创建了一个Android项目,并使用" Fixed Tabs + Swipe"创建了一个主要活动。 (使用向导)...但我不需要滑动操作,因此,如何在我的应用中禁用它?它有可能吗?
非常感谢!
答案 0 :(得分:5)
activity_main.xml
中的ViewPager替换为其他内容(如FrameLayout),并将其ID更改为合理的内容,例如@+id/activity_root
这样的事情应该有效。你必须添加逻辑来创建和维护你的片段。
public class MainActivity extends Activity implements ActionBar.TabListener {
private int mFragmentCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// For each of the sections in the app, add a tab to the action bar.
mFragmentCount = 3;
for (int i = 0; i < mFragmentCount; i++) {
// Create a tab with text Also specify this Activity object, which
// implements the TabListener interface, as the callback (listener)
// for when this tab is selected.
actionBar.addTab(actionBar.newTab().setText("Tab " + i).setTabListener(this));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// Switch fragments
// use fragmentTransaction methods with R.id.activity_root for the container id
// don't call commit(), it will be called for you
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}
}
布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />
答案 1 :(得分:3)
我遇到了同样的问题。这是禁用滑动的快速方法。
在activity_main.xml chane ViewPager中,如下所示:
<com.project.android.NoSwipeViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />
然后在项目中创建以下类:
package com.project.android;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class NoSwipeViewPager extends ViewPager {
private boolean enabled;
public NoSwipeViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.enabled = false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onTouchEvent(event);
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onInterceptTouchEvent(event);
}
return false;
}
public void setPagingEnabled(boolean enabled) {
this.enabled = enabled;
}
}
NoSwipeViewPager中的 this.enabled = false; 将禁用滑动。