I need to implement Horizontally Scrollable Tabs inside a Fragment inside an already created Activity. But the Inventory class naturally extends Fragment and if I try to extend FragmentActivity from Inventory, it doesn't work.
Inside this:
Put something like this:
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Inventory extends FragmentActivity {
ViewPager viewPager = null;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.inventory);
viewPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fragmentManager));
}
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
android.support.v4.app.Fragment fragment = null;
if (position==0){
fragment = new Fa();
}
if (position==1){
fragment = new Fb();
}
if (position==2){
fragment = new Fc();
}
return fragment;
}
#Override
public int getCount() {
return 0;
}
}
Also, I am using this tutorial to implement tabs.
Related
I'm working in an app. I have an activity where I have my menu, I select the options and instance a new fragment, it depends of the option of course. But now I have to create a image swiper inside the fragment (StudyFragment is named)...I don't know how.
I saw some tutorials about how to create it with card view but I don't understand how to use it in my app.
This is my menu.
Sorry for my bad English.
package co.intrasoft.quin_1;
import android.support.v4.app.Fragment;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
public class HomeActivity extends AppCompatActivity {
BottomNavigationView bottomNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
bottomNavigationView = findViewById(R.id.navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()){
case R.id.action_category:
selectedFragment = CategoryFragment.newInstance();
break;
case R.id.action_ranking:
selectedFragment = RankingFragment.newInstance();
break;
case R.id.action_study:
selectedFragment = StudyFragment.newInstance();
break;
case R.id.action_profile:
selectedFragment = ProfileFragment.newInstance();
break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout,selectedFragment);
transaction.commit();
return true;
}
});
setDefaultFragment();
}
private void setDefaultFragment() {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout,CategoryFragment.newInstance());
transaction.commit();
}
}
This is an example that I did.
Main Activity
package com.example.viviana.appfragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
private int[] images = new int []{
R.drawable.cal1, R.drawable.cal2, R.drawable.cal3, R.drawable.cal4,
R.drawable.cal5, R.drawable.cal6, R.drawable.cal7, R.drawable.cal8,
R.drawable.cal9, R.drawable.cal10, R.drawable.cal11, R.drawable.cal12,
R.drawable.cal13, R.drawable.cal14, R.drawable.cal15, R.drawable.cal16
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager pager = (ViewPager)findViewById(R.id.pager);
ImageAdapter adapter = new ImageAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
}
private class ImageAdapter extends FragmentStatePagerAdapter{
private ImageFragment imageFragment;
public ImageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
ImageFragment fragment = new ImageFragment(images[position]);
return fragment;
}
#Override
public int getCount() {
return images.length;
}
}
}
-xml of main activity
<android.support.v4.view.ViewPager 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:id="#+id/pager"
>
</android.support.v4.view.ViewPager>
Image Fragment class
package com.example.viviana.appfragment;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
/**
Created by viviana on 7/03/18.
*/
public class ImageFragment extends Fragment {
private int img;
public ImageFragment() {
}
public ImageFragment(int img){
this.img =img;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ImageView imageView = (ImageView)inflater.inflate(R.layout.image_view,container,false);
imageView.setImageResource(img);
return imageView;
}
}
I also declare 3 fragment page View As PageView. my app is not responding as soon as i run up there is no error. i reference to this youtube video
https://www.youtube.com/watch?v=NYjgX-GNRGo . I think this code is outdated. Can someone show me how can i correct this?
package com.example.name.scrollable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
public class MainActivity extends FragmentActivity
{
ViewPager mViewPager = null;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) mViewPager.findViewById(R.id.pager);
FragmentManager fragmentManager = getSupportFragmentManager();
mViewPager.setAdapter(new MyFragmentAdapter(fragmentManager));
}
}
class MyFragmentAdapter extends FragmentPagerAdapter
{
public MyFragmentAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int position)
{
Fragment fragment = null;
if(position == 0)
{
fragment = new Fragment_a();
}
if(position == 1)
{
fragment = new Fragment_b();
}
if (position == 2)
{
fragment = new Fragment_c();
}
return fragment;
}
#Override
public int getCount()
{
return 3;
}
}
i also make 3 fragment page show can i show with the help of ViewPager
add this one:
mViewPager = (ViewPager)findViewById(R.id.pager);
instead of this:
mViewPager = (ViewPager) mViewPager.findViewById(R.id.pager);
I'm dealing with and Android app using an activity and 3 fragments with a ViewPager and a FragmentPagerAdapter.
The app is working correctly but I still cannot retrieve the reference of a Fragment within the Activity.
The methods findFragmentById and findFragmentbyTag always return null.
Here's my code:
FragmentPagerAdapter:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class ViewPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public Fragment getItem(int position) {
switch (position) {
// Open FragmentTab1.java
case 0:
FragmentTab1 fragmenttab1 = new FragmentTab1();
return fragmenttab1;
// Open FragmentTab2.java
case 1:
FragmentTab2 fragmenttab2 = new FragmentTab2();
return fragmenttab2;
// Open FragmentTab3.java
case 2:
FragmentTab3 fragmenttab3 = new FragmentTab3();
return fragmenttab3;
}
return null;
}
}
Fragment implementation:
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
public class FragmentTab1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragmenttab1.xml
View view = inflater.inflate(R.layout.fragmenttab1, container, false);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
}
And finally my activity:
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
// Locate the viewpager in activity_main.xml
viewPager = (ViewPager) findViewById(R.id.pager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
// Set the ViewPagerAdapter into ViewPager
viewPager.setAdapter(viewPagerAdapter);
}
#Override
protected void onStart()
{
super.onStart();
InitializeVariables();
viewPager.setCurrentItem(1);
Fragment fragment = (Fragment)getSupportFragmentManager().findFragmentByTag(makeFragmentName(viewPager.getId(), 0));
}
I have error in listfragment "The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, ListFragment)" what should i do? cause i can't appear my fragment.
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
private FadingActionBarHelper mFadingActionBarHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFadingActionBarHelper = new FadingActionBarHelper(getActionBar(),
getResources().getDrawable(R.drawable.actionbar_bg));
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new ListViewFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
}
return super.onOptionsItemSelected(item);
}
public FadingActionBarHelper getFadingActionBarHelper() {
return mFadingActionBarHelper;
}
}
The problem is that the FragmentTransaction 's .add(int, Fragment) method accept arguments with the second one of type Fragment, however, I have ListFragment which is not acceptable by this method.
So, how to add ListFragment with FragmentTransaction ?
Here ListView Fragement class:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import com.viewpagerindicator.TabPageIndicator;
public class ListViewFragment extends FragmentActivity {
private static final String[] CONTENT = new String[] { "Recent", "Artists",
"Albums", "Songs", "Playlists", "Genres", "Hello", "Hii" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_listview);
FragmentPagerAdapter adapter = new GoogleMusicAdapter(
getSupportFragmentManager());
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
indicator.setViewPager(pager);
}
class GoogleMusicAdapter extends FragmentPagerAdapter {
public GoogleMusicAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
}
#Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length].toUpperCase();
}
#Override
public int getCount() {
return CONTENT.length;
}
}
Maybe I am wrong but without your ListViewFragment code I will assume that some of these are the problem you are finding.
(Not very likely) Your ListViewFragment is not a subclass of Fragment (or ListFragment).
Your ListViewFragment is a subclass of android.support.v4.app.Fragment (check your imports) instead of being a subclass of android.app.Fragment. If this is the case you have 2 possibilities.
2.1 Change your import to android.app.Fragment
2.2 Make your Activity a subclass of FragmentActivity and use getSupportFragmentManager() instead of getFragmentManager().
If neither of these are your problem, please post your ListViewFragment class.
Hope it helps :)
I'm trying to implement a Swipe View with Tabs app but my adapter is giving an error. The constructor function is not getting defined in my Main Activity. I have used the getSupportFragmentManager() function as the argument and all my files are .support.v4.app ones.
This is the code for my adapter. Please note I have not defined the fragments yet.
package com.example.tabpractice;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int arg0) {
switch (arg0) {
case 0:
return new Fragment1();
case 1:
return new Fragment2();
case 2:
return new Fragment3();
case 3:
return new Fragment4();
break;
}
return null;
}
#Override
public int getCount() {
return 4;
}
}
And this is the code for my Main Activity
package com.example.tabpractice;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public abstract class MainActivity extends FragmentActivity implements TabListener{
ViewPager vPager;
TabsPagerAdapter mAdapter;
ActionBar actionBar;
String[] tabs = {"Home","Income","Expense","Transactions"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = TabsPagerAdapter(getSupportFragmentManager());
vPager.setAdapter(null);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for(String tab_name : tabs){
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
}
You forget to instantiate the TabsPagerAdapter, instead of TabsPagerAdapter(...); try to use new TabsPagerAdapter(...);