问题描述
我的工作 Actionbar.TabListener
和片段
标签。
I am working on Actionbar.TabListener
and Fragments
Tab.
片段包含通过点击按钮生成每个标签片段
。
Fragments contains multiple Fragments
that are generated via clickable buttons on each tabbed Fragments
.
我的其他文件显示如下 MainActivity
和
My MainActivity
and other files are shown below:
我的主要活动如下:
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
// Declare Tab Variable
ActionBar.Tab Tab1, Tab2, Tab3;
Fragment fragmentTab1 = new FragmentTab1();
Fragment fragmentTab2 = new Mcx();
Fragment fragmentTab3 = new FragmentTab3();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
// Hide Actionbar Icon
actionBar.setDisplayShowHomeEnabled(false);
// Hide Actionbar Title
actionBar.setDisplayShowTitleEnabled(false);
// Create Actionbar Tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set Tab Icon and Titles
Tab1 = actionBar.newTab().setText("A");
Tab2 = actionBar.newTab().setText("B");
Tab3 = actionBar.newTab().setText("C");
// Set Tab Listeners
Tab1.setTabListener(new TabListener(fragmentTab1));
Tab2.setTabListener(new TabListener(fragmentTab2));
Tab3.setTabListener(new TabListener(fragmentTab3));
// Add tabs to actionbar
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
actionBar.addTab(Tab3);
}
}
和 TabListener
文件延伸ActionBar.Tablistener
:
import android.app.ActionBar.Tab;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ActionBar;
public class TabListener implements ActionBar.TabListener {
Fragment fragment;
public TabListener(Fragment fragment) {
// TODO Auto-generated constructor stub
this.fragment = fragment;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.replace(R.id. fragment_container, fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.remove(fragment);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
activity_main.xml中
配置如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
问:
由于上面我所实现的,我怎么可能去同时实现 ActionTab
和片段视图
作为刷卡视图?
Given what I have implemented above, how could I go about implementing both ActionTab
and Fragment View
as a swipe view?
推荐答案
一些研究谷歌的我发现,我们可以实现刷卡片段与看法与标签使用ViewPager的
After some research on the google i found that we can achieve swipe view in fragments with Tab with the use of ViewPager
您可以进入Android Studio和选择TabActivity(刷卡与ViewPager模板)
Go to Android Studio and select TabActivity (Swipe with ViewPager template)
首先,我们必须与MainActivity延长fragmentAcitvity,并应与ActionBar.TabListener实施
first we have to extend fragmentAcitvity with MainActivity, and that should be implemented with ActionBar.TabListener
在code是如下。
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
@SuppressWarnings("ALL")
public class MainActivity extends FragmentActivity implements ActionBar.TabListener{
ActionBar actionbar;
public static ViewPager viewpager;
FragmentPageAdapter ft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager = (ViewPager) findViewById(R.id.pager);
ft = new FragmentPageAdapter(getSupportFragmentManager());
actionbar = getActionBar();
viewpager.setAdapter(ft);
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.addTab(actionbar.newTab().setText("TAB1").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("TAB2").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("TAB3").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("TAB").setTabListener(this));
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
actionbar.setSelectedNavigationItem(arg0);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewpager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
XML为MainActivity .... activity_mail.xml将...
XML for MainActivity .... activity_mail.xml will be...
<android.support.v4.view.ViewPager 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" />
FragmentpageAdapter
FragmentpageAdapter
public class FragmentPageAdapter extends FragmentPagerAdapter {
public FragmentPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
switch (arg0){
case 0: return new Tab1Fragment();
case 1: return new Tab2Fragment();
case 2: return new Tab3Fragment();
case 3: return new Tab4Fragment();
default:break;
}
return null;
}
@Override
public int getCount() {
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
return getPageTitle(4);
}
}
如果你把通过Android Studio中的默认模板上面提到会有一个fragment_main文件还...
If you take By default Template in Android Studio as mentioned above there will be an fragment_main file also ...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
</RelativeLayout>
在实现与您和宣布,以FragmentPagerAdapter和MainActivity添加相关标签的所有片段。
After that implement all Fragments with related Tabs that you declared and added in FragmentPagerAdapter and MainActivity.
您必须设置在Androidmenifest.xml活动主题
You have to set activity theme in Androidmenifest.xml
"@android:style/Theme.Holo.Light"
和也在style.xml
and also in style.xml
<style name="AppTheme" parent="Theme.AppCompat">
这篇关于我怎么能实现刷卡观ActionTab和片段的多个片段内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!