android 将数据从活动传递到页面适配器

krcsximq  于 2023-01-19  发布在  Android
关注(0)|答案(1)|浏览(130)

我需要将字符串数据传递到选项卡布局。我已经将数据从适配器传递到选项卡布局中的片段。但我的问题是我无法将该数据从活动传递到适配器java类。我需要将数据传递到多个片段。我该如何做到这一点?

主要活动代码

package com.example.tablay;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;

import com.google.android.material.tabs.TabLayout;

import java.util.List;
import java.util.Stack;

public class MainActivity extends AppCompatActivity {

    private ViewPager pager;
    private TabLayout tabs;

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

        pager=(ViewPager) findViewById(R.id.pager);
        tabs=(TabLayout) findViewById(R.id.tabs);

        pager.setAdapter(new TabFragmentPagerAdapter(getSupportFragmentManager()));
        tabs.setupWithViewPager(pager);
        tabs.setTabGravity(TabLayout.GRAVITY_FILL);

        
    }

}

页面适配器代码:

package com.example.tablay;

import android.os.Bundle;
import android.provider.ContactsContract;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.Date;
import java.util.List;

public class TabFragmentPagerAdapter extends FragmentPagerAdapter {
    String[] title = new String[]{
            "Tab 1", "Tab 2", "Tab 3"
    };

    public TabFragmentPagerAdapter(@NonNull FragmentManager fm) {
        super(fm);
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        Fragment fragment=null;
        switch (position){
            case 0:
                tab1Fragment tab1Fragment=new tab1Fragment();
                Bundle bundle = new Bundle();
                bundle.putString("edttext", "data From Activity");
                tab1Fragment.setArguments(bundle);
                return tab1Fragment;
            case 1:
                tab2Fragment tab2Fragment=new tab2Fragment();
                bundle = new Bundle();
                bundle.putString("edttext1", "data From Activity1");
                tab2Fragment.setArguments(bundle);
                return tab2Fragment;
            case 2:
                tab3Fragment tab3Fragment=new tab3Fragment();
                return tab3Fragment;
            default:
                fragment=null;
                break;
        }
        return  fragment;
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return  title[position];
    }

    @Override
    public int getCount() {
        return  title.length;
    }
}

片段代码:

package com.example.tablay;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class tab1Fragment extends Fragment {

    public tab1Fragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_tab1, container, false);
        TextView tes=(TextView) view.findViewById(R.id.testtab1);
        String strtext = getArguments().getString("edttext");

        tes.setText(strtext);
        return  view;
    }
}

我需要将字符串数据传递到选项卡布局。我已经将数据从适配器传递到选项卡布局中的片段。但我的问题是我无法将该数据从活动传递到适配器java类。我需要将数据传递到多个片段。我该如何做到这一点?

tkclm6bt

tkclm6bt1#

您可以通过构造函数将数据从Activity传递到适配器,如下所示:

public class MainActivity extends AppCompatActivity {

private ViewPager pager;
private TabLayout tabs;

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

    pager=(ViewPager) findViewById(R.id.pager);
    tabs=(TabLayout) findViewById(R.id.tabs);

    pager.setAdapter(new TabFragmentPagerAdapter(getSupportFragmentManager(),ActivityStringData));
    tabs.setupWithViewPager(pager);
    tabs.setTabGravity(TabLayout.GRAVITY_FILL);

    
}

public class TabFragmentPagerAdapter extends FragmentPagerAdapter {
String[] title = new String[]{
        "Tab 1", "Tab 2", "Tab 3"
};
String data=null;

public TabFragmentPagerAdapter(@NonNull FragmentManager fm,String activityData) {
    super(fm);
     this.data=activityData
}

@NonNull
@Override
public Fragment getItem(int position) {
    Fragment fragment=null;
    switch (position){
        case 0:
            tab1Fragment tab1Fragment=new tab1Fragment();
            Bundle bundle = new Bundle();
            bundle.putString("edttext", "data From Activity");
            tab1Fragment.setArguments(bundle);
            return tab1Fragment;
        case 1:
            tab2Fragment tab2Fragment=new tab2Fragment();
            bundle = new Bundle();
            bundle.putString("edttext1", "data From Activity1");
            tab2Fragment.setArguments(bundle);
            return tab2Fragment;
        case 2:
            tab3Fragment tab3Fragment=new tab3Fragment();
            return tab3Fragment;
        default:
            fragment=null;
            break;
    }
    return  fragment;
}

@Nullable
@Override
public CharSequence getPageTitle(int position) {
    return  title[position];
}

@Override
public int getCount() {
    return  title.length;
}

}

相关问题