Implement Swipe Views

Implement Swipe View in Android

MainClass

package com.example.malik.test;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity{

ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

viewPager = (ViewPager) findViewById(R.id.view_pager);
SwipeAdapter swipeAdapter = new SwipeAdapter(getSupportFragmentManager());
viewPager.setAdapter(swipeAdapter);

}

}

MainXML

<?xml version=”1.0″ encoding=”utf-8″?>
<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”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”com.example.malik.test.MainActivity”
>

<android.support.v4.view.ViewPager
android:id=”@+id/view_pager”
android:layout_width=”500dp”
android:layout_height=”match_parent”

/>
</RelativeLayout>

 

PageFragment CLASS

package com.example.malik.test;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A simple {@link Fragment} subclass.
*/
public class PageFragment extends android.support.v4.app.Fragment {

TextView textView;

public PageFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_page, container, false);
textView = (TextView) view.findViewById(R.id.textView123);
Bundle bundle = getArguments();
String message = Integer.toString(bundle.getInt(“count”));
textView.setText(“This is the ” + message + ” Swipe view Page”);

return view;
}

}

 

page_fragment 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=”.PageFragment”
>
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Large Text”
android:id=”@+id/textView123″
android:textSize=”40dp”
android:textStyle=”bold”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”83dp” />
</RelativeLayout>

 

SwipeAdapter CLASS

package com.example.malik.test;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

/**
* Created by malik on 14/06/2016.
*/
public class SwipeAdapter extends FragmentStatePagerAdapter {
public SwipeAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int i) {
Fragment fragment = new PageFragment();
Bundle bundle = new Bundle();
bundle.putInt(“count”, i+1);
fragment.setArguments(bundle);

return fragment;
}

@Override
public int getCount() {
return 5;
}
}

 

Output :

pic

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.