我一直在尝试使用包将数据从“主要活动”传递到片段。我已经使用TabLayout和ViewPager以Tab格式在我的活动中添加片段。我没有收到任何错误,但是我得到了一个空对象作为回报,并且在我的第一个片段上显示了“ ERROR”,因为我为空对象添加了If条件。 这是我的代码
主要活动代码
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");
}
}
ViewPager代码
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);
}
}
片段1代码
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;
}
}
片段2代码
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;
}
}
片段3代码
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;
}
}
谁能建议我为什么从Main Activity中获取一个空对象来分割,以及如何将多个数据从Main Activity中传输到第二个和第三个片段。
答案 0 :(得分:0)
启动Fragment时,您可以通过包传递参数
/*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
答案 1 :(得分:0)
这是代码排序和范围问题。
您在中创建新的Fragment_1
viewPagerAdapter.AddFragment(new Fragment_1(),"Frag1");
并将其存储在ViewPagerAdapter
中,该附件没有捆绑。
然后,您随后在Fragment_1
的本地范围内创建oncreate
的新实例,并将Bundle附加到该实例上,然后将该实例扔掉。
更好的代码顺序。
//....
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
Bundle bundle = new Bundle();
String message = textView.getText().toString();
bundle.putString("key",message);
Fragment_1 fragment_1 = new Fragment_1();
fragment_1.setArguments(bundle);
// Add the existing instance of the Fragment_1 to the viewpager instead of creating another new instance.
viewPagerAdapter.AddFragment(fragment_1,"Frag1");
viewPagerAdapter.AddFragment(new Fragment_2(),"Frag2");
viewPagerAdapter.AddFragment(new Fragment_3(),"Frag3");
viewPager.setAdapter(viewPagerAdapter);
//....