Android 4+ バージョン用の ADT を使用して Android プロジェクトを作成し、「固定タブ + スワイプ」(ウィザードを使用) を使用してメイン アクティビティを作成しましたが、スワイプ アクションは必要ありません。私のアプリ?それが可能だ?
どうもありがとう!
Android 4+ バージョン用の ADT を使用して Android プロジェクトを作成し、「固定タブ + スワイプ」(ウィザードを使用) を使用してメイン アクティビティを作成しましたが、スワイプ アクションは必要ありません。私のアプリ?それが可能だ?
どうもありがとう!
activity_main.xml
もの (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" />
私も同じ問題を抱えていました。これは、スワイプを無効にする簡単な方法です。
activity_main.xml で、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;
}
}
this.enabled = false; NoSwipeViewPager でスワイプを無効にします。