[英]RecyclerViewAdapter to New Fragment Transaction
I have written in Code Section 我在代码部分写过
Main Activity, In Main Activity, there are 3 Fragments, fragments 1, 2 & 3. 主要活动,在主要活动中,有3个片段,片段1、2和3。
In the fragments 1, I'm showing a Text and some Images from an URL by Using a RecyclerView
adapter. 在片段1中,我通过使用RecyclerView
适配器显示URL中的文本和一些图像。 The total number of Texts and Images is 27 items. 文本和图像的总数是27个项目。 It means item1(text1, image1)
, item2(text2, image2)
... itemN(textN, imageN)
这意味着item1(text1, image1)
, item2(text2, image2)
... itemN(textN, imageN)
So I want to set an OnClickListener
that works for 所以,我想设置一个OnClickListener
,对于工程
//this is pseudo-code
if (text1 is clicked)
{
goto NEW FRAGMENT1;
}
if (text2 is clicked)
{
goto NEW FRAGMENT2;
}
So let's talk about the code, in my RecyclerView
I'm Successfully able to identify which item is clicked. 因此,让我们谈谈代码,在我的RecyclerView
我能够成功识别单击了哪个项目。 My only problem is, I was unable to transfer from RecyclerView
to new Fragment
. 我唯一的问题是,我无法从RecyclerView
转移到新的Fragment
。 In the getSupportFragmentManager
or getFragmentManager
I'm getting an error. 在getSupportFragmentManager
或getFragmentManager
,出现错误。
A Sample Code of my code: 我的代码的示例代码:
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}
You need to pass reference of your activity in constructor of adapter. 您需要在适配器的构造函数中传递活动的引用。 Because getSupportFragmentManager()
is method of activity class not your adapter class. 因为getSupportFragmentManager()
是活动类的方法,而不是适配器类。
class MyAdapter(Activity mActivity,ArrayList<Model> list){
}
so use it like below to replace fragment in your activity container. 因此,请像下面那样使用它来替换您的活动容器中的片段。
mActivity.getSupportFragmentManager()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.