gpt4 book ai didi

java - 安卓开发 : Directing to specific tabs in fragment

转载 作者:搜寻专家 更新时间:2023-11-01 09:45:03 24 4
gpt4 key购买 nike

这就是 Main Activity 的方式好像。通过单击主要 Activity 中的每个卡片 View ,我将转到 Activity two它包含带有滑动选项卡布局和 recyclerview 的 fragment 。我遇到的问题是试图将每个 cardview 定向到第二个 Activity 中它自己的特定选项卡,而不是第一个默认选项卡。我尝试在网上查看解决方案,但没有太大帮助。任何形式的帮助将不胜感激,提前致谢。

澄清一下,从第一个主要 Activity 到第二个 Activity 的 Intent 调用是通过适配器完成的。

这是主要 Activity

public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private CoverAdapter adapter;
private List<Covers> coversList;

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

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

initCollapsingToolbar();

recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

coversList = new ArrayList<>();
adapter = new CoverAdapter(this, coversList);

RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);

prepareCovers();

try {
Glide.with(this).load(R.drawable.cover).into((ImageView) findViewById(R.id.backdrop));
} catch (Exception e) {
e.printStackTrace();
}


}

private void prepareCovers() {
int[] covers = new int[]{
R.drawable.school};
Covers academiaCover = new Covers("Universities",covers[0]);
coversList.add(academiaCover);

academiaCover = new Covers("Colleges", covers[0]);
coversList.add(academiaCover);

academiaCover = new Covers("School", covers[0]);
coversList.add(academiaCover);

academiaCover = new Covers("Others", covers[0]);
coversList.add(academiaCover);

adapter.notifyDataSetChanged();


}

private void initCollapsingToolbar() {
final CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle(" ");
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
appBarLayout.setExpanded(true);

// hiding & showing the title when toolbar expanded & collapsed
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
boolean isShow = false;
int scrollRange = -1;

@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (scrollRange == -1) {
scrollRange = appBarLayout.getTotalScrollRange();
}
if (scrollRange + verticalOffset == 0) {
collapsingToolbar.setTitle(getString(R.string.app_name));
isShow = true;
} else if (isShow) {
collapsingToolbar.setTitle(" ");
isShow = false;
}
}
});
}

这是第二个 Activity

public class ActivityTwo extends AppCompatActivity {

private Toolbar toolbar;
private ViewPager mPager;
private SlidingTabLayout mTabs;
private static final int UNIVERSITES = 0;
private static final int COLLEGES = 1;
private static final int SCHOOLS = 2;
private static final int OTHERS = 3;
public static ProgressBar spinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_two);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar); // Setting toolbar as the ActionBar with setSupportActionBar() call
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setTitleTextColor(Color.WHITE);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
mTabs.setViewPager(mPager);

spinner = (ProgressBar)findViewById(R.id.progress_bar);
spinner.setVisibility(View.GONE);


}

class MyPagerAdapter extends FragmentPagerAdapter {
String[] tabs;
public MyPagerAdapter(FragmentManager fm) {
super(fm);
tabs=getResources().getStringArray(R.array.tabs);
}

@Override
public Fragment getItem(int position) {
Fragment myFragment = null;

switch (position){
case UNIVERSITES:
myFragment = FragmentUniversities.newInstance("","");
break;

case COLLEGES:
myFragment = FragmentColleges.newInstance("","");
break;

case SCHOOLS:
myFragment = FragmentSchool.newInstance("","");
break;
case OTHERS:
myFragment = FragmentOthers.newInstance("","");
break;

}

return myFragment;

}

@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}

@Override
public int getCount() {
return 4;
}
}

最佳答案

在您的 Intent 中手动发送数据,您希望在哪个选项卡位置显示如下:

   Intent intent=new Intent(YOUR_ACTIVITY_CONTEXT,ActivityTwo.class);
//give whichever position you want to set for ex: 0 for clicking universities or 1 for clicking colleges and so on.
intent.putExtra("position",1);
YOUR_ACTIVITY_CONTEXT.startActivity(intent);

在你的 ActivityTwo 中:

   mPager.setCurrentItem(getIntent().getIntExtra("position",0));

添加以下行以获取各个选项卡的页面:

  mTabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
mPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});

关于java - 安卓开发 : Directing to specific tabs in fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38480986/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com