Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing ViewPager into a LinearLayout
    primarykey
    data
    text
    <p>What I am trying to do is to have a ViewPager section on my app, where the user can scroll to see the results. I am using Android Studio.</p> <p>At the moment I am currently displaying the results like so, by outputting the values to <em>out</em> by stringing them together:</p> <p><strong>Part of Main_Activity.java</strong></p> <pre><code>class CustomOnItemSelectedListener implements AdapterView.OnItemSelectedListener { public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int pos,long id) { String tempValue = spinner1.getSelectedItem().toString(); float inputValue; if(text.getText().length() == 0) { inputValue = Float.parseFloat("10"); text.setText("10"); } else { inputValue = Float.parseFloat(text.getText().toString()); } if ("Fahrenheit".equals(tempValue)) { out.setText(String.valueOf(ConverterUtil.convertFahrenheitToCelsius(inputValue)) + " &amp; " + String.valueOf(ConverterUtil.convertFahrenheitToKelvin(inputValue))); type.setText("Celsius + Kelvin"); } if ("Celsius".equals(tempValue)) { out.setText(String.valueOf(ConverterUtil.convertCelsiusToFahrenheit(inputValue)) + " &amp; " + String.valueOf(ConverterUtil.convertCelsiusToKelvin(inputValue))); type.setText("Fahrenheit + Kelvin"); } if ("Kelvin".equals(tempValue)) { out.setText(String.valueOf(ConverterUtil.convertKelvinToCelsius(inputValue)) + " &amp; " + String.valueOf(ConverterUtil.convertKelvinToFahrenheit(inputValue))); type.setText("Celsius + Fahrenheit"); } } </code></pre> <p>What I would like to do is to to have each result in a separate view in which the user can scroll, however this is where I get a little lost/confused.</p> <p>I have implemented my Fragment:</p> <p><strong>Part of Main_Activity.java</strong></p> <pre><code>class ScreenSlidePagerActivity extends FragmentActivity { private static final int NUM_PAGES = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPager = (ViewPager) findViewById(R.id.pager); mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager()); mPager.setAdapter(mPagerAdapter); } private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { public ScreenSlidePagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return new ScreenSlidePageFragment(); } @Override public int getCount() { return NUM_PAGES; } } } </code></pre> <p>and in the activity_main.xml layout, I have replaced the out &amp; type fields with a ViewPager:</p> <p><strong>Part of activity_main.xml</strong></p> <pre><code>&lt;android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" /&gt; </code></pre> <p>When called, it loads this class and displays the information using my result_slider layout:</p> <p><strong>ScreenSlidePageFragment.java</strong></p> <pre><code>public class ScreenSlidePageFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ViewGroup rootView = (ViewGroup) inflater.inflate( R.layout.result_slider, container, false); return rootView; } } </code></pre> <p><strong>result_slider.xml</strong></p> <pre><code>&lt;com.example.temperatureconverter xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent"&gt; &lt;TextView style="?android:textAppearanceMedium" android:padding="16dp" android:lineSpacingMultiplier="1.2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World" /&gt; &lt;/com.example.temperatureconverter&gt; </code></pre> <p>How can I go about passing the data from my results to a pane in the ViewPager? Using the code below, I don't get any errors, however the app crashes with <em>Error inflating class android.support.v4.view.ViewPager</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload