java - 在滑动选项卡布局中停留在同一选项卡中时切换到不同的 fragment

标签 java android xml android-fragments tabs

我有一个滑动选项卡布局,其中的回收器 View 包含三个选项卡。第一个选项卡包含卡片 View 。实现以下目标的最佳方法是什么:当您单击第一张卡卡 View (一个 fragment )内的按钮时,它会被另一个卡 View (另一个 fragment )替换,同时保留在同一选项卡中。

我知道我必须创建一个新 fragment 并使用事务。我只是不知道在哪里。我是否将其放入适配器中?如果可能的话,我想在第一个卡片 View 的 XML 布局中使用 onClick 作为按钮。

Sliding Tab How-to

Sliding Tab Layout

Sliding Tab Strip

主要 Activity :

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends AppCompatActivity  {

    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);


        // Get the ViewPager and set it's PagerAdapter so that it can display items
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(new MainFragmentPageAdapterForTabs(getSupportFragmentManager(),
                MainActivity.this));

        // Give the SlidingTabLayout the ViewPager
        SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
        // Center the tabs in the layout
        slidingTabLayout.setDistributeEvenly(true);
        slidingTabLayout.setViewPager(viewPager);

    }


    public void bodyButtonAction(View view){
        Intent intentBody = new Intent(MainActivity.this, MainActivity.class);
        startActivity(intentBody);
    }

标签 fragment :

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

// In this case, the fragment displays simple text based on the page
public class MainPageFragmentForTabs extends Fragment {
    public static final String ARG_PAGE = "ARG_PAGE";

    private int mPage;

    public static MainPageFragmentForTabs newInstance(int page) {

        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        MainPageFragmentForTabs fragment = new MainPageFragmentForTabs();
        //body fragment
        Fragment bodyFragment = new MainPageFragmentForTabs();

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(ARG_PAGE);


    }


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


        View view = null;


        if(mPage==1){
            View view1 = inflater.inflate(R.layout.main_fragment_page, container, false);
            FragmentActivity a = getActivity();

            //recycler
            RecyclerView recyclerView = (RecyclerView) view1.findViewById(R.id.my_recycler_view);
            recyclerView.setHasFixedSize(true);

            //layout manager
            LinearLayoutManager manager = new LinearLayoutManager(a);
            manager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(manager);


            MainAdapterCV1 ca = new MainAdapterCV1();
            recyclerView.setAdapter(ca);

            view=view1;

        }
        if(mPage==2){
            View view2 = inflater.inflate(R.layout.main_fragment_page, container, false);
            //stand-in code
            TextView textView = (TextView) view2;
            textView.setText("Fragment #" + mPage);
            //stand-in code
            view=view2;

        }
        if(mPage==3){
            View view3 = inflater.inflate(R.layout.main_fragment_page, container, false);
            //stand-in code
            TextView textView = (TextView) view3;
            textView.setText("Fragment #" + mPage);
            //stand-in code
            view=view3;

        }

        return view;
    }
}

第一个卡片 View 的适配器:

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainAdapterCV1 extends RecyclerView.Adapter<MainAdapterCV1.CardViewHolder> {


    //
    @Override
    public int getItemCount() {
        return 1;
    }

    @Override
    public void onBindViewHolder(CardViewHolder cardViewHolder, int i) {

    }
    //
    @Override
    public CardViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.
                from(viewGroup.getContext()).
                inflate(R.layout.main_card_view1, viewGroup, false);

        return new CardViewHolder(itemView);
    }

    public static class CardViewHolder extends RecyclerView.ViewHolder {


        public CardViewHolder(View v) {
            super(v);

        }
    }
}

最佳答案

从技术上讲,您可以在任何地方添加或替换 fragment 。然而,我认为在适配器中执行此操作比在 fragment 中执行更符合逻辑。原因之一是适配器通常具有单击事件监听器代码,而我在发布的代码中没有看到它,这很好。替换 fragment 的示例代码(使用您的代码):

MainPageFragmentForTabs myFragment = MainPageFragmentForTabs.newInstance(page);

FragmentTransaction transaction = thisActivity.getFragmentManager().beginTransaction();
// Replace the Fragment (set in MainActivity) with this fragment.
transaction.replace(R.id.sample_content_fragment, myFragment, "MainPage");

transaction.addToBackStack(null);       // support the Back key
transaction.commit();

关于java - 在滑动选项卡布局中停留在同一选项卡中时切换到不同的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30429004/

相关文章:

java - 使用对象的 float 属性的 compareTo() 方法。它是如何使用的,为什么?

java - 减慢相机对象的 setPreviewCallback 方法

java - 显示图像计数器

Android 在Manifest 中添加<uses-feature>

java - 从 Java 为 XML 变量赋值

java - 在java代码中使用xpath时出错

java - JAX-RPC 1.1 中的公共(public)默认构造函数错误

android - 计算距离相机远近的移动物体的速度

Android 每日更新地理围栏

xml - 删除 XML 中的所有 HTML