每当我尝试在recyclerAdapter中替换片段时,该片段将变为空白。
这是我设置代码的方式:
我有一个viewpager,其中添加了4个片段。它也与底部导航视图对齐,这意味着我可以通过左右滑动来更改片段,也可以使用导航栏来更改片段。因此,底部导航栏的设置如下:
片段1 |片段2 |片段3 |片段4
viewpager可以完美运行,并且可以在这4个片段之间轻松切换。
但是然后我还有一个recyclerAdapter,它在Fragment 2中设置了cardviews(RecyclerAdapter在单独的Java类中,而不是在Fragment 2 java类中)。
因此,在片段2中,我有4个不同的片段,我将其称为:片段2.1,片段2.2,片段2.3和片段2.4。每当我单击卡片视图之一的位置时,都应该将片段更改为片段2.1,片段2.2,片段2.3或片段2.4。根据我单击的卡片视图。
这些片段2.1、2.2、2.3和2.4不会添加到viewpager中。
但是我已经在recyclerAdapter中设置了一个代码,以通过begintransaction更改片段。不幸的是,当我尝试这样做时,我最终得到了一个空白片段。因此,我的问题是,如何更改添加到viewpager的片段?我是否必须将片段2.1、2.2、2.3和2.4添加到viewpager,还是可以以某种方式将其替换为begin transaction?代码如下:
RecyclerAdapter(应该通过单击cardview的位置来更改片段):
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from( viewGroup.getContext() ).inflate( R.layout.fragment_card_view_tabel, viewGroup, false );
viewGroup.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
}
} );
return new RecyclerViewHolder( view );
}
@Override
public void onBindViewHolder(RecyclerViewHolder recyclerViewHolder, int position) {
recyclerViewHolder.mBeskrivelse.setText( OurData.beskrivelse[position] );
recyclerViewHolder.mTitle.setText( OurData.title[position] );
recyclerViewHolder.cardView.setCardBackgroundColor( Color.parseColor( OurData.colors[position] ) );
}
@Override
public int getItemCount() {
return OurData.title.length;
}
class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public int nummer;
private TextView mBeskrivelse;
private TextView mTitle;
private CardView cardView;
private ViewPager mPager;
public RecyclerViewHolder(View itemView) {
super( itemView );
mPager = (ViewPager) itemView.findViewById( R.id.flMain );
mBeskrivelse = (TextView) itemView.findViewById( R.id.item_beskrivelse );
mTitle = (TextView) itemView.findViewById( R.id.item_title );
cardView = (CardView) itemView.findViewById( R.id.card_view );
itemView.setOnClickListener( this );
}
@Override
public void onClick(View v) {
nummer = getAdapterPosition();
if (nummer == 0) {
//mPager.setCurrentItem(0);
((FragmentActivity) itemView.getContext()).getSupportFragmentManager().beginTransaction().replace( R.id.flMain, new DataTabelFragment() ).addToBackStack( null ).commit();
} else if (nummer == 1) {
((FragmentActivity) itemView.getContext()).getSupportFragmentManager().beginTransaction().replace( R.id.flMain, new Sensor1Fragment() ).commit();
} else if (nummer == 2) {
((FragmentActivity) itemView.getContext()).getSupportFragmentManager().beginTransaction().replace( R.id.flMain, new Sensor2Fragment() ).commit();
} else if (nummer == 3) {
((FragmentActivity) itemView.getContext()).getSupportFragmentManager().beginTransaction().replace( R.id.flMain, new Sensor3Fragment() ).commit();
}
}
}
}
ViewPagerAdapter(这是我从底部导航视图添加的片段的位置):
public class ViewPageAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<Fragment> mFragmentListCardView = new ArrayList<>();
private final List<String> nameArray = new ArrayList<>();
public ViewPageAdapter(FragmentManager fm) {
super( fm );
}
public void addFragment(Fragment fragment, String name) {
mFragmentList.add( fragment );
nameArray.add(name);
}
public void addFragmentCardView(Fragment fragment) {
mFragmentListCardView.add( fragment );
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get( position );
}
@Override
public int getCount() {
return mFragmentList.size();
}
//public int getItemPosition(Object object) {
// return POSITION_NONE;
//}
}
MainActivity(将片段添加到ViewPagerAdapter的位置):
public void setupViewPager(ViewPager viewPager) {
ViewPageAdapter adapter = new ViewPageAdapter( getSupportFragmentManager() );
adapter.addFragment( new StartsideFragment(), "StartsideFragment" );
adapter.addFragment( new CardViewTabelFragment(), "CardViewTabelFragment" );
adapter.addFragment( new SensorOversigtFragment(), "SensorOversigtFragment" );
adapter.addFragment( new KontaktFragment(), "KontaktFragment" );
viewPager.setAdapter( adapter );
}
public void setViewPager(int fragmentNumber) {
mPager.setCurrentItem( fragmentNumber );
}
public void enableViewPagerSwitch() {
mPager.addOnPageChangeListener( new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (prevMenuItem != null) {
prevMenuItem.setChecked( false );
} else {
mBottomNav.getMenu().getItem( 0 ).setChecked( false );
navigationView.getMenu().getItem( 0 ).setChecked( false );
}
Log.d( "page", "onPageSelected: " + position );
mBottomNav.getMenu().getItem( position ).setChecked( true );
navigationView.getMenu().getItem( position ).setChecked( true );
prevMenuItem = mBottomNav.getMenu().getItem( position );
prevMenuItem = navigationView.getMenu().getItem( position );
}
@Override
public void onPageScrollStateChanged(int state) {
}
} );
setupViewPager( mPager );
}
public void bot_Navigation() {
mBottomNav = (BottomNavigationView) findViewById( R.id.nav_bot );
botNavHelper.disableShiftMode( mBottomNav );
mBottomNav.setOnNavigationItemSelectedListener( new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.bot_startside:
setViewPager( 0 );
break;
case R.id.bot_datatabel:
setViewPager( 1 );
break;
case R.id.bot_sensorOversigt:
setViewPager( 2 );
break;
case R.id.bot_kontakt:
setViewPager( 3 );
break;
case R.id.bot_logUd:
setViewPager( 4 );
alertDialog();
break;
}
return true;
}
} );
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
onCreate2();
bot_Navigation();
}
protected void onCreate2() {
setContentView( R.layout.activity_main );
Toolbar toolbar = (Toolbar) findViewById( R.id.toolbar );
setSupportActionBar( toolbar );
TextView mTitle = (TextView) toolbar.findViewById( R.id.toolbartitle );
mTitle.setText( toolbar.getTitle() );
getSupportActionBar().setDisplayShowTitleEnabled( false );
mPager = (ViewPager) findViewById( R.id.flMain );
enableViewPagerSwitch();
DrawerLayout drawer = (DrawerLayout) findViewById( R.id.drawer_layout );
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close );
drawer.addDrawerListener( toggle );
toggle.syncState();
navigationView = (NavigationView) findViewById( R.id.nav_view );
navigationView.setNavigationItemSelectedListener( this );
navigationView.setCheckedItem( R.id.nav_startside );
}
*在此处添加片段2的代码*
公共类CardViewTabelFragment扩展了片段{
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
public CardViewTabelFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate( R.layout.card_list, container, false );
recyclerView = (RecyclerView) view.findViewById( R.id.recycler_view );
adapter = new RecyclerAdapter();
recyclerView.setAdapter( adapter );
final RecyclerView.LayoutManager layoutManager = new LinearLayoutManager( getActivity() );
((LinearLayoutManager) layoutManager).setOrientation( LinearLayoutManager.VERTICAL );
recyclerView.setLayoutManager( layoutManager ) ;
return view;
}
}
*感谢您的帮助。我可以根据需要*