将数据从一个设计选项卡传递到另一选项卡

斯旺德·韦迪亚

我正在使用材料设计选项卡,如下所示:

以下是我的MainActivity.class:

 public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    private TabLayout tabLayout;
    private ViewPager viewPager;

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

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(this);
        tabLayout.setupWithViewPager(viewPager);

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        System.out.println("onPageSelected Called");
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}

我的ViewPagerAdapter如下:

    public class ViewPagerAdapter extends FragmentPagerAdapter {

    private static int count = 2;

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                 return new FragmentOne();
            case 1:
                return new FragmentTwo();
        }
        return null;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position)
        {
            case 0 :
                return "Tab One";
            case 1 :
                return "Tab Two";
        }
        return null;
    }
}

这是我的片段:

这是我的第一个片段名称是FragmentOne:

    public class FragmentOne extends Fragment {

    private EditText editText;
    private Button btnSendData;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        System.out.println("IN FRAGMENT ONE");
        View view = inflater.inflate(R.layout.fragment_one,container,false);

        editText = (EditText) view.findViewById(R.id.et_name);
        btnSendData = (Button) view.findViewById(R.id.btn_send);

        btnSendData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                FragmentTwo fragment = new FragmentTwo();
                Bundle bundle = new Bundle();
                bundle.putString("username",editText.getText().toString());
                fragment.setArguments(bundle);
                getFragmentManager.beginTransaction.replace(R.id.frag_second,fragment).commit();

            }
        });

        return view;
    }
}

这是另一个名为FragmentTwo的片段:

 public class FragmentTwo extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        System.out.println("IN FRAGMENT TWO");
        View view = inflater.inflate(R.layout.fragment_two,container,false);
        Bundle bundle = getArguments();
        if(bundle!= null)
        {
            String value = getArguments().getString("username");
        }
        return  view;
    }

    @Override
    public void onResume() {
        super.onResume();
        System.out.println("onResume gets called");
    }
}

在第二个片段中,我正在获取数据,但是它与先前的视图一起添加了另一个视图。看到图片:

在此处输入图片说明

所以我想在两种情况下将数据从FragmentOne传递到FragmentTwo:
1.我想在单击按钮时传递数据,并且
2.当我滑动到FragmentTwo时,应该传递数据

Also when I try to swipe to FragmentTwo nothing gets called in FragmentTwo ? why is it so ? Also when I click to second tab nothing gets called . Please help me how should i pass data to FragmentTwo on button click ?

following are layout files: here is fragment_one

<LinearLayout 
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:gravity="center"
       android:orientation="vertical">
           <EditText
               android:layout_marginLeft="16dp"
               android:layout_marginRight="16dp"
               android:id="@+id/et_name"
               android:hint="Username"

               android:layout_width="match_parent"
               android:layout_height="wrap_content"/>

           <Button
                android:padding="16dp"
                android:text="Send Data"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:id="@+id/btn_send"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"/>

and second fragment , fragment_second.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/second_frag"
android:gravity="center">
<TextView
    android:textSize="24sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Fragment Two"
    android:id="@+id/textView2" />

Onkar Nene

Each fragment is associated with the parent activity. so you can't directly communicate from one fragment to another fragment. You will need to go through Parent Activity using interface.

Check this docs : https://developer.android.com/training/basics/fragments/communicating.html

On button click pass the value to methods in your custom interface and access those methods in second fragment.

when I try to swipe to FragmentTwo nothing gets called in FragmentTwo

为此,您需要实现片段生命周期-https: //developer.android.com/reference/android/app/Fragment.html

更新

在您的代码中进行了一些修改后,只需查看以下内容即可。代码 -

Manifex.xml

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:theme="@style/Theme.AppCompat.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

MainActivity.java

package com.app.onkar.tabdemo;

import android.support.v4.app.Fragment;
import android.support.design.widget.TabLayout;

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

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;

    @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);
        tabLayout = (TabLayout) findViewById(R.id.tabs);


        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

        adapter.addFragment(new FragmentOne(), "ONE");
        adapter.addFragment(new FragmentTwo(), "TWO");
        viewPager.setAdapter(adapter);

        viewPager.addOnPageChangeListener(this);
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        System.out.println("onPageSelected Called");
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public static class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();
        private static int count = 2;

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            switch (position) {
                case 0:
                    return new FragmentOne();
                case 1:
                    return new FragmentTwo();
            }
            return null;
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Tab One";
                case 1:
                    return "Tab Two";
            }
            return null;
        }
    }
}

FragmentOne.java

package com.app.onkar.tabdemo;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
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;

public class FragmentOne extends Fragment {
    private EditText editText;
    private Button btnSendData;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        System.out.println("IN FRAGMENT ONE");
        View view = inflater.inflate(R.layout.fragment_one,container,false);

        editText = (EditText) view.findViewById(R.id.et_name);
        btnSendData = (Button) view.findViewById(R.id.btn_send);

        btnSendData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                FragmentTwo fragment = new FragmentTwo();
                Bundle bundle = new Bundle();
                bundle.putString("username",editText.getText().toString());
                fragment.setArguments(bundle);
                getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.second_frag,fragment).commit();
            }
        });

        return view;
    }
}

FragmentTwo.java

package com.app.onkar.tabdemo;


import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class FragmentTwo extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        System.out.println("IN FRAGMENT TWO");
        View view = inflater.inflate(R.layout.fragment_two,container,false);
        TextView txt2 = (TextView) view.findViewById(R.id.textView2);
        Bundle bundle = getArguments();
        if(bundle!= null)
        {
            String value = getArguments().getString("username");
            txt2.setText(value);
        }
        return  view;
    }

    @Override
    public void onResume() {
        super.onResume();
        System.out.println("onResume gets called");
    }

}

布局文件没有变化。只需尝试上面的代码-它完全可以按照您想要的方式工作。希望对您有所帮助!

屏幕 在此处输入图片说明 在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将行从一个选项卡复制并删除到另一个选项卡

如何将参数从一个选项卡传递到另一个选项卡?

在#vba 中使用转置将数据从一个选项卡传输(复制和粘贴)到另一个选项卡

快速将数据从一个选项卡控制器传递到另一个

通过单击按钮从一个选项卡导航到另一个选项卡时设置活动选项卡

在滑动选项卡布局中将数据从一个片段传递到多个片段

将变量从MainActivity传递到另一个Activity到选项卡Fragment

根据多个条件将行从一个选项卡复制到另一个选项卡

仅将非空白行从一个选项卡复制到另一个选项卡

需要 Google Script 将值从一个选项卡复制到另一个选项卡

如何将参数传递到另一个选项卡导航屏幕

将熊猫数据框显示到另一个选项卡中

Xterm:如何将输入从一个终端选项卡定向到另一个?

将控件选项卡从一个 UI 插入到另一个 UI

如何根据 URL 将活动类从默认选项卡删除到另一个选项卡?

将数据表的结果从一个选项卡显示到另一个选项卡,并将其分配给Shiny中的数据集列表

如何使用多个选项卡将ID从一个剃刀页面传递到另一个剃刀页面

如何将数据从视图控制器传递到选项卡栏控制器的第一个选项卡

Android操作栏选项卡+滑动视图分页器从一个选项卡动态导航到另一个

从选项卡从输入类型提交按钮传递到另一个

将选定的 ListFragment 的行项目详细信息(当前选项卡)传递给另一个 ListFragment(下一个选项卡)

通过 importrange (Google Sheets) 导入数据时将数据行存档到另一个选项卡

如果用户从一个选项卡移动到另一个选项卡,如何在Angular Material中重置/刷新选项卡主体数据?

将新创建的pdf显示到另一个选项卡

Excel VLOOKUP-将需要显示从一个选项卡到另一个Excel选项卡的一列

在 Microsoft Teams 中从选项卡内重定向到另一个选项卡失败

用于更新和/或将数据附加到另一个选项卡的脚本

如何从选项卡内部更改为另一个选项卡?

使用引导程序从选项卡调用另一个选项卡?