ViewPager
ile FragmentTabHost
kullanarak bir kaydırma sekmesi arabirimi oluşturmak istiyorum. Şimdiye kadar kaymalarını Fragmanlar aldım. Olan tek şey, parçaların engelli/takılı olan ve çalışmayan sekmelerin arkasına kayabilmesidir. FragmentTabHost sekmelerini sekmeler arasında kaydırmak için ViewPager ile nasıl çalıştırabilirim?ViewPager ile bir FragmentTabHost Kullanarak
FragmanTabHost'u viewpager kullanmadan denedim, gayet iyi çalışıyor, ancak sadece viewpager ile çalışmıyor.
Ana Java kaynak kodu:
import java.util.List;
import java.util.Vector;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
private ViewPager pager;
private MyPagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
pager = (ViewPager) findViewById(R.id.viewpager);
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, BatteryFragment.class.getName()));
fragments.add(Fragment.instantiate(this, DeviceFragment.class.getName()));
fragments.add(Fragment.instantiate(this, NetworkFragment.class.getName()));
adapter = new MyPagerAdapter(getSupportFragmentManager(), fragments);
pager.setAdapter(adapter);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("battery").setIndicator("Battery",
getResources().getDrawable(R.drawable.ic_battery_tab)),
BatteryFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("network").setIndicator("Network",
getResources().getDrawable(R.drawable.ic_network_tab)),
NetworkFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("device").setIndicator("Device",
getResources().getDrawable(R.drawable.ic_device_tab)),
DeviceFragment.class, null);
}
public class MyPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return this.fragments.get(arg0);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return this.fragments.size();
}
}
}
XML kodu:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
Nasıl uyguladınız? Viewpager ile fragmenttabhost yapmak istiyorum ama herhangi bir örnek bulamıyorum. –