i have 2 fragment in my tab layout, i manage to disable the swiping function between tab layout by button click. How to implement the disable function in my onclick button. thanks for advance
MainActivity
public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener{
private TabLayout tabLayout;
private ViewPager viewPager;
int ans =2;
------FCViewPager disable;------
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
BasePagerAdapter adapter = new BasePagerAdapter(getSupportFragmentManager());
Tab1 tab1 = new Tab1();
Tab2 tab2 = new Tab2();
adapter.addFragment(tab1, "1");
adapter.addFragment(tab2, "2");
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(adapter);
tabLayout.setOnTabSelectedListener(this);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setupWithViewPager(viewPager);
}
----- public void testing( View view) {
disable.setEnableSwipe(false);
}---------
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
public class BasePagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public BasePagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
public void removeFragment(int position) {
mFragmentTitleList.remove(position);
mFragmentList.remove(position);
notifyDataSetChanged();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
Tab1
public class Tab1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View h = inflater.inflate(R.layout.tab1, container, false);
return h;
}
}
Tab2
public class Tab2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View h = inflater.inflate(R.layout.tab2, container, false);
return h;
}
You can disable swipe feature in your ViewPager. Create custom ViewPager as follow.
public class FCViewPager extends ViewPager {
private boolean enableSwipe;
public FCViewPager(Context context) {
super(context);
init();
}
public FCViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
enableSwipe = true;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return enableSwipe && super.onInterceptTouchEvent(event);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return enableSwipe && super.onTouchEvent(event);
}
public void setEnableSwipe(boolean enableSwipe) {
this.enableSwipe = enableSwipe;
}
}
In your MainActivity
//R.id.pager has to be FCViewPager not default ViewPager
viewPager = (FCViewPager) findViewById(R.id.pager);
Your button onClick function
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pager.setEnableSwipe(false);
}
});
User scroll disable viewpager for disabling and enabling scrolling by default and programmatically
public class ScrollDisabledViewpager extends ViewPager {
private boolean isPagingEnabled = false;
public ScrollDisabledViewpager(Context context) {
super(context);
}
public ScrollDisabledViewpager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
return this.isPagingEnabled && super.onTouchEvent(event);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return this.isPagingEnabled && super.onInterceptTouchEvent(event);
}
public void setPagingEnabled(boolean b) {
this.isPagingEnabled = b;
}
}
Button onClick event you can set this true/false to enable/ disable scrolling
viewPager.setPagingEnabled(true/ false);
Related
As i have Fragment there is ViewPager in which i am displaying three another fragment, now the onCreateView method is called from all three fragment.
Here is my code.
public class CustomViewPager extends ViewPager {
private boolean enabled;
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.enabled = true;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onTouchEvent(event);
}
return false;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onInterceptTouchEvent(event);
}
return false;
}
public void setPagingEnabled(boolean enabled) {
this.enabled = enabled;
}
}
Xml
<com.customView.CustomViewPager
android:id="#+id/newOrderContainerPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/layout_next_previous"
android:layout_below="#+id/newTitle"
android:layout_marginBottom="#dimen/ten_dp">
</com.customView.CustomViewPager>
Here is adapter
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {`enter code here`
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
return super.instantiateItem(container, position);
}
}
Main Fragment which display ViewPager
#Override
public View setContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_new_order, container, false);
setupViewPager(customViewPager);
customViewPager.setPagingEnabled(false);
return view;
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
adapter.addFrag(new NewOrderFragmentFirstPage(), NewOrderFragmentFirstPage.class.getName());
adapter.addFrag(new NewOrderFragmentSecondPage(), NewOrderFragmentFirstPage.class.getName());
adapter.addFrag(new NewOrderFragmentThirdPage(), NewOrderFragmentThirdPage.class.getName());
viewPager.setAdapter(adapter);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.txt_Next:
txt_Previous.setVisibility(View.VISIBLE);
if (count < 3) {
count++;
}
customViewPager.setCurrentItem(customViewPager.getCurrentItem() + 1);
textView.setText(count + "/3");
break;
case R.id.txt_previous:
if (count > 1) {
count--;
}
if (count == 1) {
txt_Previous.setVisibility(View.INVISIBLE);
}
customViewPager.setCurrentItem(customViewPager.getCurrentItem() - 1);
textView.setText(count + "/3");
break;
}
}
Please help.
Thanks
You can add the code that you want to execute only single time in onActivityCreated() this will prevent its call from all the 3 fragments
I am using ViewPager inside my Navigation Drawer all are works fine in devices other than marshmallow devices. in marshmallow devices when i clicking toggle button or swiping the navigation drawer (in both direction) feel a stuck(glitch) in the flow, also when i swipe the viewpager into different tabs also found the stuckking. This was found only in marshmallow device. please help me,, thanks in advance....
this is my code
activity class
public class HomeActivity extends AppCompatActivity{
Toolbar toolbar;
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
FragmentTransaction fragmentTransaction;
NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = (Toolbar)findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.main_container,new HomeViewPagerFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Home");
navigationView = (NavigationView)findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.home:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container,new HomeViewPagerFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Home");
item.setChecked(true);
drawerLayout.closeDrawers();
break;
}
return true;
}
});
}
#Override
protected void onPostCreate(#Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
}
ViewPager fragment
public class HomeViewPagerFragment extends Fragment {
ArrayList<Model> mArrayList= new ArrayList<>();
Model model;
TabLayout tabLayout;
ViewPager viewPager;
HomeViewPagerAdapter viewPagerAdapter;
public HomeViewPagerFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home_view_pager,container,false);
viewPager = (ViewPager)rootView.findViewById(R.id.view_pager);
// parsing and add datas into mArrayList
viewPagerAdapter = new HomeViewPagerAdapter(getActivity().getSupportFragmentManager(),mArrayList);
viewPager.setAdapter(viewPagerAdapter);
viewPager.setOffscreenPageLimit(2);
// Give the TabLayout the ViewPager
viewPager.setCurrentItem(Credentials.homeTabSelected);
tabLayout = (TabLayout)rootView.findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
#Override
public void onTabSelected(TabLayout.Tab tab){
Credentials.homeTabSelected = tab.getPosition();
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return rootView;
}
}
viewpager adapter
public class HomeViewPagerAdapter extends FragmentStatePagerAdapter {
ArrayList<Model> mArrayList = new ArrayList<>();
String tabTitles[] = new String[] { "tab1","tab2" };
public HomeViewPagerAdapter(FragmentManager fm, ArrayList<Model> arrayList) {
super(fm);
mArrayList = arrayList;
}
#Override
public Fragment getItem(int position) {
if(position==0) {
return new HomeCentralFragment();
}
else {
return HomeRecyclerFragment.newInstance(mArrayList);
}
}
#Override
public int getCount() {
return tabTitles.length;
}
#Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
HomeCentralFragment
public class HomeCentralFragment extends Fragment {
public HomeCentralFragment(){
}
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home_central,null);
//my code is here(buttons, click events etc...)
return rootView;
}
}
HomeRecyclerFragment
public class HomeRecyclerFragment extends Fragment {
RecyclerView recyclerView;
public static ArrayList<Model> mArrayList =new ArrayList<>();
Fragment fragment;
FragmentTransaction fragmentTransaction;
public static HomeRecyclerFragment newInstance(ArrayList<Model> arrayList){
HomeRecyclerFragment fragment = new HomeRecyclerFragment();
mArrayList = arrayList;
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home_recycler_view,null);
recyclerView = (RecyclerView)rootView.findViewById(R.id.card_recycler_view);
recyclerView.setHasFixedSize(true);
//to make horizontal or vertical listview using recyclerview
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
RecyclerView.Adapter adapter = new HomeRecyclerAdapter(getActivity(), mArrayList);
recyclerView.setAdapter(adapter);
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
GestureDetector gestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
#Override public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
#Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if(child != null && gestureDetector.onTouchEvent(e)) {
Credentials.selectedPolititian = rv.getChildAdapterPosition(child);
Intent intent = new Intent(getContext(), PersonDetailActivity.class);
intent.putExtra("Class","HomeActivity");
intent.putExtra("array",mArrayList);
startActivity(intent);
getActivity().finish();
}
return false;
}
#Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
return rootView;
}
}
HomeRecyclerAdapter
public class HomeRecyclerAdapter extends RecyclerView.Adapter<HomeRecyclerAdapter.ViewHolder> {
private ArrayList<Model> profiles;
Context mContext;
public HomeRecyclerAdapter(Context context, ArrayList<Model> profile) {
this.profiles = profile;
this.mContext = context;
}
#Override
public HomeRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_fragment_home_recycler, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(HomeRecyclerAdapter.ViewHolder viewHolder, int i) {
viewHolder.name.setText(profiles.get(i).getmName());
int length= profiles.get(i).getmDepartment().size();
int k;
String res ="";
for (k=0;k<length;k++){
res = res + profiles.get(i).getmDepartment().get(k)+"\n";
}
viewHolder.department.setText(res);
}
#Override
public int getItemCount() {
return profiles.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView name, department;
//private ImageView imageView;
public ViewHolder(View view) {
super(view);
// imageView = (ImageView) view.findViewById(R.id.image);
name = (TextView) view.findViewById(R.id.p_name);
department = (TextView) view.findViewById(R.id.p_department);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = getAdapterPosition();
}
});
}
}
}
check view fragments layout, avoid .jpeg images (use .png images)...
check the view pager with some simple fragments, if it work smoothly check the layout views..
I have a viewpager with swipeable fragments and also a radiogroup that shows which view is selected. When I start the application, the first radiobutton is not selected, it is only selected when I swipe another view and get back. I am a beginner, so please share your knowledge. Here's my code:
Viewpager Adapter:
public class ViewPagerAdapter extends FragmentPagerAdapter {
public static int int_items = 3;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new WelcomeOneFragment();
case 1:
return new WelcomeTwoFragment();
case 2:
return new WelcomeThreeFragment();
}
return null;
}
#Override
public int getCount() {
return int_items;
}
Fragment with the radiogroup:
public class TabFragment extends BaseFragment implements ViewPager.OnPageChangeListener, View.OnClickListener {
public static ViewPager mViewPager;
private Button mButtonMassage;
private RadioGroup mRadioGroup;
public static TabFragment newInstance() {
return new TabFragment();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tab, container, false);
initUi(rootView);
return rootView;
}
#Override
public void initUi(View rootView) {
mRadioGroup = (RadioGroup) rootView.findViewById(R.id.radio_group);
mViewPager = (ViewPager) rootView.findViewById(R.id.view_pager);
mButtonMassage = (Button) rootView.findViewById(R.id.buttonMassage);
initListeners();
}
#Override
public void initListeners() {
mButtonMassage.setOnClickListener(this);
mViewPager.addOnPageChangeListener(this);
initData();
}
#Override
public void initData() {
mViewPager.setAdapter(new ViewPagerAdapter(getChildFragmentManager()));
mViewPager.setCurrentItem(0);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonMassage:
Intent i = new Intent(getActivity(), MassageActivity.class);
startActivity(i);
break;
default:
break;
}
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
mRadioGroup.check(mRadioGroup.getChildAt(position).getId());
}
#Override
public void onPageScrollStateChanged(int state) {
}
}
just call this
mRadioGroup.check(mRadioGroup.getChildAt(0).getId());
after
mViewPager.setCurrentItem(0);
as listner will not callup in starting and you have to set it for manually after setting adapter to viewpager
hope it will work for you!! :)
This is a pretty common issue with ViewPager. onPageSelected is not called upon start, so I would just call:
mRadioGroup.check(mRadioGroup.getChildAt(0).getId());
In your initUi() method.
I am developing an android application in which i used android view pager for swipe between 4 fragments.swiping between the fragments performing well.what i need is i want to disable the swipe until operations on the first fragment completed.if all the necessary operations performed at the particular fragment swiping has to enable from that fragment and so on until the last fragment.what should i do for this.can any one have the suggetions?.please remember that i need to control the swiping from all fragments.please help me out.thanks in advance..
public class MyPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPagerAdapter(FragmentManager fm) {
super(fm);
this.fragments = new ArrayList<Fragment>();
fragments.add(new SalesTransactionFragment());
fragments.add(new FragmentCollection());
// fragments.add(new FragmentBlue());
fragments.add(new FragmentGreen());
fragments.add(new FragmentPink());
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
}
MainActivity
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyPagerAdapter pageAdapter = new MyPagerAdapter(getSupportFragmentManager());
ViewPager pager = (ViewPager)findViewById(R.id.myViewPager);
pager.setOffscreenPageLimit(4);
pager.setAdapter(pageAdapter);
}
One of the Fragment
public class FragmentPink extends Fragment {
Button b;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_pink, container, false);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
b=(Button) getActivity().findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getActivity(),MainActivity.class);
startActivity(i);
}
});
}
}
One approach would be to set OnTouchListener to the ViewPager like this -
mPager.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(mPaginEnabled)
return false;
else
return true;
}
});
You need to subclass ViewPager
public class MyViewPager extends ViewPager {
private boolean mSwipable = true;
public MyViewPager(Context context) {
super(context);
}
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return mSwipable ? super.onInterceptTouchEvent(event) : false;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
return mSwipable ? super.onTouchEvent(event) : false;
}
public boolean isSwipable() {
return mSwipable;
}
public void setSwipable(boolean swipable) {
mSwipable = swipable;
}
}
Now you can enable/disable swipes by calling setSwipable method.
UPD: To edit this property in XML the class will be:
public class MyViewPager extends ViewPager {
private boolean mSwipable = true;
public MyViewPager(Context context) {
super(context);
}
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyViewPager);
try {
mSwipable = a.getBoolean(R.styleable.MyViewPager_swipeable, true);
} finally {
a.recycle();
}
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return mSwipable ? super.onInterceptTouchEvent(event) : false;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
return mSwipable ? super.onTouchEvent(event) : false;
}
public boolean isSwipable() {
return mSwipable;
}
public void setSwipable(boolean swipable) {
mSwipable = swipable;
}
}
And your values/attr.xml:
<declare-styleable name="MyViewPager">
<attr name="swipeable" format="boolean" />
</declare-styleable>
Now you can use it in your layout:
<my.package.name.MyViewPager
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/viewPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:swipeable="false"/>
I developed an android application, as you see in this picture,
I used sherlock action bar for searching, the only problem is that when the user search something in web tab if he/she wants to see the image result, he should type and search again in image tab, is there any way to pass the edittext parameter to other tabs and do searching when user changed the tabs?
MainActivity:
public class MainActivity extends SherlockFragmentActivity implements
TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private com.actionbarsherlock.app.ActionBar actionBar;
private String[] tabs = { "Sound", "Image", "Web" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
actionBar.setHomeButtonEnabled(false);
viewPager.setAdapter(mAdapter);
viewPager.setOffscreenPageLimit(3);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText(tabs[0])
.setTabListener(this), false);
actionBar.addTab(actionBar.newTab().setText(tabs[1])
.setTabListener(this), false);
actionBar.addTab(actionBar.newTab().setText(tabs[2])
.setTabListener(this), true);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabSelected(com.actionbarsherlock.app.ActionBar.Tab tab,
android.support.v4.app.FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(com.actionbarsherlock.app.ActionBar.Tab tab,
android.support.v4.app.FragmentTransaction ft) {
}
#Override
public void onTabReselected(com.actionbarsherlock.app.ActionBar.Tab tab,
android.support.v4.app.FragmentTransaction ft) {
}
}
Adapter:
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new Sound();
case 1:
return new Image();
case 2:
return new Search();
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
Sound Frgment:
public class Sound extends Fragment {
private String URL_ADDRESS, Servlet;
private ListView listview;
private ListViewAdapter adapter;
private List<Spanned> numberlist = new ArrayList<Spanned>();
private List<Ava2> avaList = new ArrayList<Ava2>();
private Boolean isInternetPresent = false;
private ConnectionDetector detector;
private View rootView;
private RelativeLayout loading, layout;
private int counter = 10;
private EditText editText;
private String a = "http://***";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.ava, container, false);
ImageButton voiceSearch = (ImageButton) rootView
.findViewById(R.id.imageButton2);
ImageButton buttonSearch = (ImageButton) rootView
.findViewById(R.id.imageButton1);
listview = (ListView) rootView.findViewById(R.id.listview);
loading = (RelativeLayout) rootView.findViewById(R.id.loading);
layout = (RelativeLayout) rootView.findViewById(R.id.layout);
editText = (EditText) rootView.findViewById(R.id.editText1);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
search();
return true;
}
return false;
}
});
buttonSearch.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
search();
}
});
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long l) {
try {
Intent i = new Intent(getActivity(), Play.class);
startActivity(i);
} catch (Exception e) {
}
}
}
});
return rootView;
}
First,you could write a interface:
public interface OnProfileDataPass {
public String getCurrentSearchword();
public void setCurrentSearchword(String searchword);
}
Then in your fragments:
public class WebFragment extends Fragment {
private OnProfileDataPass dataPasser;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
dataPasser = (OnProfileDataPass) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnProfileDataPass");
}
searchword = dataPasser.getCurrentSearchword();
}
}
In your activity:
public class MainActivity implements OnProfileDataPass {
String currentSearchword;
#Override
public String getCurrentSearchword() {
return currentSearchword;
}
#Override
public String setCurrentSearchword(String searchword) {
this.currentSearchword = searchword;
}
}
That's my solution.When you change the tap,you could listen the proper event and set the word with getActivity.setCurrentSearchword(String newword),then you could get the newword in the fragment's dataPasser variable's method getCurrentSearchword() to get the search word. I have test it in my app. Wish that helps you a little.
you should save the value of the editText in your activity and then pass it as an argument when you call your fragment, using the same technique as the one described here: http://developer.android.com/reference/android/app/Fragment.html