它有5个选项卡。每个选项卡是一个片段。我正在寻找的是切换选项卡之间的编程。尝试与以下,但它没有帮助我。
==========================我的主要活动java======================
public class MainActivity extends AppCompatActivity {
private SectionsPageAdapter mSectionsPageAdapter;
private FirebaseAuth auth;
public ViewPager mViewPager;
public TabLayout tabLayout;
private final String TAG = "ListActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
auth = FirebaseAuth.getInstance();
setContentView(R.layout.activity_main);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(mViewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.getTabAt(0).setText("program");
tabLayout.getTabAt(1).setText("security");
tabLayout.getTabAt(2).setText("devices");
tabLayout.getTabAt(3).setText("network");
tabLayout.getTabAt(4).setText("sections");
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavView_Bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(3);
menuItem.setChecked(true);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.ic_home:
break;
case R.id.ic_message:
Intent intent1 = new Intent(MainActivity.this, ActivityOne.class);
startActivity(intent1);
break;
case R.id.ic_add:
if (auth.getCurrentUser() != null) {
createNewListItem();
}else{
Toast.makeText(getApplicationContext(), "You have to Login to Add a new Service!", Toast.LENGTH_SHORT).show();
}
break;
case R.id.ic_profile:
Intent intent3 = new Intent(MainActivity.this, ActivityThree.class);
startActivity(intent3);
break;
}
return false;
}
});
}
private void setupViewPager(ViewPager viewPager) {
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new Tab1Fragment());
adapter.addFragment(new Tab2Fragment());
adapter.addFragment(new Tab3Fragment());
adapter.addFragment(new Tab4Fragment());
adapter.addFragment(new Tab5Fragment());
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(4);
}
==================================================================
public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ItemRowHolder> {
private ArrayList<SectionDataModel> dataList;
private Context mContext;
public RecyclerViewDataAdapter(Context context, ArrayList<SectionDataModel> dataList) {
this.dataList = dataList;
this.mContext = context;
}
@Override
public ItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, null);
ItemRowHolder mh = new ItemRowHolder(v);
return mh;
}
@Override
public void onBindViewHolder(ItemRowHolder itemRowHolder, int i) {
final String sectionName = dataList.get(i).getHeaderTitle();
ArrayList singleSectionItems = dataList.get(i).getAllItemsInSection();
itemRowHolder.itemTitle.setText(sectionName);
SectionListDataAdapter itemListDataAdapter = new SectionListDataAdapter(mContext, singleSectionItems);
itemRowHolder.recycler_view_list.setHasFixedSize(true);
itemRowHolder.recycler_view_list.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
itemRowHolder.recycler_view_list.setAdapter(itemListDataAdapter);
itemRowHolder.recycler_view_list.setNestedScrollingEnabled(false);
/* itemRowHolder.recycler_view_list.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
//Allow ScrollView to intercept touch events once again.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
// Handle RecyclerView touch events.
v.onTouchEvent(event);
return true;
}
});*/
itemRowHolder.btnMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TabLayout tabLayout = (TabLayout)v.findViewById(R.id.tabs);
Toast.makeText(v.getContext(), "click event on more, "+sectionName , Toast.LENGTH_SHORT).show();
if (sectionName=="sections"){
tabLayout.getTabAt(5).select();
}else if (sectionName=="network"){
tabLayout.getTabAt(4).select();
}else if(sectionName=="devices"){
tabLayout.getTabAt(3).select();
}else if(sectionName=="security"){
tabLayout.getTabAt(2).select();
}else {
tabLayout.getTabAt(1).select();
}
}
});
4条答案
按热度按时间vs3odd8k1#
在Activity和片段之间进行通信的最简单方法是使用接口。其基本思想是在给定的片段A中定义一个接口,并让Activity实现该接口。
一旦它实现了该接口,您就可以在它覆盖的方法中执行任何您想要的操作。
有一个很好的tutorial关于如何做正是这种事情。
我希望这对你有帮助!
0ve6wy6x2#
将此代码添加到适配器中
p4rjhz4m3#
1)在MainActivity中创建一个方法
2)在MyFragment中更改您的onClick监听器,如下所示
4c8rllxm4#
请遵循以下步骤。在MainActivity.java
定义全局常量int n=default_fragment_no
在你的片段里