我已多次尝试将两个片段添加到一个操作选项卡。当我按下操作选项卡“搜索”时,我想加载一个片段,当我滑动它将转到第二个“操作”选项卡下的片段。请在我使用的代码下面找到。
MainActivity.java
package com.example.swipetabs;
import com.tabs.*;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.FragmentTransaction;
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.view.ViewPager;
import android.view.Menu;
import android.widget.ImageView;
import com.comparenow.*;
public class MainActivity extends FragmentActivity implements TabListener {
ActionBar action_bar;
ViewPager viewPager;
ImageView creditCards;
Fragment fragement;
Fragment fragement1;
Fragment fragement2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager=(ViewPager)findViewById(R.id.pager);
viewPager.setAdapter(new MyAdoptor(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
action_bar.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
}
});
action_bar=getActionBar();
//action_bar.setBackgroundDrawable(d)
action_bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1=action_bar.newTab();
tab1.setText("Login");
tab1.setTabListener(this);
ActionBar.Tab tab2=action_bar.newTab();
tab2.setText("Compare Now");
tab2.setTabListener(this);
ActionBar.Tab tab3=action_bar.newTab();
tab3.setText("Search");
tab3.setTabListener(this);
action_bar.addTab(tab1);
action_bar.addTab(tab2);
action_bar.addTab(tab3);
}
public void switchToFragmentB(){
viewPager.setCurrentItem(4);
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
//this is the code
viewPager.setCurrentItem(tab.getPosition());
String name=(String)tab.getText().toString();
// compare it to the tab you would like to do something with
if (name == "Search") {
//load the 1st fragment
fragement1= new CreditCards();
getFragmentManager().beginTransaction()
.setCustomAnimations(android.R.animator.fade_in,
android.R.animator.fade_out)
.replace(R.id.store_fragment_container, fragment).commit();
//load the 2nd fragment
fragement2 = new Loginfrag();
getFragmentManager().beginTransaction()
.setCustomAnimations(android.R.animator.fade_in,
android.R.animator.fade_out)
.replace(R.id.imageView1,fragement2).commit();
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
class MyAdoptor extends FragmentPagerAdapter
{
public MyAdoptor(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
Fragment fragement=null;
if(arg0==0)
{
fragement=new Loginfrag();
}
if(arg0==1)
{
fragement=new Comparefrag();
}
if(arg0==2)
{
fragement=new Searchfrag();
}
System.out.print("<><><><>");
return fragement;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
CreditCards.java(Fragment)/ Loginfrag .java(Fragement)两者都是相同的,它也有相应的xml文件。
package com.comparenow;
import com.example.swipetabs.R;
import com.example.swipetabs.R.layout;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class CreditCards extends Fragment {
public CreditCards() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.creditcards_c, container, false);
}
我的代码段有问题,我不确定它的参数。
fragement1= new CreditCards();
getFragmentManager().beginTransaction()
.setCustomAnimations(android.R.animator.fade_in,
android.R.animator.fade_out)
.replace(R.id.store_fragment_container, fragment).commit();
//load the 2nd fragment
fragement2 = new Loginfrag();
getFragmentManager().beginTransaction()
.setCustomAnimations(android.R.animator.fade_in,
android.R.animator.fade_out)
.replace(R.id.imageView1,fragement2).commit();
}
答案 0 :(得分:1)
一般来说,我相信您要找的是在ViewPager
内的Fragment
内创建ViewPager
。
您的MainActivity
或多或少是正确的,但您应该忘记viewPager.setCurrentItem(tab.getPosition())
中onTabSelected()
下面的所有内容。您不必在寻呼机适配器的Searchfrag
方法中使用getItem()
,而是需要为Fragment
和其他Searchfrag
创建和使用父Fragment
你想在搜索标签中滚动到。这个Fragment
可以非常简单,只包含ViewPager
和寻呼机适配器。在此父Fragment's
页面转换器适配器中,您将编写getItem()
方法以返回项目0的Searchfrag
以及您要滚动到项目1的任何其他片段。这两个Fragment's
方法FragmentPagerAdapter
1}}看起来就像是在“搜索”标签下。
例如,保留Searchfrag
的{{1}}和其他Fragment
应该采用getItem()
方法,如下所示:
public static class SampleFragmentAdapter extends FragmentPagerAdapter {
public SampleFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
if(arg0 == 0){
return new Searchfrag();
} else {
return new OtherFragmentUnderSearchTab();
}
}
@Override
public int getCount() {
return 2;
}
}
此父Fragment
(我们称之为ParentFragment
)应替换Searchfrag
getItem()
方法中的MyAdoptor
,如下所示:
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
Fragment fragement=null;
if(arg0==0)
{
fragement=new Loginfrag();
}
if(arg0==1)
{
fragement=new Comparefrag();
}
if(arg0==2)
{
fragement=new ParentFragment();
}
System.out.print("<><><><>");
return fragement;
}