i want to add a fragment only on selecting items from the dropdown menu and by default there should be no fragment in the activity except the dropdown. I tried using .add() function but its giving me error.
I have 2 fragments named SAR.java and Pagdi.java and the dropdown has 3 entries:select-following,sar,pagdi.
ReportIncident.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.sindhuworld.ReportIncident">
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="@layout/toolbar_layout"
android:id="@+id/toolBar">
</include>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:id="@+id/spinner"
android:entries="@array/Select_following"
android:gravity="center"
android:layout_below="@id/toolBar"
android:background="@drawable/dropdownimg"
></Spinner>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/sar">
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pagdi">
</FrameLayout>
</RelativeLayout>
ReportIncident.java here drop down has 3 entries: select_following,sar,pagdi..i don't need any fragment when the select-following item has been selected but for rest two items i want fragment to be added in the activity. Suggest the code to be written in cases or any alternative approach?
package com.example.user.sindhuworld;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
public class ReportIncident extends AppCompatActivity {
Spinner sp;
Toolbar toolbar;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report_incident);
toolbar=(Toolbar)findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
viewPager=(ViewPager)findViewById(R.id.viewpager);
sp=(Spinner)findViewById(R.id.spinner);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
case 1:
case 2:
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
There is no need for 2 containers as you did below
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/sar">
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pagdi">
</FrameLayout>
it's enought to use only one container then add fragmetns and remove if needed
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragContainer">
</FrameLayout>
then inside your spinner's item selected, you should just handle case 2 and 3 because you don't anything to happen when select_following is clicked
switch (position){
case 1:
getSupportFragmentManager().beginTransaction().replace(R.id.fragContainer, new Sar()).commit();
break;
case 2:
getSupportFragmentManager().beginTransaction().replace(R.id.fragContainer, new Pagdi()).commit();
break;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.