繁体   English   中英

RecyclerViewAdapter到新的片段事务

[英]RecyclerViewAdapter to New Fragment Transaction

我在代码部分写过

主要活动,在主要活动中,有3个片段,片段1、2和3。

在片段1中,我通过使用RecyclerView适配器显示URL中的文本和一些图像。 文本和图像的总数是27个项目。 这意味着item1(text1, image1)item2(text2, image2) ... itemN(textN, imageN)

所以,我想设置一个OnClickListener ,对于工程

//this is pseudo-code
if (text1 is clicked) 
{
    goto NEW FRAGMENT1;
}
if (text2 is clicked)
{
    goto NEW FRAGMENT2;
}

因此,让我们谈谈代码,在我的RecyclerView我能够成功识别单击了哪个项目。 我唯一的问题是,我无法从RecyclerView转移到新的Fragment getSupportFragmentManagergetFragmentManager ,出现错误。

我的代码的示例代码:

viewHolder.item_shows.setOnClickListner(new View.OnCliclListner(){
@Override
public void onClick (View view)
{
String itemPosition = String.valueOf(viewHolder.getAdapterPosition());
// item position works fine. Its shows which number item clicked.
// ******** HELP **********}}};
return viewHolder;
--------------------
//In ******** HELP ********** this position I want to create
if(iemPosition.equals("0"))
{open New Fragment1}
if(itemPosition.equals("1"))
{open New Fragment2}

您需要在适配器的构造函数中传递活动的引用。 因为getSupportFragmentManager()是活动类的方法,而不是适配器类。

class MyAdapter(Activity mActivity,ArrayList<Model> list){

}

因此,请像下面那样使用它来替换您的活动容器中的片段。

mActivity.getSupportFragmentManager()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM