0

嗨,我正在制作这个应用程序,其中包含两个可以在它们之间滑动的片段。代码编译并运行而不会崩溃,并且显示两个选项卡,您可以在选项卡之间滑动,但选项卡没有内容。我认为这是尝试设置内容视图的片段的问题。如果您需要更多信息,请询问:)

我的主要活动

import java.util.ArrayList;

import org.holoeverywhere.app.Activity;
import org.holoeverywhere.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockActivity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.WindowManager;

public class SwipeTabsMainActivity extends Activity {
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
TextView tabCenter;
TextView tabText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    mViewPager = new ViewPager(this);
    mViewPager.setId(R.id.pager);

    setContentView(mViewPager);
    ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

    mTabsAdapter = new TabsAdapter(this, mViewPager);

        mTabsAdapter.addTab(bar.newTab().setText("Stopwatch"),StopWatchFragment.class, null);
    mTabsAdapter.addTab(bar.newTab().setText("Timer"), CountdownFragment.class, null);



}

public static class TabsAdapter extends FragmentPagerAdapter implements
        ActionBar.TabListener, ViewPager.OnPageChangeListener {
    private final Context mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    static final class TabInfo {
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(Class<?> _class, Bundle _args) {
            clss = _class;
            args = _args;
        }
    }

    public TabsAdapter(Activity activity, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mActionBar = activity.getSupportActionBar();
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mActionBar.addTab(tab);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mTabs.size();
    }

    @Override
    public Fragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(),
                info.args);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        mActionBar.setSelectedNavigationItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        Object tag = tab.getTag();
        for (int i = 0; i < mTabs.size(); i++) {
            if (mTabs.get(i) == tag) {
                mViewPager.setCurrentItem(i);
            }

        }
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }
}
}

主活动的 xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1" />

我的倒计时片段

import org.holoeverywhere.app.Fragment;
import org.holoeverywhere.internal._View;
import org.holoeverywhere.preference.PreferenceManager;
import org.holoeverywhere.preference.SharedPreferences;
import org.holoeverywhere.widget.Button;
import org.holoeverywhere.widget.TextView;
import org.holoeverywhere.widget.Toast;

import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import org.holoeverywhere.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Chronometer;
import android.widget.ImageButton;
import android.widget.Chronometer.OnChronometerTickListener;
import android.widget.RelativeLayout;

public class CountdownFragment extends Fragment implements
    OnChronometerTickListener {
TextView second, hour, minute;
ImageButton hourup, hourdown, minuteup, minutedown, secondup, seconddown;
Button start, stop;
int hourCount, minCount, secCount;
Context c;
Chronometer count;
boolean viberate, running;
Vibrator v;
boolean startstopshow;
SharedPreferences getPrefs;
View fv = getView();
RelativeLayout rl;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    initilize(c);
    setClicker();
    count.start();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
    c = getActivity();




}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.countdown, container, false);
     rl = (RelativeLayout )inflater.inflate(R.layout.countdown, container, false);

    return super.onCreateView(inflater, container, savedInstanceState);
}



private void initilize(Context c) {
    hourup = (ImageButton) rl.findViewById(R.id.hourup);
    minuteup = (ImageButton) rl.findViewById(R.id.minuteup);
    secondup = (ImageButton) rl.findViewById(R.id.secondup);
    hourdown = (ImageButton) rl.findViewById(R.id.hourdown);
    minutedown = (ImageButton) rl.findViewById(R.id.minutedown);
    seconddown = (ImageButton) rl.findViewById(R.id.seconddown);
    start = (Button) rl.findViewById(R.id.start);
    stop = (Button) rl.findViewById(R.id.stop);
    second = (TextView) rl.findViewById(R.id.second);
    minute = (TextView) rl.findViewById(R.id.minute);
    hour = (TextView) rl.findViewById(R.id.hour);
    hourCount = 0;
    minCount = 0;
    secCount = 0;

    count = (Chronometer) rl.findViewById(R.id.chronometer1);
    v = (Vibrator) c.getSystemService(Context.VIBRATOR_SERVICE);
    running = false;
    getPrefs = PreferenceManager.getDefaultSharedPreferences(c);
}

@Override
public void onChronometerTick(Chronometer chronometer) {
    switch (chronometer.getId()) {

    case (R.id.chronometer1):
        secCount--;
        updateDisplay();
        break;
    }
}

}

我的片段 xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="56dp" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/hourup"
            style="?borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/up" />

        <ImageButton
            android:id="@+id/minuteup"
            style="?borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/up" />

        <ImageButton
            android:id="@+id/secondup"
            style="?borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/up" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <org.holoeverywhere.widget.TextView
            android:id="@+id/hour"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="00 : "
            android:textSize="35sp" />

        <org.holoeverywhere.widget.TextView
            android:id="@+id/minute"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="00 : "
            android:textSize="35sp" />

        <org.holoeverywhere.widget.TextView
            android:id="@+id/second"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="00"
            android:textSize="35sp" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/hourdown"
            style="?borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/down" />

        <ImageButton
            android:id="@+id/minutedown"
            style="?borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/down" />

        <ImageButton
            android:id="@+id/seconddown"
            style="?borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/down" />
    </TableRow>
</TableLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_weight="50"
    android:gravity="bottom"
    android:weightSum="100" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_alignParentBottom="true" >

        <org.holoeverywhere.internal._View
            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="1dip"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="4dip"
            android:layout_marginRight="4dip"
            android:background="?dividerVertical" />

        <org.holoeverywhere.internal._View
            android:id="@+id/ViewColorPickerHelper"
            android:layout_width="1dip"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="4dip"
            android:layout_marginTop="4dip"
            android:background="?dividerVertical" />

        <org.holoeverywhere.widget.Button
            android:id="@+id/start"
            style="@style/Holo.Button.Borderless"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/view1"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/stop"
            android:text="Start" />

        <org.holoeverywhere.widget.Button
            android:id="@+id/stop"
            style="@style/Holo.Button.Borderless"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/ViewColorPickerHelper"
            android:text="Stop" />
    </RelativeLayout>
</LinearLayout>

<Chronometer
    android:id="@+id/chronometer1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Chronometer"
    android:textSize="0px"
    android:visibility="invisible" />

</RelativeLayout>

截屏

4

2 回答 2

2

我终于找到了问题:

你必须替换: super.onCreateView(inflater, container, savedInstanceState); 到您的倒计时视图:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.countdown, container, false);
     rl = (RelativeLayout )inflater.inflate(R.layout.countdown, container, false);

    return  v;
}

这是结果的屏幕截图:

在此处输入图像描述

于 2013-01-12T01:26:22.103 回答
0

我可以看到您正在使用 ActionBarSherlock。试试那个图书馆:http: //viewpagerindicator.com/。它将为您提供帮助,并且自定义标签和导航非常容易

您可以在 Google play 上看到很多应用示例:https: //play.google.com/store/apps/details?id=com.viewpagerindicator.sample& feature=search_result#?t=W251bGwsMSwxLDEsImNvbS52aWV3cGFnZXJpbmRpY2F0b3Iuc2FtcGxlIl0 。

这是一个例子:主要活动:

import android.os.Bundle;
import android.support.v4.view.ViewPager;


import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.viewpagerindicator.TabPageIndicator;
public class MainActivity extends SherlockFragmentActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         ViewPager mViewPager = (ViewPager) findViewById(R.id.viewpager); 


         mViewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager()));

            //Bind the title indicator to the adapter
        TabPageIndicator tabIndicator = (TabPageIndicator)findViewById(R.id.titles);
        tabIndicator.setViewPager(mViewPager);


         mViewPager.setCurrentItem(1);
    }


}

片段页面适配器:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
    private Fragment[] fragments;
    private static final String[] CONTENT = new String[] { "frag A","frag B", "frag C" };

    // Reference : https://bitbucket.org/owentech/abstabsviewpager/downloads


    public MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub

        this.fragments = new Fragment[] { new FragmentA(),
                new FragmentB(),
                new FragmentC()};
    }

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub
        return this.fragments[arg0];


    }

    public CharSequence getPageTitle(int position) {
        return CONTENT[position];
    }

    @Override
    public int getCount() {

        return this.fragments.length;

    }


}

片段X:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class FragmentA extends Fragment{

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragmenta, container, false);


        return v;


    }

}

主要的 xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <com.viewpagerindicator.TabPageIndicator
    android:id="@+id/titles" 
    android:theme="@style/Theme.PageIndicatorDefaults"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

清单 xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.viewpagertest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="15" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Sherlock" >
        <activity
            android:name="com.example.viewpagertest.MainActivity"
            android:label="@string/app_name"
             android:theme="@style/Theme.PageIndicatorDefaults" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
于 2013-01-11T23:36:37.893 回答