Reputation: 861
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.
At the moment I am currently displaying the results like so, by outputting the values to out by stringing them together:
Part of Main_Activity.java
class CustomOnItemSelectedListener implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> 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)) + " & " + String.valueOf(ConverterUtil.convertFahrenheitToKelvin(inputValue)));
type.setText("Celsius + Kelvin");
}
if ("Celsius".equals(tempValue)) {
out.setText(String.valueOf(ConverterUtil.convertCelsiusToFahrenheit(inputValue)) + " & " + String.valueOf(ConverterUtil.convertCelsiusToKelvin(inputValue)));
type.setText("Fahrenheit + Kelvin");
}
if ("Kelvin".equals(tempValue)) {
out.setText(String.valueOf(ConverterUtil.convertKelvinToCelsius(inputValue)) + " & " + String.valueOf(ConverterUtil.convertKelvinToFahrenheit(inputValue)));
type.setText("Celsius + Fahrenheit");
}
}
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.
I have implemented my Fragment:
Part of Main_Activity.java
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;
}
}
}
and in the activity_main.xml layout, I have replaced the out & type fields with a ViewPager:
Part of activity_main.xml
<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" />
When called, it loads this class and displays the information using my result_slider layout:
ScreenSlidePageFragment.java
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;
}
}
result_slider.xml
<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">
<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" />
</com.example.temperatureconverter>
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 Error inflating class android.support.v4.view.ViewPager
Upvotes: 3
Views: 3556
Reputation: 6263
Try following the answer to this question: How to implement a ViewPager with different Fragments / Layouts
Make sure you include the libraries and merge your MainActivity with the one from the answer.
Then in your layout file for the MainActivity, put your ViewPager
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Where you would like the content to scroll/go
Upvotes: 2