I create a ViewPager with PageLineIndicator and ViewPager contain three Fragments(FragmentA,FragmentB,FragmentC) and it's working.
Now I want to create gridview Button click menu in FragmentA which is on click position in gridview called next fragmentB and if I click next position then called FragmentC.
Here below is code of viewpager which we create:
MainActivity
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import com.viewpagerindicator.LinePageIndicator;
public class MainActivity extends FragmentActivity {
FragmentAdapter mAdapter;
ViewPager mPager;
LinePageIndicator mIndicator;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new FragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (LinePageIndicator) findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
}
public void switchToFragmentA(){
mPager.setCurrentItem(0);
}
public void switchToFragementB(){
mPager.setCurrentItem(1);
}
public void switchToFragmentC(){
mPager.setCurrentItem(2);
}
}
fragmentAdapter
package com.demo.demoproject;
import com.viewpagerindicator.IconPagerAdapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class FragmentAdapter extends FragmentPagerAdapter implements IconPagerAdapter{
public FragmentAdapter(FragmentManager supportFragmentManager) {
// TODO Auto-generated constructor stub
super(supportFragmentManager);
}
@Override
public int getIconResId(int index) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
Fragment fragment = new Fragment();
switch(position){
case 0:
fragment = new FragmentA();
break;
case 1:
fragment = new FragmentB();
break;
case 2:
fragment = new FragmentC();
break;
}
return fragment;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
FragmentA
package com.demo.demoproject;
import java.util.ArrayList;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class FragmentA extends Fragment {
GridView gridView;
ArrayList<Item> gridArray = new ArrayList<Item>();
CustomGridViewAdapter customGridAdapter;
public FragmentA() {
// 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_a, container, false);
/*Button button = (Button) view.findViewById(R.id.Button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
((MainActivity)getActivity()).switchToFragmentB();
}
});*/
Bitmap homeIcon = BitmapFactory.decodeResource(this.getResources(),
R.drawable.home);
Bitmap userIcon = BitmapFactory.decodeResource(this.getResources(),
R.drawable.personal);
gridArray.add(new Item(homeIcon, "Home"));
gridArray.add(new Item(userIcon, "User"));
gridArray.add(new Item(homeIcon, "House"));
gridArray.add(new Item(userIcon, "Friend"));
gridArray.add(new Item(homeIcon, "Home"));
gridArray.add(new Item(userIcon, "Personal"));
gridArray.add(new Item(homeIcon, "Home"));
gridArray.add(new Item(userIcon, "User"));
gridArray.add(new Item(homeIcon, "Building"));
gridView = (GridView) view.findViewById(R.id.gridView1);
customGridAdapter = new CustomGridViewAdapter(getActivity(),
R.layout.row_grid, gridArray);
gridView.setAdapter(customGridAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
switch (position) {
case 0:
((MainActivity) getActivity()).switchToFragementB();
break;
case 1:
((MainActivity) getActivity()).switchToFragmentC();
break;
}
}
});
return view;
}
}
CustomGridViewAdapter class
package com.demo.demoproject;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomGridViewAdapter extends ArrayAdapter<Item> {
Context context;
int layoutResourceId;
ArrayList<Item> data = new ArrayList<Item>();
public CustomGridViewAdapter(Context context, int layoutResourceId,
ArrayList<Item> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RecordHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RecordHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
row.setTag(holder);
} else {
holder = (RecordHolder) row.getTag();
}
Item item = data.get(position);
holder.txtTitle.setText(item.getTitle());
holder.imageItem.setImageBitmap(item.getImage());
return row;
}
static class RecordHolder {
TextView txtTitle;
ImageView imageItem;
}
}
Item class
package com.demo.demoproject;
import android.graphics.Bitmap;
/**
*
* @author manish.s
*
*/
public class Item {
Bitmap image;
String title;
public Item(Bitmap image, String title) {
super();
this.image = image;
this.title = title;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
activity_main.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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="0dp"
android:layout_weight="1" />
<com.viewpagerindicator.LinePageIndicator
android:id="@+id/indicator"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:padding="5dip" />
</LinearLayout>
fragment_a.xml
<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"
android:paddingTop="70dp" >
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</LinearLayout>
row_grid.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="@+id/item_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:src="@drawable/ic_launcher" >
</ImageView>
<TextView
android:id="@+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="15sp" >
</TextView>
</LinearLayout>
this code for navigate in main_activity..
in main Activity
public void switchToFragmentA(){
mPager.setCurrentItem(0);
}
public void switchToFragementB(){
mPager.setCurrentItem(1);
}
public void switchToFragmentC(){
mPager.setCurrentItem(2);
}
And call it from FragmentA
FragmentA class
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
switch (position) {
case 0:
((MainActivity) getActivity()).switchToFragementB();
break;
case 1:
((MainActivity) getActivity()).switchToFragmentC();
break;
}
}
});