I'm having a problem using SlidingUpPanel (https://github.com/umano/AndroidSlidingUpPanel) and FragmentStatePagerAdapter.
I have 3 fragments in the pager: the first two with a sliding panel (one a fragment and one a hardcoded layout) and the third without it.
When I had a pager with just two fragments the panel were working perfectly fine. Now that I've added a third they are not working anymore.
At first the fragment are created correctly, but when I swipe all the way to the third and then come back the panel get messed up.
Basically the panel that used to be on the first fragment is now on the panel of the second one, the hardcoded layout of the second panel is now gone and the panel of the first fragment is empty.
The main content of all fragment is still correct and behaves as intended.
I've drawn a schema of the situation to explain better:
My code to select the fragment is:
public Fragment getItem(int i) {
Fragment fragment = null;
switch (i) {
case 0:
fragment = new MatchListFragment();
break;
case 1:
fragment = new StreamFragment();
break;
case 2:
fragment = new PendingMatches();
break;
default:
break;
}
return fragment;
}
Layout of first fragment (MatchListFragment):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:gravity="center"
android:orientation="vertical"
android:visibility="visible">
CONTENT
</RelativeLayout>
<FrameLayout
android:id="#+id/scrollable_panel"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Layout of second fragment (StreamFragment)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/log_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:divider="#drawable/material_divider"
android:dividerHeight="1dp"
android:visibility="visible" />
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/scrollable_panel"
android:layout_width="match_parent"
android:layout_height="88dp"
android:background="#color/white"
android:gravity="center">
<ImageView
android:id="#+id/icon"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:background="#color/light_gray" />
<ImageView
android:layout_width="25dp"
android:layout_height="10dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_margin="5dp"
android:src="#drawable/green_pill" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/icon"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<com.coffeestrap.android.Widgets.IconAndText
android:id="#+id/profile_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
app:resource="#drawable/ic_profile_icon"
app:undertext="New" />
<com.coffeestrap.android.Widgets.IconAndText
android:id="#+id/talk_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
app:resource="#drawable/ic_talk_icon"
app:undertext="Learning" />
<com.coffeestrap.android.Widgets.IconAndText
android:id="#+id/conversation_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
app:resource="#drawable/ic_conversation_icon"
app:undertext="Teaching" />
<com.coffeestrap.android.Widgets.IconAndText
android:id="#+id/all_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
app:resource="#drawable/ic_all_icon"
app:undertext="All" />
</LinearLayout>
</RelativeLayout>
I cant really understand how to solve this especially because on the second fragment (StreamFragment()) the sliding panel layout is hardcoded and I can't understand why it loads everything else correctly and the replace the panel with the one from the first fragment(MatchListFragment).
In the end I strongly believe it's an issue with the library I'm using and not one caused by me.
I've opened an issue on their github page
here
Related
My MainActivity has a TabLayout that I'm showing(View.VISIBILE) when certain fragments(say type A) are loaded and hiding(View.GONE) when other fragments(say type B) are loaded.
Now while transitioning between type A and type B fragments - the TabLayout and the fragment don't load together. First the TabLayout becomes visible and the previous fragment shifts down, then the new fragment replaces the old fragment. Of course this happens in milliseconds but some visible effect is there on looking carefully.
How can I make sure both TabLayout visibility change and fragment replace happen together?
Here is the code to load fragments in MainActivity.java:
...
private View mTitleWithTabs = findViewById(R.id.title_and_hometabs);
...
#UiThread
public void loadFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction
.add(R.id.frameLayout, fragment)
.addToBackStack(null)
.commit();
if (/* type A fragment */) {
setTabsHomeButtonSelected(true); // setting an ImageView as selected or not
scrollAndSelectTabAtPositionOnlyUI(0);
mTitleWithTabs.setVisibility(View.VISIBLE);
} else {
setTabsHomeButtonSelected(false);
mTitleWithTabs.setVisibility(View.GONE);
}
}
Here is the relevant code from activity_main.xml
...
<include
android:id="#+id/title_and_hometabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/title_with_hometabs"/>
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/exo_mini"
android:layout_below="#id/title_and_hometabs"/>
...
Here is title_with_hometabs.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/title_with_hometabs"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:showIn="#layout/activity_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/title"
android:paddingStart="35dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".90"
android:gravity="center"
android:text="#string/app_name"
android:textColor="#color/colorViewAll"
android:textSize="#dimen/text_size_22" />
<ImageView
android:id="#+id/search_browser"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_weight=".10"
android:src="#drawable/ic_search_black_14dp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorDivider" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="42dp"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="?actionBarSize"
android:layout_height="42dp"
android:gravity="center">
<ImageView
android:id="#+id/home_button_tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#mipmap/home" />
</RelativeLayout>
<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="#color/colorDivider" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs_list"
android:layout_height="42dp"
android:layout_width="match_parent"
app:tabBackground="#android:color/transparent"
app:tabRippleColor="#null"
app:tabMode="scrollable"
app:tabIndicatorColor="#android:color/holo_red_dark"
app:tabSelectedTextColor="#android:color/holo_blue_dark"
app:tabTextAppearance="#style/CustomTabLanguages"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#color/colorDivider" />
</LinearLayout>
</LinearLayout>
The thing that worked for me was to use getSupportFragmentManager().executePendingTransactions() after calling commit() inside the loadFragment() method in MainActivity.java.
This is because the delay between additional UI element in MainActivity and fragment-load was coming because of FragmentManager.commit() being asynchronous.
Other ways that I explored and didn't work:
Using FragmentManager.commitNow() doesn't work because I need the backstack.
Using FragmentManager.runOnCommit(additionUIElementChangeRunnable) doesn't also allow the fragment transaction to be added the backstack.
Note: FragmentManager.commitNow() doesn't allow using backstack because ordering guarantee in backstack can't be provided when commitNow is used with commit.
I'm using an android.support.v4.widget.DrawerLayout with com.android.support:appcompat-v7 in my main activity (which extends AppCompatActivity) to provide a navigation drawer, and ListView within the drawer to present user clickable items.
This all works perfectly well except on Samsung Tab devices running Android 5.0.2.
The code has been tested and works as expected on various versions of Android from 4.2.1 to 6.0.1, and works fine on an emulator running 5.0.2.
On the Samsung devices, the nav drawer is dismissed on the tap, but the new activity (e.g. MyPreferenceActivity or HelpPageActivity, in the code below) is never displayed.
My question: is there anything incorrect about the code or layout that might cause this not to work on Samsung Tab/5.0.2 devices?
The main activity is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:gravity="fill"
android:background="#color/standard_bkgnd">
<include
android:id="#+id/toolbar_actionbar"
layout="#layout/toolbar_actionbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/toolbar_actionbar"
>
<!-- normal content view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
...main UI stuff...
</LinearLayout>
<!-- drawer view -->
<include layout="#layout/nav_drawer" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
The nav_drawer layout is as follows:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="280dp"
android:layout_height="match_parent"
android:background="#color/button_material_dark"
android:orientation="vertical"
android:layout_gravity="start">
<RelativeLayout
android:id="#+id/drawer_header"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#color/material_blue_grey_800"
android:padding="8dp" >
<ImageView
android:id="#+id/app_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_launcher_08"
android:layout_marginTop="15dp"
android:contentDescription="#string/app_icon"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="42dp"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/app_icon"
android:orientation="vertical" >
<TextView
android:id="#+id/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textColor="#fff"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginTop="4dp"
android:text="#string/app_long_name"
android:textColor="#fff"
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
<ListView
android:id="#+id/nav_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/drawer_header"
android:choiceMode="singleChoice"/>
</RelativeLayout>
The ListView is configured to present a number of items that user can click to view other content within the app, or perform other activities:
// Drawer Item click listeners
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0: // preferences
startActivity(new Intent(that, MyPreferenceActivity.class));
break;
case 1: // help
startActivity(new Intent(that, HelpPageActivity.class));
break;
case 2: // send feedback
composeEmail();
break;
default:
break;
}
_drawerLayout.postDelayed(new Runnable() {
#Override
public void run() {
_drawerLayout.closeDrawers();
}
}, 500);
drawerList.clearChoices();
}
});
Any suggestions much appreciated!
So, it turns out not to have been anything to do with Samsung Tab/5.0.2 after all - it was coincidence that the only reports of problems I'd had came from those devices.
The problem was that I had a landscape layout for my main activity which had the order of the normal content view and the drawer view reversed, which prevented the ListItems receiving the click event:
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/toolbar_actionbar"
>
<!-- normal content view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
...main UI stuff...
</LinearLayout>
<!-- drawer view *** this must come after the normal content view *** -->
<include layout="#layout/nav_drawer" />
</android.support.v4.widget.DrawerLayout>
As soon as I switched them around, things worked as expected. Doh.
I'm running into some problems trying to figure out how to make fragments that I have programmatically added into a LinearLayout clickable. I'm using fragments because they will be in multiple activities and it was a good way for me to create a layout like this:
http://i.stack.imgur.com/P4lOG.png
But, if there's a better way to do this that would make the process of making it clickable, I'm certainly open to changing things up.
Anyway, I'm adding the fragments to the LinearLayout, jobsList, like so:
FragmentManager fragmentManager = getFragmentManager();
int count = 2;
for (int i = 0; i < count; i++) {
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
Bundle bundle = new Bundle();
bundle.putInt("jobID", i + 1);
jobFragment job = new jobFragment();
job.setArguments(bundle);
fragmentTransaction.add(R.id.jobsList, job, Integer.toString(i));
fragmentTransaction.commit();
}
The count of 2 is just a placeholder for now, later there will be an arbitrary number of jobs.
Here is the layout, it's a bit of a mess but I got all the specific weights the way I wanted this way.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/basicPort"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:text="Port" />
<TextView
android:id="#+id/basicLoadOrEmpty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:text="Load/Empty" />
<TextView
android:id="#+id/basicInOrOut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:text="In/Out" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".6"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".333" >
<TextView
android:id="#+id/basicContainerNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".4"
android:background="#drawable/background"
android:text="Container #" />
<TextView
android:id="#+id/basicChassisNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".4"
android:background="#drawable/background"
android:text="Chassis #" />
<TextView
android:id="#+id/basicContainerType"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".2"
android:background="#drawable/background"
android:text="Cont. Type" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".333" >
<TextView
android:id="#+id/basicDirectionArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".2"
android:background="#drawable/background"
android:text="Direction" />
<TextView
android:id="#+id/basicCustomer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".8"
android:background="#drawable/background"
android:text="Customer Location" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".333" >
<TextView
android:id="#+id/basicSteamshipLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".4"
android:background="#drawable/background"
android:text="Steamship Line" />
<TextView
android:id="#+id/basicBKG_BOL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".6"
android:background="#drawable/background"
android:text="BKG-BOL#" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/basicStatus1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:background="#drawable/background"
android:text="Status 1" />
<TextView
android:id="#+id/basicStatus2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:background="#drawable/background"
android:text="Status 2" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="20dp" />
</LinearLayout>
My question to you is, is it possible to make these fragments clickable and uniquely identifiable, and if so, what is the best way to go about doing that?
Thank you for your help!
What you could do is:
Modify the layout of your fragment, so it uses the RelativeLayout (now its probably LinearLayout right?). By modify I mean either use RelativeLayout INSTEAD of LinearLayout (more complicated but i think a bit better), or put your LinearLayout INSIDE RelativeLayout.
Place a view that matches in size the size of the fragment, on top of your textViews. Add an onClickListener to it that performes the code on clicks.
First - fragments are not clickable, layouts and their children are. A Fragment can handle the click action for things that occur in its layout but you do not "click" the fragment.
Second - sounds like you're confused about how to use fragments and what a Fragment actually does. A Fragment is like an Activity with some parts missing. A Fragment has its own lifecycle and can be used to perform many of the same tasks as an Activity the difference being that (1) a Fragment is "hosted" by an activity that provides the missing functions and does not run on its own (2) a Fragment can run in the background with no UI (I do not know of a background activity yet).
You do not need to/should not be using Fragments for what you are trying to do.
Your layout is really just a series of TextViews. You should be able to make that layout in XML reuse that layout file - by simply getting a LayoutInflater anywhere else in your app that you want to. If things got really crazy you could create a custom TextView class to provide detailed functionality but you should not be using 10 fragments, each with one TextView to make that layout work.
I just wanted to follow up on things in case anybody stumbles upon this answer in the future. It turns out using fragments was a bad idea. While I was able to make them clickable and identifiable through a bit of a hack job, I ran into problems later on when I was trying to fix layout issues. So, thanks to #Rarw I looked into using a LayoutInflater instead.
And that was much easier! I just made the layout xml file, used the LayoutInflater to add it to a LinearLayout and added that to the list. Makes things much easier down the road:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout container = new LinearLayout(this);
inflater.inflate(R.layout.basic, container, true);
container.setId(i);
list.addView(container, params);
container.setClickable(true);
container.setOnClickListener(this);
A hacky solution is to place a transparent button on top of the entire fragment, have the fragment extend onClickListener, and set the button's onClickListener to this.
One way to create such a button:
Use a constraintLayout as the root layout for the xml.
As the last of child of the constraintLayout, create a button, constrain it to occupy entire fragment by setting layout_constrainXXX to corner elements.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<!-- The remaining properties of the layout -->
<!-- All the children of the layout -->
<Button
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:background="#00000000"
android:id="#+id/fragment_button" />
</android.support.constraint.ConstraintLayout>
Note that you can do this other ways too, such as in a RelativeLayout
Then in the java/kotlin file, extend onClickListener.
override fun onClick(p0: View?) {
// Whatever you want to do here
}
In method onActivityCreated, set the transparent button's listener as:
var button : Button = fragment_button
button.setOnClickListener(this)
I have an activity with the associated activity.xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/pattern_background"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:clickable="false" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
...
<LinearLayout
android:id="#+id/llComments"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</ScrollView>
<include
android:id="#+id/layoutComments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/widget_comentarios_fragment" >
</include>
In the LinearLayout with id llComments I introduce a fragment called CommentsFragment dynamically from the activity with the following code:
private Fragment commentsFragment;
private FragmentTransaction ft;
...
llComentarios = (LinearLayout) findViewById(R.id.llComments);
...
Bundle args = new Bundle();
args.putString(Constants.IDMODEL, getId());
commentsFragment = CommentsFragment.newInstance(args);
ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.llComments, commentsFragment).commit();
The Fragment associated xml file is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/comments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#color/transparent"
android:dividerHeight="10dp"
android:paddingLeft="#dimen/indicator_internal_padding"
android:paddingRight="#dimen/indicator_internal_padding" />
</LinearLayout>
The problem I cannot solve is to show in the activity more than one item (in fact I am showing one and a third items) in the list if I do not fix a height for the listview.It should adapt to as many items I add to the list in the fragment through the adapter. I have tried different combinations of heights in the layouts and views, but still does not work. Maybe I am annulating one with other.
Any help would be appreciated.
Thanks in advance
After some days I find out the exact problem. I was including a ListView into a ScrollView, and that's clearly not recomended by Android developers.
You can look for further info here: Why ListView cannot be used in a ScrollView?
Anyway, if you want to do it, you can do that with a hack. Take a look at these proposals:
How can I put a ListView into a ScrollView without it collapsing?
For me that worked. Problem solved.
I have a simple layout, which contains two fragments. One of the two fragments periodically will replace itself, the other one will remain consistent for the whole time I am using it. For some reason, removing/adding it works just fine, but replacing it causes the other fragment to disappear. I know from debug code that the Fragment simply gets detached, for no apparent reason. Why might this be? If I use the latter, I have to be extremely careful to ensure that the QuestionArea is gone first...
These lines of code come from the in the FragmentActivity class.
fragManager.beginTransaction()
.replace(R.id.QuestionArea, getQuestionPostView(), "QuestionArea")
.commit();
fragManager.beginTransaction()
.remove(fragManager.findFragmentById(R.id.QuestionArea))
.add(R.id.QuestionArea, getQuestionPostView(), "QuestionArea")
.commit();
Here is what I hope is the other relevant code:
XML of the main display:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical" >
<com.pearsonartphoto.FontFitTextView
android:id="#+id/Name"
style="#style/bigText"
android:background="#color/blue"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
<TextView android:id="#+id/Instructions"
style="#style/medText"
android:layout_width="fill_parent"
android:layout_below="#id/PowerName"
/>
<fragment
android:name="com.pearsonartphoto.NumberDisplay"
android:gravity="center"
android:id="#+id/QuestionArea"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/Instructions"/>
<fragment
android:name="com.pearsonartphoto.AbstractAnswerFragment"
android:id="#+id/AnswerArea"
style="#style/bigText"
android:gravity="center"
android:layout_below="#+id/QuestionArea"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="530sp"/>
</RelativeLayout>
Part of FragmentActivity which sets up the AnswerArea
protected void setAnswerPad(AbstractAnswerFragment pad)
{
fragManager.beginTransaction()
.replace(R.id.AnswerArea, pad, "AnswerArea")
.commit();
fragManager.executePendingTransactions();
answerPadToAnswer=pad.getAnswerKey();
}
First time (From FragmentActivity) that the QuestionArea is set.
fragManager.beginTransaction()
.replace(R.id.QuestionArea, getQuestionPreView(), "QuestionArea")
.commit();
The functions getPostView() and getPreView()
#Override
Fragment getQuestionPreView() {
NumberDisplay display=(NumberDisplay)manager.findFragmentById(R.id.QuestionArea);
display.setNumber(-1);
return display;
}
#Override
Fragment getQuestionPostView() {
NumberDisplay display=(NumberDisplay)manager.findFragmentById(R.id.QuestionArea);
display.setNumber(answer);
return display;
}
Your problems, most likely, come from trying to change static fragments(fragments declared in the xml layout) at runtime, which you shouldn't do. If you plan to replace, remove etc those fragments in your FragmentActivity you should put instead a wrapper layout(like a FrameLayout) in their places in the xml layout and use that container for future fragment transactions. Check this answer from one of the Android engineers.
There were a few things that needed to be done to make this work:
The key thing was to change my main XML code to FrameLayout.
I had to create some of the smaller fragments, when I wasn't having to create them before.
New main XML code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical" >
<com.pearsonartphoto.FontFitTextView
android:id="#+id/PowerName"
style="#style/bigText"
android:background="#color/blue"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
<TextView android:id="#+id/Instructions"
style="#style/medText"
android:layout_width="fill_parent"
android:layout_below="#id/PowerName"
/>
<FrameLayout
android:gravity="center"
android:id="#+id/QuestionArea"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/Instructions"/>
<FrameLayout
android:id="#+id/AnswerArea"
style="#style/bigText"
android:gravity="center"
android:layout_below="#+id/QuestionArea"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="530sp"/>
</RelativeLayout>