Android swiping, za pomocą ViewPager bez kart

Próbuję stworzyć aplikację, która ma typ nawigacji przesuwania. Tak daleko zaszedłem:

Aktywność fragmentu:

package com.app.BoomBase;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment_control extends Fragment {

String tag = this.getClass().getSimpleName();
@Override
public void onCreate(Bundle savedInstanceState) {
    Log.i(tag, "onCreate");
    super.onCreate(savedInstanceState);
    /** Getting the arguments to the Bundle object */
    Bundle data = getArguments();

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Log.i(tag, "onCreateView");

    View view = inflater.inflate(R.layout.fragment_controle,container, false ); 

    return view;
  }

  }

FragmentPageAdapter:

package com.app.BoomBase;

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

  public class Fragment_Pager extends FragmentPagerAdapter {

final int PAGE_COUNT = 3;
public Fragment_Pager(FragmentManager fm) {
    super(fm);
    // TODO Auto-generated constructor stub
}

@Override
public Fragment getItem(int arg0) {

    Fragment_control myFragment = new Fragment_control();
    Bundle data = new Bundle();
    data.putInt("current_page", arg0+1);
    myFragment.setArguments(data);
    return myFragment;

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return PAGE_COUNT;
}

}

Główna aktywność:

package com.app.BoomBase;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends FragmentActivity {

String tag = this.getClass().getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(tag, "onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /** Getting a reference to the ViewPager defined the layout file */
    ViewPager pager = (ViewPager) findViewById(R.id.pager);

    /** Getting fragment manager */
    FragmentManager fm = getSupportFragmentManager();

    /** Instantiating FragmentPagerAdapter */
    Fragment_Pager pagerAdapter = new Fragment_Pager(fm);

    /** Setting the pagerAdapter to the pager object */
    pager.setAdapter(pagerAdapter);

}
}

Ale mój problem polega na tym, że nie mogę wymyślić, jak dodać fragmenty do kodu. Chcę przesunąć palcem do następnej aktywności za pomocą przycisków i rzeczy na nich. Jak mam to zrobić ?

Author: Lasse, 2013-04-04

2 answers

PageAdapter używa getItem() do przełączania się pomiędzy Fragments, Tutaj można zadeklarować, która strona robi co. "Pozycja "0 jest pierwszą stroną, 1 jest drugą i tak dalej. Możesz po prostu zwrócić nową instancję swojej innej Fragments lub przekazać argumenty, jeśli chcesz.

Na przykład:

@Override
public Fragment getItem(int position) {
    switch (position) {
    case 0:
        // Your current main fragment showing how to send arguments to fragment
        Fragment_control myFragment = new Fragment_control();
        Bundle data = new Bundle();
        data.putInt("current_page", position+1);
        myFragment.setArguments(data);
        return myFragment;
    case 1:
        // Calling a Fragment without sending arguments
        return new MySecondFragment();
    case 2:
        return new MyThirdFragment();
    default:
        return null;
    }
}

Następnie utworzyłbyś Fragment klasę dla każdego z tych, które chcesz włączyć. W moim przykładzie mielibyście klasę dla MySecondFragment i MyThirdFragment

public class MySecondFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.second_fragment, null); 
        return view;
    }

}

I

public class MyThirdFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.third_fragment, null); 
        return view;
    }

}

Każdy fragment może po prostu napompować standardowy plik układu XML, aby uzyskać dostęp do dzieci w układzie, musisz pamiętać, aby użyć poniższego, użyję MySecondFragment jako przykład.

Załóżmy, że masz dwa Buttons z identyfikatorami R.id.button1 i R.id.button2 w pliku układu zatytułowanym 'second_fragment':

public class MySecondFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.second_fragment, null);

        // Make sure to add the parent inflater before and layout child call
        Button btn_one = (Button)view.findViewById(R.id.button1);
        Button btn_two = (Button)view.findViewById(R.id.button2);

        return view;
    }

}

Edit

Aby zacząć od strony innej niż 0 lub pierwszej pozycji, wystarczy użyć setCurrentItem() w Twoje MainAvticity, tak:

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(tag, "onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ViewPager pager = (ViewPager) findViewById(R.id.pager);
    FragmentManager fm = getSupportFragmentManager();
    Fragment_Pager pagerAdapter = new Fragment_Pager(fm);
    // Here you would declare which page to visit on creation
    pager.setAdapter(pagerAdapter);
    pager.setCurrentItem(1);

}
 15
Author: jnthnjns,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-04 14:25:25

Dodałem to w klasie fragment ..

Problem, który mam to cannot resolve cannot resolve get support fragment manager ().

@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(tag, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ViewPager pager = (ViewPager) findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
Fragment_Pager pagerAdapter = new Fragment_Pager(fm);
// Here you would declare which page to visit on creation
pager.setAdapter(pagerAdapter);
pager.setCurrentItem(1);

}
 0
Author: user271805,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-18 06:33:56