简体   繁体   English

通过按钮从一个选项卡视图移动到另一个视图单击Android ActionBar

[英]Moving From one tab view to another by button Click Android ActionBar

I am using http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/ to make Tab bar. 我正在使用http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/来制作Tab栏。

I have also 3 tab first and the second Tab view contains a List view and third on e is Settings. 我也有3个标签页,第二个标签页视图包含一个列表视图,第三个标签页是“设置”。
Settings Contain Reload button. 设置包含重新加载按钮。 When I clicked the Reload Button I want to move to First Tab. 当我单击“重新加载”按钮时,我想移至“第一选项卡”。

How can I do this ? 我怎样才能做到这一点 ?

Make following changes: 进行以下更改:

  1. Pass the viewPager reference to your adapter and from adapter to your fragment: 将viewPager引用传递给适配器,并将适配器传递给片段:

      public class TabsPagerAdapter extends FragmentPagerAdapter { ViewPager viewPager; public TabsPagerAdapter(FragmentManager fm,ViewPager viewPager) { super(fm); this.viewPager=viewPager; } @Override public Fragment getItem(int index) { switch (index) { case 0: // Top Rated fragment activity return new TopRatedFragment(viewPager); case 1: // Games fragment activity return new GamesFragment(viewPager); case 2: // Movies fragment activity return new MoviesFragment(viewPager); } return null; } @Override public int getCount() { // get item count - equal to number of tabs return 3; } } 
  2. In fragment on click of button call set selection on view pager object: 在视图寻呼机对象上单击按钮调用集选择的片段中:

      public class MoviesFragment extends Fragment { private ViewPager viewPager; public MoviesFragment (ViewPager viewPager;){ this.viewPager=viewPager; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_movies, container, false); rootView.findViewById(<your button>).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { viewPager.setCurrentItem(0); } }); return rootView; } } 

I know its quite late ....maybe it can help someone else...creating a constructor in fragments is not supported..so instead of passing viewpager as constructor.. we can write in the fragment on button click: 我知道它已经很晚了....也许它可以帮助其他人...不支持在片段中创建构造函数..因此,与其将viewpager作为构造函数传递..我们可以在单击按钮时在片段中编写:

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

   /**
   * Created by medha singh on 6/17/2016.
    */
    public class Fragment4 extends Fragment {


   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,                  Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    final View view1 = inflater.inflate(R.layout.new_lead2, container, false);
    TextView tv=(TextView)view1.findViewById(R.id.hardware);
    final ViewPager pager=   (ViewPager)getActivity().findViewById(R.id.pager2);
    tv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         pager.setCurrentItem(1);


        }
    });
    return view1;
   }

   }

So getactivity() is the solution: i got it from here: How to go other Tabs by clicking on a Button from the current Tab in Android? 因此,getactivity()是解决方案:我是从这里得到的: 如何通过单击Android当前选项卡中的按钮来转到其他选项卡?

Do this: 做这个:

btn.setOnClickListener(new OnClickListener()
{
onClick(View v)
{
//Reload the data
tabHost.setCurrentTab(/*destination-tab-id*/);
}
}
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android操作栏选项卡+滑动视图分页器从一个选项卡动态导航到另一个选项卡 - Android actionbar tabs + swipe view pager navigate from one tab to another dynamically 单击按钮以切换到Android操作栏中的其他选项卡 - Click button to switch to other tab in actionbar in Android 在Android中将视图从一个网格视图移动到另一个 - Moving a view from one gridview to another in Android 按钮单击Tab View Android - Button Click on Tab View Android 通过单击列表视图中创建的卡片,从一项活动转移到另一项活动 - Moving from one activity to another by click on card created in a list view 如何通过单击Android中的按钮从一个选项卡切换到另一个选项卡? - How to switch from one tab to another tab by clicking on button in Android? 使用ActionBar.TabListener将数据从一个选项卡传递到另一个选项卡 - Passing data from one tab to another using ActionBar.TabListener 如何通过单击选项卡更改操作栏菜单按钮 - How to change actionbar menu button with click on tab 从Android ActionBar隐藏选项卡 - Hide Tab from Android ActionBar Android从ActionBar按钮单击启动自定义辅助功能事件 - Android fire a custom accessibility event from an ActionBar button click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM