当前位置: 首页 > 知识库问答 >
问题:

如何用TabLayout将数据从活动传递到片段

松灿
2023-03-14

我一直试图使用bundle将数据从主活动传递到片段。我已经使用TabLayout和ViewPager以Tab格式添加活动的片段。我没有得到任何错误,但我得到一个null对象作为返回,它显示“错误”在我的第一个片段,因为我已经添加了一个If条件为null对象。这是我的密码

package com.example.activitytofragmenttest;

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

import android.os.Bundle;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity {

    TextView textView;
    private ViewPager viewPager;
    private ViewPagerAdapter viewPagerAdapter;
    private TabLayout tabLayout;

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

        //Text View
        textView = findViewById(R.id.mainTextView);

        //TabLayout and ViewPager
        tabLayout = findViewById(R.id.tabLayout);
        viewPager = findViewById(R.id.viewPager);
        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        tabViewPager();

        Bundle bundle = new Bundle();
        String message = textView.getText().toString();
        bundle.putString("key",message);
        Fragment_1 fragment_1 = new Fragment_1();
        fragment_1.setArguments(bundle);

        viewPager.setAdapter(viewPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void tabViewPager() {
        viewPagerAdapter.AddFragment(new Fragment_1(),"Frag1");
        viewPagerAdapter.AddFragment(new Fragment_2(),"Frag2");
        viewPagerAdapter.AddFragment(new Fragment_3(),"Frag3");
    }
}
package com.example.activitytofragmenttest;

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.ArrayList;
import java.util.List;

public class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> listFragment = new ArrayList<>();
    private final List<String> listTitle = new ArrayList<>();


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

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return listFragment.get(position);
    }

    @Override
    public int getCount() {
        return listTitle.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return listTitle.get(position);
    }


    public void AddFragment (Fragment fragment, String title){
        listFragment.add(fragment);
        listTitle.add(title);
    }
}
package com.example.activitytofragmenttest;

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

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

public class Fragment_1 extends Fragment{

    TextView fragment_1TextView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_1,container,false);

        fragment_1TextView = v.findViewById(R.id.fragment1TextView);
        Bundle message = getArguments();
        if (message != null){
            fragment_1TextView.setText(message.getString("key"));
        }else {
            fragment_1TextView.setText("Error");
        }
        return v;
    }
}
package com.example.activitytofragmenttest;

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

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

public class Fragment_2 extends Fragment {
    TextView fragment_2TextView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_2, container, false);

        fragment_2TextView = v.findViewById(R.id.fragment2TextView);
//        Bundle bundle = getArguments();
//        if (bundle != null) {
//            fragment_2TextView.setText(bundle.getString("key"));
//        }
        return v;
    }
}
package com.example.activitytofragmenttest;

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

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

public class Fragment_3 extends Fragment {
    TextView fragment_3TextView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_3, container, false);

        fragment_3TextView = v.findViewById(R.id.fragment3TextView);
//        Bundle bundle = getArguments();
//        if (bundle != null) {
//            fragment_2TextView.setText(bundle.getString("key"));
//        }
        return v;
    }
}

谁能建议我为什么从主活动到片段得到一个空对象,以及如何将多个数据从主活动转移到其他片段,也就是第二和第三个片段。

共有1个答案

归德厚
2023-03-14

在启动片段时,可以通过包传递参数

/*ACTIVITY CODE*/
Bundle args = new Bundle();
args.putStringArrayList("argument_name", myListInString);
MyFragment fragment = new MyFragment();
fragment.setArguments(args);
fragment.show(fragmentTransaction, "fragment_tag")

并在新代码段中使用它们,如下所示

/* FRAGMENT CODE */
ArrayList<String> myList = getArguments.getStringArrayList("argument_name");
// ... FILL TABLE
 类似资料: