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
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
Related
The Problem:
In my onCreate() on my main activity, I am trying to set the text value of a TextView in my fragment which is displayed by a viewpager. When I try to get my fragment instance, It always returns null.
Solutions I Tried:
Almost every solution related to this problem on SO gives me the same result(NPE).
My Code is below as well as some explanation...
Home.java Activity (My Main Activity):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Show the home layout
setContentView(R.layout.home);
// Setup the toolbar
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Now setup our tab bar
TabLayout tabLayout = (TabLayout)findViewById(R.id.homeTabs);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
// Setup the view pager
ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(viewPager);
// Start our service controller
Intent startServiceController = new Intent(this, ServiceController.class);
startService(startServiceController);
Fragment viewFragment = pagerAdapter.getRegisteredFragment(viewPager.getCurrentItem());
if(viewFragment == null){
Log.v("Fragment", "NULL");
}
}
After setting up the pageAdapter and viewpager, I try to access the fragment thats being shown. The code for my PagerAdapter is below...
PagerAdapter:
public class PagerAdapter extends SmartFragmentStatePagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ParkListFragment();
case 1:
return new MapFragment();
default:
return null;
}
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
CharSequence title;
switch (position){
case 0:
title = "Spaces Near You";
break;
case 1:
title = "Map";
break;
default:
title = "N/A";
break;
}
return title;
}
}
SmartFragmentStatePagerAdapter:
public abstract class SmartFragmentStatePagerAdapter extends FragmentStatePagerAdapter {
// Sparse array to keep track of registered fragments in memory
private SparseArray<Fragment> registeredFragments = new SparseArray<>();
public SmartFragmentStatePagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
// Register the fragment when the item is instantiated
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
// Unregister when the item is inactive
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
// Returns the fragment for the position (if instantiated)
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
}
I really don't understand what the issue is. I have tried many things. What I don't want to use is this:
String tag="android:switcher:" + viewId + ":" + index;
ParkListFragment.java:
public class ParkListFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.park_list_fragment, container, false);
}
}
ParkList Fragment Layout:
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/confidenceText"
android:textAlignment="center"
android:textSize="24sp"
android:textColor="#android:color/primary_text_light"
android:padding="10dp"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/activityText"
android:textAlignment="center"
android:textSize="24sp"
android:textColor="#android:color/primary_text_light"
android:padding="10dp"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
Stack Trace: Pastebin
So after hours of trial and error, the only thing that worked for me was this SO post:
ViewPager Fragments are not initiated in onCreate
i want to create a viewpager with one layout that contains a listview but i'm always getting this error:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.myapp/com.example.myapp.Join_activity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a
null object reference
The viewpager works fine without the listview, and the listview works fine without the viewpager. I know the problem, i'm using "setContentView" with a different layout where my listview is, but i don't know how to solve it, please help, here is the code:
public class Join_activity extends AppCompatActivity {
ListView lv_zona;
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.join_pager);//here is the problem
//my listview is in join3.xml
//ViewPager
viewPager = (ViewPager) findViewById(R.id.viewpaginador);
viewPager.setAdapter(new Join_Adapter(this));
//ListView
lv_zona = (ListView)findViewById(R.id.lv_zona);
lv_zona.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,new String[]{"A1","B1","C1","D1"}));
viewPager.addOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
#Override
public void onPageSelected(int position) {}
#Override
public void onPageScrollStateChanged(int state){}
});
}//end oncreate
}//end class
*
public class Join_Adapter extends PagerAdapter {
private Context mContext;
public Join_Adapter(Context context) {
mContext = context;
}
#Override
public Object instantiateItem(ViewGroup coleccion, int posicion) {
ModelObject modelObject = ModelObject.values()[posicion];
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup mlayout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), coleccion, false);
coleccion.addView(mlayout);
return mlayout;
}
#Override
public void destroyItem(ViewGroup collection, int position, Object view)
{collection.removeView((View) view);}
#Override
public int getCount()
{return ModelObject.values().length;}
#Override
public boolean isViewFromObject(View view, Object object)
{return view == object;}
#Override
public CharSequence getPageTitle(int position) {
ModelObject customPagerEnum = ModelObject.values()[position];
return mContext.getString(customPagerEnum.getTitleResId());
}
}
My files:
My_files.jpg
My xmls:
***join3.xml - Here is my ListView***
<?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">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lv_zona"
android:choiceMode="singleChoice" />
</LinearLayout> </LinearLayout>
*
***join_pager.xml - my main layout***
<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=".Paginador">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView2" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpaginador"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
You should use fragments for this. You are using a view pager. Now you should display the list inside a fragment that you will load in the activity. This answer shows how to do that. Your fragment xml will contain the listView. And the getItem of the adapter will return the fragment.
The reason of NullPointerException is the list activity is not in the main layout, so, findViewById returns null.
To put a listview in a ViewPager, see: https://stackoverflow.com/a/12535150/189961
I want to add a viewPager for main fragment in my android app.
This is the xml file for main fragment
<RelativeLayout 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="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
This is the Java code for Main Fragment
public class MainFragment extends ListFragment {
private static final String TAG = MainFragment.class.getSimpleName();
private StudentsDBHandler studentsDBHandler;
public MainFragment() {
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
studentsDBHandler = new StudentsDBHandler(getActivity());
View rootView = inflater.inflate(R.layout.main_fragment, container, false);
displayStudents();
return rootView;
}
private void displayStudents() {
List<Student> studentList = studentsDBHandler.getAll();
ArrayAdapter<Student> list = new ArrayAdapter<>(getActivity(), R.layout.item_student, studentList);
setListAdapter(list);
}
}
You have a DrawerLayout page. It's about your drawer contains.
İn the DrawerLayout XML
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
add this code below
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
write this code and setup your View pager like example
viewPager=(ViewPager)findViewById(R.id.viewPager);
FragmentManager fm=getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fm));
}
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
if(position==0)
fragment= new Home1();
if(position==1)
fragment=new Home2();
return fragment;
}
#Override
public int getCount() {
return 2;
}
}
}
I hope Help you
I'm using ViewPager to swipe between multiple images, but swiping between images is sluggish and not smooth although i only loaded few images. Here's my full code
MainActivity.java
public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {
public static SectionsPagerAdapter mSectionsPagerAdapter;
public static ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
Bundle args = new Bundle();
fragment = new Page();
args.putString(Page.ARG_SECTION_NUMBER, position);
fragment.setArguments(args);
return fragment;
}
public int getItemPosition(Object object) {
Log.v("MBG", "getItemPosition");
return POSITION_NONE;
}
#Override
public int getCount() {
return 6;
}
}
Page.java
public class Page extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public Page() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_page, container, false);
String index = getArguments().getString(ARG_SECTION_NUMBER);
ImageView callImage = (ImageView) rootView.findViewById(R.id.imageView);
int resID = getActivity().getResources().getIdentifier(index, "drawable", getActivity().getPackageName());
callImage.setImageResource(resID);
return rootView;
}
MainActivity.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_marginTop="0dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Prayers"
/>
</RelativeLayout>
Page.xml
<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.mahmoud.qaloun.Page">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView"
android:src="#drawable/a1"
android:scaleType="fitXY"
/>
</RelativeLayout>
But when i change this line of code
int resID = getActivity().getResources().getIdentifier(index, "drawable", getActivity().getPackageName());
to this:
int resID = getActivity().getResources().getIdentifier("1", "drawable", getActivity().getPackageName());
it works fine where "1" is an image stored in the resources folder "1.jpg"
Put
mViewPager.setOffscreenPageLimit(5);
after mViewPager.setAdapter();
Replace 5 with the number of pages that are off screen, i.e. total number of pages minus 1.
This will make your ViewPager smooth.
The reason why ViewPager is slow without this is that any page which is off screen gets destroyed and is recreated every time you switch to that page. If you have time consuming code in your Fragment's onActivityCreated(), they're going to be executed on every page switch. If you set the off screen page limit right, your Fragments will not be recreated every time you switch pages.
In my case - I have more then 10 images and each image have weight more 800kb, and I check this issue, and realy fine work if pass as a parameter 3 page
viewPager.setOffscreenPageLimit(3);
Also if you store your images in res/drawable - rename drawable to drawable-nodpi, because -nodpi mean next
These are density-independent resources. The system does not scale
resources tagged with this qualifier, regardless of the current
screen's density.
I too had the same problem. Please resize your image to smaller or required size. This will be a solution to your problem.
i have made a Master/Detail View which contains a TabHost in the Detailview.
i have to populate a list of tabs, and every tab has a listview as its content.
Now when i change my Item to display, after the third change or so, no more tab content is rendered(The indicators are rendered). But when i change the tab, the content gets rendered.
The onCreate Method of the DetailFragment is invoked when i change my item to display. The tabs are genrated and added to the tabhost. But after a few times i switched the item to display, the onCreateView Method of the OrderGroupTabFragment is not invoked anymore, without any reason to me.
Thankful for any help.
Here is my code:
DetailFragment:
/**
* A fragment representing a single Order detail screen. This fragment is either
* contained in a {#link OrderListActivity} in two-pane mode (on tablets) or a
* {#link OrderDetailActivity} on handsets.
*/
public class OrderDetailFragment extends Fragment implements IASyncMethodCallbacks {
private static Logger _logger = Logger.getLogger(OrderDetailFragment.class.getSimpleName());
/**
* The fragment argument representing the item ID that this fragment
* represents.
*
*/
public static final String ARG_ORDER = "order";
private TrafficSafetyOrder _order;
private TrafficSafetyOrderService _trafficSafetyOrderService;
private boolean _detached;
private FragmentTabHost tabHost;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public OrderDetailFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
_detached = false;
_trafficSafetyOrderService = new TrafficSafetyOrderService();
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_order_detail,
container, false);
tabHost = (FragmentTabHost)rootView.findViewById(R.id.order_detail_tab_host);
tabHost.setup(getActivity(), ((FragmentActivity)getActivity()).getSupportFragmentManager() , android.R.id.tabcontent);
OrderListItem order = (OrderListItem)getArguments().getSerializable(ARG_ORDER);
try {
if(order != null)
_order = _trafficSafetyOrderService.getOrderForId(order.getId());
} catch (Exception e) {
_logger.log(Level.WARNING, e.getMessage(), e);
}
if (_order != null) {
OrderId orderId = _order.getOrderIdByOrderIdType(OrderIdTypes.GEB);
((TextView) rootView.findViewById(R.id.order_detail_header))
.setText("Auftrag: " + orderId.getName() + orderId.getId());
for (final Group formGroup : _order.getFormGroups()) {
System.out.println("Adding Tab " + formGroup.getDisplayText());
TabSpec spec = tabHost.newTabSpec(formGroup.getDisplayText());
System.out.println("Spec created " + formGroup.getDisplayText());
spec.setIndicator(formGroup.getDisplayText());
System.out.println("Indicator set " + formGroup.getDisplayText());
Bundle bundle = new Bundle();
System.out.println("Bundle created " + formGroup.getDisplayText());
bundle.putSerializable(OrderGroupTabFragment.ORDER_GROUP_LIST_ARG, formGroup.getItems());
System.out.println("Added items to bundle " + formGroup.getDisplayText());
tabHost.addTab(spec, OrderGroupTabFragment.class, bundle);
System.out.println("Tab added " + formGroup.getDisplayText());
}
}
return rootView;
}
#Override
public void onStart() {
super.onStart();
tabHost.setCurrentTab(0);
}
public TrafficSafetyOrder getOrder() {
return _order;
}
public void setOrder(TrafficSafetyOrder _order) {
this._order = _order;
}
#Override
public void onDestroyView() {
super.onDestroyView();
try {
_trafficSafetyOrderService.updateTrafficServiceOrder(_order);
} catch (Exception e) {
_logger.log(Level.WARNING, e.getMessage(), e);
}
}
#Override
public void onDetach() {
super.onDetach();
_detached = true;
}
#Override
public void onASyncMethodCompleted(Object receiver, Method method) {
if(_detached)// if fragment is already detached from Activity, do not execute callback
return;
Toast.makeText(getActivity(), receiver != null ? receiver.getClass().getSimpleName() : "NULL" + "." + method != null ? method.getName() : "NULL" + " executed succesfully", Toast.LENGTH_LONG).show();
}
}
OrderGroupTabFragment:
public class OrderGroupTabFragment extends android.support.v4.app.Fragment{
public static final String ORDER_GROUP_LIST_ARG = "ORDER_GROUP_ITEMS";
private List<GroupItem> groupItems;
private ListView listView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
System.out.println("Creating Order List View");
View view = inflater.inflate(R.layout.fragment_order_group_tab_list, null);
groupItems = (List<GroupItem>)getArguments().getSerializable(ORDER_GROUP_LIST_ARG);
listView = (ListView)view.findViewById(R.id.order_detail_tab_list_view);
listView.setAdapter(new GroupItemAdapter(getActivity(), groupItems));
System.out.println("Order List View Created");
return view;
}
}
GroupItemAdapter:
public class GroupItemAdapter extends ArrayAdapter<GroupItem> {
private Context _context;
public GroupItemAdapter(Context context, List<GroupItem> groupItems) {
super(context, R.layout.lvi_group_item , groupItems);
this._context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
LayoutInflater li = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.lvi_group_item,
parent, false);
holder = new ViewHolder();
holder.header = (TextView)convertView.findViewById(R.id.tab_header);
holder.isVerified = (CheckBox)convertView.findViewById(R.id.tab_verification_checkbox);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
GroupItem item = getItem(position);
holder.header = (TextView)convertView.findViewById(R.id.tab_header);
holder.isVerified = (CheckBox)convertView.findViewById(R.id.tab_verification_checkbox);
if(item != null)
holder.header.setText(item.getName());
return convertView;
}
private class ViewHolder {
TextView header;
CheckBox isVerified;
}
}
fragment_order_detail.xml:
<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">
<LinearLayout android:id="#+id/order_detail_button_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
style="#android:style/Widget.ActionBar">
<Button android:id="#+id/order_detail_button_undo"
android:layout_width="0dp"
android:layout_height="wrap_content"
style="#android:style/Widget.ActionButton"
android:drawableTop="#drawable/ic_action_undo"
android:text="Undo"
android:layout_weight="1"/>
<Button android:id="#+id/order_detail_button_redo"
android:layout_width="0dp"
android:layout_height="wrap_content"
style="#android:style/Widget.ActionButton"
android:drawableTop="#drawable/ic_action_redo"
android:text="Redo"
android:layout_weight="1"/>
<Button android:id="#+id/order_detail_button_submit"
android:layout_width="0dp"
android:layout_height="wrap_content"
style="#android:style/Widget.ActionButton"
android:drawableTop="#drawable/ic_action_tick"
android:text="Submit"
android:layout_weight="1"/>
</LinearLayout>
<TextView
style="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_below="#id/order_detail_button_bar"
android:layout_alignParentStart="true"
android:id="#+id/order_detail_header"/>
<android.support.v4.app.FragmentTabHost android:id="#+id/order_detail_tab_host"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/order_detail_header">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#android:id/tabs"/>
</RelativeLayout>
</android.support.v4.app.FragmentTabHost>
fragment_order_group_tab_list.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/order_detail_tab_list_view"/>
</RelativeLayout>
lvi_group_item.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="#+id/tab_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"/>
<CheckBox android:id="#+id/tab_verification_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/tab_header"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_toEndOf="#id/tab_header"
android:text="#string/lvi_group_item_verification_succesfull"
/>
</RelativeLayout>
gr33tz gangfish
Got it working.
I took the wrong fragmentmanager to pass to the tabhost.setup() method in my OnCreateView method in the DetailFragment.
tabHost.setup(getActivity(), getChildFragmentManager() , android.R.id.tabcontent);