I have used FrameLayout for showing overlay screen. While instantiating RelativeLayout, findViewById returns null.
Here is the xml file:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<me.relex.circleindicator.CircleIndicator
android:id="#+id/indicator"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginBottom="48dp"
android:layout_gravity="bottom"
app:ci_drawable="#drawable/orange_radius"
app:ci_drawable_unselected="#drawable/white_radius"/>
</RelativeLayout>
<!--Below is the transparent layout positioned at startup -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#70000000"
android:id="#+id/topLayout">
<ImageView
android:id="#+id/ivInstruction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="25dp"
android:layout_marginRight="15dp"
android:clickable="false"
android:paddingLeft="20dip"
android:scaleType="center"
android:src="#drawable/home" />
</RelativeLayout>
Fragment code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = getActivity().getLayoutInflater().inflate(R.layout.our_work_layout, null);
topLayout = (RelativeLayout) mView.findViewById(R.id.topLayout);
if (isFirstTime()) {
topLayout.setVisibility(View.INVISIBLE);
}
ViewPager viewpager = (ViewPager) mView.findViewById(R.id.viewpager);
// mPager.setAdapter(mAdapter);
ImageAdapter adapter = new ImageAdapter(act);
viewpager.setAdapter(adapter);
CircleIndicator indicator = (CircleIndicator) mView.findViewById(R.id.indicator);;
indicator.setViewPager(viewpager);
return mView;
}
private boolean isFirstTime()
{
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean ranBefore = preferences.getBoolean("RanBefore", false);
if (!ranBefore) {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("RanBefore", true);
editor.commit();
topLayout.setVisibility(View.VISIBLE);
topLayout.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
topLayout.setVisibility(View.INVISIBLE);
return false;
}
});
}
return ranBefore;
}
Your problem is here:
ViewPager viewpager = (ViewPager) mView.findViewById(R.id.viewpager);
There is no "viewpager" in this layout. You used the id "pager".
Correct this line to the following:
ViewPager viewpager = (ViewPager) mView.findViewById(R.id.pager);
Related
I'm integrating ViewPager and ScrollView on android but it doesn't seem to work. The ViewPager scroll works just fine but the ScrollView does not work at all on all the solutions I've tried. Below is my MainActivity class and xml layout.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_drawer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
hostDescription = (TextView) findViewById(R.id.host_description);
hostDescription.setTypeface(quickSandRegular);
hostDescription.setTextColor(Color.BLACK);
scrollView = (ScrollView) findViewById(R.id.scroll);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View header=navigationView.getHeaderView(0);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter();
viewPager.setAdapter(adapter);
// set navigation font
Menu m = navigationView.getMenu();
for (int i=0;i<m.size();i++) {
MenuItem mi = m.getItem(i);
// apply font to subment
SubMenu subMenu = mi.getSubMenu();
if (subMenu!=null && subMenu.size() >0 ) {
for (int j=0; j <subMenu.size();j++) {
MenuItem subMenuItem = subMenu.getItem(j);
applyFontToMenuItem(subMenuItem);
}
}
applyFontToMenuItem(mi);
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
}
xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- here insert your scrollview and all views you need for this page this is actually your page -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:fillViewport="true"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/LinearLayoutMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/blue"
android:orientation="vertical" >
<TextView
android:id="#+id/welcome_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:layout_marginBottom="30dp"
android:textColor="#android:color/holo_green_light"
android:layout_marginLeft="17dp"
android:background="#color/blue"
android:textSize="26dp"/>
<TextView
android:id="#+id/intro_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:layout_marginBottom="10dp"
android:text="Save on your next adventure."
android:textColor="#android:color/holo_green_light"
android:layout_marginLeft="17dp"
android:background="#color/blue"
android:textSize="28dp"/>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#cccccc" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical" >
<TextView
android:id="#+id/host_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Become a Host"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:textSize="21dp"/>
<!--<ImageView
android:id="#+id/image1"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingBottom="10dp"
android:scaleType="fitXY"
android:src="#drawable/sv" />-->
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingRight="20dp"
/>
<TextView
android:id="#+id/host_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Earn money sharing your extra space"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:textSize="17dp"/>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#cccccc" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical" >
<TextView
android:id="#+id/host_telxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Become a Host"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:textSize="21dp"/>
<!--<ImageView
android:id="#+id/image1"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingBottom="10dp"
android:scaleType="fitXY"
android:src="#drawable/sv" />-->
<TextView
android:id="#+id/host_escription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Earn money sharing your extra space"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:textSize="17dp"/>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#cccccc" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
ImageAdapter
class ImageAdapter extends PagerAdapter {
#Override
public int getCount() {
return IMAGES.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(getApplicationContext());
imageView.setImageResource(IMAGES[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
You need to implement ViewPager.OnPageChangeListener and write your logic in onPageScrollStateChanged
#Override
public void onPageScrollStateChanged(int state) {
ScrollView.requestDisallowInterceptTouchEvent(!(!lastPage && state == ViewPager.SCROLL_STATE_IDLE));
}
You need to write logic to find if you are on last page or not which is simple.
Solved the problem by customizing layout_height of the ViewPager. Below is the code:
<android.support.v4.view.ViewPager
android:id="#+id/view_pager2"
android:layout_width="match_parent"
android:layout_height="300dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"/>
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter();
viewPager.setAdapter(adapter);
viewPager.setClipToPadding(false);
viewPager.setPadding(70, 0, 70, 0);
class ImageAdapter extends PagerAdapter {
#Override
public int getCount() {
return IMAGES.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(getApplicationContext());
imageView.setImageResource(IMAGES[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
#Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
setPrimaryItem((View) container, position, object);
// lastPage = IMAGES[position]==object;
}
}
Tabhost + viewpager horizontal scroll. if the fragment is empty or contains a textview, it works, but if the fragment contains a list view,, fragment it's doesn't scroll horizontal !!
if I try to scroll from the textview it works or if the fragment is empty it's work , but from the listview no, noted that the textview its width and height are wrap_content while those in the listview are match_parent
frgament 2 (it contains the fragment of tabhost )
fragment.XML
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/main_content1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RESEAU"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"/>
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#000000" />
<HorizontalScrollView
android:id="#+id/hScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
</HorizontalScrollView>
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</FrameLayout>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
fragment2.java
public class Fragment2 extends android.support.v4.app.Fragment implements OnTabChangeListener, OnPageChangeListener {
private FragmentTabHost mTabHost;
private ViewPager viewPager;
private MyFragmentPagerAdapter myViewPagerAdapter;
View v;
public Fragment2() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment2, container, false);
viewPager = (ViewPager) v.findViewById(R.id.pager);
// init tabhos
this.initializeTabHost(savedInstanceState);
// init ViewPager
this.initializeViewPager();
return v;
}
// fake content for tabhost
class FakeContent implements TabHost.TabContentFactory {
private final Context mContext;
public FakeContent(Context context) {
mContext = context;
}
#Override
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumHeight(0);
v.setMinimumWidth(0);
return v;
}
}
private void initializeViewPager() {
List<android.support.v4.app.Fragment> fragments = new Vector<android.support.v4.app.Fragment>();
fragments.add(new TramHor());
fragments.add(new BusHor());
fragments.add(new Train());
this.myViewPagerAdapter = new MyFragmentPagerAdapter(
getChildFragmentManager(), fragments);
this.viewPager = (ViewPager) v.findViewById(R.id.pager);
this.viewPager.setAdapter(this.myViewPagerAdapter);
this.viewPager.setOnPageChangeListener(this);
}
private void initializeTabHost(Bundle args) {
mTabHost = (FragmentTabHost) v.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("", getResources().getDrawable(R.drawable.tram)),
TramHor.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("", getResources().getDrawable(R.drawable.bus)),
BusHor.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("", getResources().getDrawable(R.drawable.train)),
Train.class, null);
mTabHost.setOnTabChangedListener(this);
}
public void onTabChanged(String tabId) {
int pos = this.mTabHost.getCurrentTab();
this.viewPager.setCurrentItem(pos);
HorizontalScrollView hScrollView = (HorizontalScrollView) v.findViewById(R.id.hScrollView);
View tabView = mTabHost.getCurrentTabView();
int scrollPos = tabView.getLeft()
- (hScrollView.getWidth() - tabView.getWidth()) / 2;
hScrollView.smoothScrollTo(scrollPos, 0);
}
public void onPageScrollStateChanged(int arg0) {
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageSelected(int position) {
this.mTabHost.setCurrentTab(position);
}
}
MyFragmentPageAdapter.java
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
List<android.support.v4.app.Fragment> fragments;
public MyFragmentPagerAdapter(FragmentManager fm, List<android.support.v4.app.Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
}
a fragment of tabhost
<LinearLayout
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"
android:orientation="vertical"
tools:context="com.example.getgpslocation.fragment.TramHor">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LIGNE/DIRECTION"
android:layout_marginTop="70dp"
android:layout_marginLeft="20dp"/>
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#000000"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lv_sliding_menu2"
android:background="#FFFFFF"
android:choiceMode="singleChoice"
android:layout_gravity="start"/>
</LinearLayout>
I found the solution, viewpager in xml was not in the right place
<LinearLayout
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.example.getgpslocation.fragment.Fragment2"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/main_content1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RESEAU"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"/>
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#000000" />
<HorizontalScrollView
android:id="#+id/hScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none"/>
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#+id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
I have an image slider with the bottom code:
public class MainActivity extends Activity {
private class ImagePagerAdapter extends PagerAdapter {
private final int[] mImages = new int[] {
R.drawable.chiang_mai,
R.drawable.himeji,
R.drawable.petronas_twin_tower,
R.drawable.ulm
};
#Override
public void destroyItem(final ViewGroup container, final int position, final Object object) {
((ViewPager) container).removeView((ImageView) object);
}
#Override
public int getCount() {
return this.mImages.length;
}
#Override
public Object instantiateItem(final ViewGroup container, final int position) {
final Context context = MainActivity.this;
final ImageView imageView = new ImageView(context);
final int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(this.mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public boolean isViewFromObject(final View view, final Object object) {
return view == ((ImageView) object);
}
}
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
final ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
final CirclePageIndicator circleIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
circleIndicator.setViewPager(viewPager);
final TextView tvText = (TextView) findViewById(R.id.text);
tvText.setText(getResources().getString(R.string.bodyText));
}
}
And this is the XML code:
<LinearLayout
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
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:padding="10dip"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
So I need to set a text Hover on the Viewpager so that when I swipe it, it will show up for a specific picture. How can I create a hover text on this image slider while making it run automatically for some specific time, and after a swipe show a hover text.
Have you just one XML Layout? If you've two it makes it quite alot easier to work with.
So this layout below is where the actual image slider will be place on the Home Fragment in this case.
<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.example.****.****.HomeFragment"
android:background="#000000"
>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="fill_parent"
android:layout_height="204dp"
android:scaleType="centerCrop"
></android.support.v4.view.ViewPager>
Secondly, this XML Layout below is the Layout for the ViewPager itself. As you can see it is made up of an ImageView and a TextView. With the TextView placed on top of the ImageView.
Hope this helps a bit.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scaleType="centerCrop"
/>
<TextView
android:id="#+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/image_view"
android:layout_alignTop="#+id/image_view"
android:layout_alignRight="#+id/image_view"
android:layout_alignBottom="#+id/image_view"
android:layout_margin="1dp"
android:gravity="bottom"
android:textSize="35dp"
android:text="******"
android:textStyle="bold"
android:textColor="#17baef" />
</RelativeLayout>
Please Help me.
I searched everywhere how I can make a ScrollView inside a ViewPager but it was not successfully. The hole layout of the ViewPager should be scrollable.
Important: I only want a ScrollView in the vertical.
Already now I want to say thanks for the answers!
My code
"Main"
public class Rezepte2 extends Fragment {
// Declare Variables
ViewPager viewPager;
PagerAdapter adapter;
String[] rezeptTitel;
String[] naehrwerte;
String[] zubereitung;
int[] rezeptImage;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from viewpager_main.xml
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.rezepte_fragment, container, false);
// Generate sample data
rezeptTitel = new String[] { "Nudeln","Fleisch" };
naehrwerte = new String[] { "50kcal","70kcal" };
zubereitung = new String[] { "ganz easy","voll easy" };
rezeptImage = new int[] { R.drawable.ic_action_next_item, R.drawable.ic_action_next_item,
R.drawable.ic_launcher, R.drawable.ic_action_next_item };
// Locate the ViewPager in viewpager_main.xml
viewPager = (ViewPager) layout.findViewById(R.id.viewPager);
// Pass results to ViewPagerAdapter Class
adapter = new ViewPagerAdapter(getActivity(), rezeptTitel, naehrwerte, zubereitung, rezeptImage);
// Binds the Adapter to the ViewPager
viewPager.setAdapter(adapter);
return layout;
}
Adapter
public class ViewPagerAdapter extends PagerAdapter {
// Declare Variables
Context context;
String[] rezeptTitel;
String[] naehrwerte;
String[] zubereitung;
int[] rezeptImages;
LayoutInflater inflater;
public ViewPagerAdapter(Context context, String[] rezeptTitel, String[] naehrwerte,
String[] zubereitung, int[] rezeptImages) {
this.context = context;
this.rezeptTitel = rezeptTitel;
this.naehrwerte = naehrwerte;
this.zubereitung = zubereitung;
this.rezeptImages = rezeptImages;
}
#Override
public int getCount() {
return rezeptTitel.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
// Declare Variables
TextView txtrezeptTitel;
TextView txtnaehrwerte;
TextView txtzubereitung;
ImageView imagerezept;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.rezepte_fragment_basic, container,
false);
// Locate the TextViews in viewpager_item.xml
txtrezeptTitel = (TextView) itemView.findViewById(R.id.rezeptTitel);
txtnaehrwerte = (TextView) itemView.findViewById(R.id.naehrwerte);
txtzubereitung = (TextView) itemView.findViewById(R.id.zubereitung);
// Capture position and set to the TextViews
txtrezeptTitel.setText(rezeptTitel[position]);
txtnaehrwerte.setText(naehrwerte[position]);
txtzubereitung.setText(zubereitung[position]);
// Locate the ImageView in viewpager_item.xml
imagerezept = (ImageView) itemView.findViewById(R.id.image_rezepte_basic);
// Capture position and set to the ImageView
imagerezept.setImageResource(rezeptImages[position]);
// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}
}
SwipeView xml
`
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<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"/>
</LinearLayout>`
ViewPager fragment xml
`
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_red_light" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/rezeptTitel"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/ic_launcher"
android:id="#+id/image_rezepte_basic"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/naehrwerte" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/zubereitung" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>ยด
maybe you're just not scrolling correctly - an emulator doesn't really have a touch screen. You have to touch and hold part of it while panning with another finger
When I switch to the tab with this fragment, it always crashes. The logcat seems to say that it crashes on `mGrid = (GridView) (getView().findViewById(R.id.gridViewRed));
Any idea why?
RedScorerFragment.java
public class RedScorerFragment extends SherlockFragment {
LayoutInflater infl;
GridView mGrid;
#Override
public void onCreate(Bundle savedInstanceState){
mGrid = (GridView) (getView().findViewById(R.id.gridViewRed));
mGrid.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_MOVE)
return true;
return false;
}
});
mGrid.setAdapter(new ImageAdapter(getActivity()));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
infl = inflater;
return inflater.inflate(R.layout.fragment_score_red, container, false);
}
fragment_score_red.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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=".RedScorerFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/gridLabel__red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:text="#string/grid_label__red"
android:background="#color/white"
/>
<GridView
android:id="#+id/gridViewRed"
android:layout_width="wrap_content"
android:layout_height="350dp"
android:layout_alignParentLeft="true"
android:layout_below="#id/gridLabel__red"
android:columnWidth="40dp"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="7dp"
android:gravity="center"
android:background="#color/white"
>
</GridView>
<LinearLayout
android:id="#+id/linLayoutSide__red"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/gridView__red"
android:layout_alignParentLeft="true"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/weighted_label"
/>
<EditText
android:id="#+id/waitRed__red"
android:inputType="number"
android:maxLength="1"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="center"
android:textColor="#color/red"
/>
<EditText
android:id="#+id/waitBlue__red"
android:inputType="number"
android:maxLength="1"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="center"
android:textColor="#color/blue" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
You need to rethink where you're finding your views. This is because onCreate() is called before onCreateView()
So you should move
mGrid = (GridView) (getView().findViewById(R.id.gridViewRed));
mGrid.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_MOVE)
return true;
return false;
}
});
mGrid.setAdapter(new ImageAdapter(getActivity()));
to onCreateView() This means that you need to not return the inflation, but instead work with it,
So your onCreateView() should be something like:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
infl = inflater;
View mView = inflater.inflate(R.layout.fragment_score_red, container, false);
mGrid = (GridView) (mView.findViewById(R.id.gridViewRed));
//rest of code above
return mView;
}