У меня есть макет с тремя TabItem
и ViewPager
, вкладки работают и отображаются, но когда я хочу загрузить новый фрагмент, например, одним нажатием кнопки на одной из этих вкладок, что бы я ни делал, я не могу изменить фрагмент. Я хочу загрузить фрагмент, когда нажата кнопка на одной из моих вкладок, но приведенный ниже код не работает
getSupportFragmentManager().beginTransaction().
add(R.id.frame_layout_container,userDetailFragment).commit();
replace
также не работает, я новичок в разработке Android, кто-то может мне помочь?
дополнительное объяснение: в одном из них у меня есть просмотр списка, когда я нажимаю на этот элемент, я перейду к деталям об этом элементе. Вот код в основном XML-файле:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:fitsSystemWindows="true"
android:id="@+id/drawer_layout"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/LinearLayout">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="@+id/toolbar"
android:background="#37548D"
android:elevation="4dp" />
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tab_layout"
android:background="#115A91"
app:tabSelectedTextColor="#FFF"
app:tabTextColor="#0A1102"
>
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/enter_tab"
android:text="@string/inter_fragment"
/>
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/contact_tab"
android:text="@string/fragment_contacts"
/>
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/register_tab"
android:text="@string/register_fragment"
/>
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frame_layout_container"
/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="@+id/navigation_view"
app:headerLayout="@layout/drawer_layout"
app:menu="@menu/main_menu"
/>
</android.support.v4.widget.DrawerLayout>
обновление: это код моего UserDetailFragment с ListFragment
public class ContactsFragment extends ListFragment {
CallBacks callBacks;
public ContactsFragment() { }
ArrayList<UserObject> userObjects;
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
userObjects = intent.getParcelableArrayListExtra(Intent_Service.SERVICE_PAYLOAD);
ArrayAdapter<UserObject> userObjectArrayAdapter = new UserArrayAdapter(context,0,userObjects);
setListAdapter(userObjectArrayAdapter);
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(getActivity(), Intent_Service.class);
getActivity().startService(intent);
LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).
registerReceiver(broadcastReceiver,new IntentFilter(Intent_Service.SERVICE_MESSAGE));
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_contacts,null);
}
public void onListItemClick(ListView l, View v, int position, long id) {
UserObject userObject = userObjects.get(position);
this.callBacks.send_user_object(userObject);
}
public interface CallBacks {
public void send_user_object(UserObject userObject);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.callBacks = (CallBacks)context;
}
}
и это код моей MainActivity:
public class MainActivity extends AppCompatActivity implements ContactsFragment.CallBacks {
android.support.v7.widget.Toolbar toolbar;
PageAdapter pageAdapter;
ViewPager viewPager;
TabLayout tabLayout;
TabItem contacts;
TabItem register;
TabItem signIn;
// ArashBroadCast broadcastReceiver = new ArashBroadCast(this);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page_drawer);
tab_variable_initialiser();
setSupportActionBar(toolbar);
NavigationView navigationView = findViewById(R.id.navigation_view);
navigationView.setItemIconTintList(null);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
if(tab.getPosition() == 1) {
toolbar.setBackgroundColor(ContextCompat.getColor(MainActivity.this,R.color.tab_contacts));
tabLayout.setBackgroundColor(ContextCompat.getColor(MainActivity.this,R.color.main_contacts));
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(ContextCompat.getColor(MainActivity.this,R.color.status_contacts));
}
} else if(tab.getPosition() == 2) {
toolbar.setBackgroundColor(ContextCompat.getColor(MainActivity.this,R.color.tab_register));
tabLayout.setBackgroundColor(ContextCompat.getColor(MainActivity.this,R.color.main_register));
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(ContextCompat.getColor(MainActivity.this,R.color.status_register));
}
} else {
toolbar.setBackgroundColor(ContextCompat.getColor(MainActivity.this,R.color.tab_signin));
tabLayout.setBackgroundColor(ContextCompat.getColor(MainActivity.this,R.color.main_signin));
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(ContextCompat.getColor(MainActivity.this,R.color.status_signin));
}
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
// tabLayout.setupWithViewPager(viewPager);
}
protected void onDestroy() {
super.onDestroy();
}
public void tab_variable_initialiser() {
this.tabLayout = findViewById(R.id.tab_layout);
this.contacts = findViewById(R.id.contact_tab);
this.register = findViewById(R.id.register_tab);
this.signIn = findViewById(R.id.enter_tab);
this.viewPager = findViewById(R.id.view_pager);
this.pageAdapter = new PageAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
this.viewPager.setAdapter(pageAdapter);
this.toolbar = findViewById(R.id.toolbar);
}
@Override
public void send_user_object(UserObject userObject) {
UserDetailFragment userDetailFragment = new UserDetailFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.contact_fragment_layout,userDetailFragment).commit();
}
}
если я использую R.id.contact_tab
, он работает, но не заменяет весь фрагмент, вместо этого userDetailFragment
показан ниже списка.
и я получаю ошибку: No view found for id 0x7f080031 (arash.samandar.recyclerview_final:id/contact_tab) for fragment UserDetailFragment{e489f08 #3 id=0x7f080031}