Tuesday, 25 November 2014

MainActivity with ViewPager | FragmentPagerAdapter | Action button with 3 Textview



My Query is when I will click on text id: id_overview the fragment page move on Overview fragment and when I click on id_highlight the fragment move on highlight fragment when I click on id_amenity the fragment move on Aminity fragment and both should happen sliding as well as fragment moves. My problem is how to write code on MainActivity page to navigation move text from one text to another also click action on text item happen to move one fragment to another. CatLog shows error: android.view.InflateException: Binary XML file line #7: Error inflating class android.support.V4.view.ViewPager.


MainActivity.java class



public class MainActivity extends FragmentActivity {
ViewPager viewPager;
FragmentPageAdapter ft;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = new ViewPager(this);
viewPager.setId(R.id.pager);
setContentView(viewPager);
initialingpaging();
}
private void initialingpaging() {
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, Overview.class.getName()));
fragments.add(Fragment.instantiate(this, Highlight.class.getName()));
fragments.add(Fragment.instantiate(this, Amenity.class.getName()));
ft = new FragmentPageAdapter(getSupportFragmentManager());
viewPager.setAdapter(ft);
}
}


FragmentPageAdapter



public class FragmentPageAdapter extends FragmentPagerAdapter{
public FragmentPageAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int arg0) {
switch (arg0) {
case 0:
return new Overview();
case 1:
return new Highlight();
case 2:
return new Amenity();
default:
break;
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}


Overview.java, Highlight.java and Amenity.java class are similar code as below:



public class Overview extends Fragment {
View myFragmentView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle
savedInstanceState)
{
myFragmentView = inflater.inflate(R.layout.overview_layout, container, false);
return myFragmentView;
}
}


overview.xml, highlight.xml and amenity.xml codes are similar for three for three fragments.



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://ift.tt/nIICcg"
android:id="@+id/my_amenity"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Amenity Page goes... here."
android:gravity="center"/>
</LinearLayout>


activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<android.support.V4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="70dp"
/>

<TextView
android:id="@+id/id_overview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Overview"
android:textStyle="bold" />

<TextView
android:id="@+id/id_highlight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Highlight"
android:textSize="20sp"
android:textStyle="bold" />

<TextView
android:id="@+id/id_amenity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Amenity"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>

No comments:

Post a Comment