Android:如何在使用FragmentPagerAdapter时替换或覆盖片段

时间:2015-12-31 21:42:55

标签: android android-fragments onclick fragmentpageradapter fragmenttransaction

我有一个带有三个标签的应用,我使用FragmentPagerAdapter进行控制。现在,当我点击一个片段的TextView时,我想在这些选项卡之间跳转(带有TextView的片段应该替换为目标片段)。当我点击Button时,另一个片段应覆盖在此片段上方Button

我知道那些是两个不同的东西,但我也知道我必须使用FragmentTransaction但我无法弄清楚如何做到这一点。

你能帮我吗?

这是我的MainActivy.java:

package letscho.workout;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity {

    Toolbar toolbar;
    ViewPager viewPager;
    TabLayout tabLayout;
    FragmentManager fragmanager;
    FragmentTransaction fragtrans;
    Fragment1 fragment1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        viewPager = (ViewPager) findViewById(R.id.viewpager);

        ViewPagerAdapter1 viewPagerAdapter1 = new ViewPagerAdapter1(getSupportFragmentManager());
        viewPager.setAdapter(viewPagerAdapter1);


        tabLayout = (TabLayout) findViewById(R.id.tablayout);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

            @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 static void changefragmentOnBtnClick(){

        fragmanager = getFragmentManager();
        fragtrans = fragmanager.beginTransaction();
        fragtrans.replace(R.id.viewpager, getActivity());
        fragtrans.commit();
    }
}

ViewPager:

package letscho.workout;

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




public class ViewPagerAdapter1 extends FragmentPagerAdapter {

    String [] tabtitlearray = {"Übungen", "Push", "Pull"};

    public ViewPagerAdapter1 (FragmentManager manager) {

        super(manager);
    }


    @Override
    public Fragment getItem(int position) {

        switch (position) {

            case 0: return new Fragment1();
            case 1: return new Fragment2();
            case 2: return new Fragment3();

        }

        return null;

    }

    @Override
    public int getCount() {
        return 3;
    }


    @Override
    public CharSequence getPageTitle(int position) {

        return tabtitlearray[position];
    }
}

片段所有应该开始的地方:

package letscho.workout;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Fragment2 extends Fragment {
    View contentView2;
    TextView textView;
    Button btn1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        contentView2 = inflater.inflate(R.layout.layout2_push, null);
        return contentView2;

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        textView = (TextView)contentView2.findViewById(R.id.push_text_eins);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity.changefragmentOnBtnClick();
            }
        });
        
        btn1 = (Button) contentView2.findViewById(R.id.btn_timer1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                
            }
        });
    }

    @Override
    public void onStop() {
        super.onStop();
    }


}

0 个答案:

没有答案