Saturday 26 July 2014

Android Example With ViewPager for Swiping Left to Right and Viceversa

ViewPager With Fragment in Android

Android Example With ViewPager for Swiping Left to Right and Viceversa

The Project Contains Swiping of Contact from Left to Right and ViceVersa.

MainActivity.java

public class MainActivity extends ActionBarActivity {

private static final int NUMBER_OF_PAGES = 10;

private ViewPager mViewPager;
private DataFragmentPagerAdapter mMyFragmentPagerAdapter;

ArrayList<Contact> contactsList;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.viewpager);

contactsList = new ArrayList<Contact>();

prepareData();


mMyFragmentPagerAdapter = new DataFragmentPagerAdapter(
getSupportFragmentManager(), contactsList);
mViewPager.setAdapter(mMyFragmentPagerAdapter);
}

private void prepareData() {

contactsList.add(new Contact("pratap1", "5121390971"));
contactsList.add(new Contact("pratap2", "5121390972"));
contactsList.add(new Contact("pratap3", "5121390973"));
contactsList.add(new Contact("pratap4", "5121390974"));
contactsList.add(new Contact("pratap5", "5121390975"));
contactsList.add(new Contact("pratap6", "5121390976"));
contactsList.add(new Contact("pratap7", "5121390977"));

}


}


PageFragment .java

public class PageFragment extends Fragment {
 
    public static PageFragment newInstance(Contact singleContact) {

        PageFragment pageFragment = new PageFragment();
        Bundle bundle = new Bundle();
      /*  bundle.putString("name", singleContact.getName());
        bundle.putString("phone", singleContact.getPhone());
        pageFragment.setArguments(bundle);*/
     
        bundle.putSerializable("contact", singleContact);
        pageFragment.setArguments(bundle);
     
        return pageFragment;
    }
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
   
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         
        View view = inflater.inflate(R.layout.fragment, container, false);
        final TextView textView1 = (TextView) view.findViewById(R.id.textView1);
        final TextView textView2 = (TextView) view.findViewById(R.id.textView2);
     
        Contact cont= (Contact) getArguments().getSerializable("contact");
     
        textView1.setText(cont.getName());
        textView2.setText(cont.getPhone());
     
        Button btnClick=(Button) view.findViewById(R.id.btnClick);
     
        btnClick.setOnClickListener(new  OnClickListener() {

@Override
public void onClick(View v) {

Toast.makeText(getActivity(), "Clciked "+textView1.getText().toString(), Toast.LENGTH_SHORT).show();

}
});
     
     
        return view;
    }
}


DataFragmentPagerAdapter.java

public  class DataFragmentPagerAdapter extends FragmentPagerAdapter {

ArrayList<Contact> users;

public DataFragmentPagerAdapter(FragmentManager fm, ArrayList<Contact> usersList) {
super(fm);
this.users=usersList;
}

@Override
public Fragment getItem(int index) {

return PageFragment.newInstance(users.get(index));
}

@Override
public int getCount() {

return users.size();
}
}


Contact.java

public class Contact implements Serializable{

private String name;

public Contact(String name, String phone) {
super();
this.name = name;
this.phone = phone;
}

private String phone;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

}


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.pratap.androidviewpager.MainActivity"
     >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       />

</RelativeLayout>

fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Click ME" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:text="Medium Text"
        android:layout_margin="5dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />
 
</RelativeLayout>


Screenshots 




Download Source code for the entire project

No comments:

Post a Comment