0
我创建了一个android应用程序,其中添加了一个名为profileview.java的活动,用户可以在该活动上查看该活动或其他用户的个人资料数据。Profileview活动包含两个片段和喜欢的片段,我必须在这些片段上显示用户发布的帖子以及用户喜欢的帖子,但不知道如何将用户ID从Profileview活动发送到其他两个片段。
profileviewactivity.class
public class profileviewActivity extends AppCompatActivity {
public String userid;
private ViewPager viewPager;
private TabLayout tabLayout;
private settingtabAdopter Tabadapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profileview);
viewPager = (ViewPager) findViewById(R.id.viewpager);
Tabadapter = new settingtabAdopter(getSupportFragmentManager());
viewPager.setAdapter(Tabadapter);
tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
userid = getIntent().getExtras().get("userid").toString();
}
}
settingadapter.java
public class settingtabAdopter extends FragmentPagerAdapter {
public settingtabAdopter(@NonNull FragmentManager fm) {
super(fm);
}
@NonNull
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
posttimeline postfragment = new posttimeline();
return postfragment;
case 1:
like chatfragment = new like();
return chatfragment;
default:
return null;
}
}
@Override
public int getCount() {
return 2;
}
@Nullable
@Override
public CharSequence getPageTitle(int i) {
switch (i){
case 0:
return "Posts";
case 1:
return "Likes";
default:
return null;
}
}
}
posttimeline.java
public class posttimeline extends Fragment {
View posttimelineview;
public posttimeline() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
posttimelineview = inflater.inflate(R.layout.fragment_posttimeline, container, false);
return posttimelineview;
}
}
like.java
public class like extends Fragment {
View Likeview;
public like() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Likeview = inflater.inflate(R.layout.fragment_like, container, false);
return Likeview;
}
}
我想从profileaviewactivity发送userid到posttimeline和类似的片段。
问题来源:Stack Overflow
userid作为settingtabAdopter构造函数的参数传递
public settingtabAdopter(@NonNull FragmentManager fm, String userid)
profileviewActivity-> onCreate
Tabadapter = new settingtabAdopter(getSupportFragmentManager(), userid);
2. 在内部设置私有变量 settingtabAdopter
private String userid;
在构造函数中初始化它
this.userid = userid
3. 在每个片段中创建私有变量并添加setter(后时间表等)片段
private String mUserid; void setMUserId(String mUserid){ this.mUserid = mUserid; }
4. 初始化每个片段后,在settingtabAdopter内部getItem函数上使用设置私有变量userid
public Fragment getItem(int position) { switch (position){ case 0: posttimeline postfragment = new posttimeline(); postfragment.setMUserId(userid) return postfragment; case 1: like chatfragment = new like(); chatfragment.setMUserId(userid) return chatfragment; default: return null; } }
回答来源:Stack Overflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。