开发者社区> 问答> 正文

尝试将用户ID从活动发送到Viewholder片段

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

展开
收起
montos 2020-03-25 13:31:36 364 0
1 条回答
写回答
取消 提交回答
    1. 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
    
    
    2020-03-25 13:33:16
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多