简体   繁体   English

通过单击recyclerview项打开片段

[英]open a Fragment by clicking on a recyclerview item

I'm trying the last view days to open a Fragment by clicking on a recyclerview item. 我正在尝试在最后一个查看日中通过单击recyclerview项来打开片段。 I'm still getting the same error: The method getSupportFragmentManager() is undefined for the type SightsAdapter.ListItemViewHolder 我仍然遇到相同的错误:对于类型SightsAdapter.ListItemViewHolder,未定义方法getSupportFragmentManager()

I read lots of threads but they couldn't help me and I'm absolutely new to android. 我读了很多线程,但是它们不能帮助我,我绝对不是android的新手。 Does anyone have any idea what i'm doing wrong? 有人知道我在做什么错吗?

Here is my code: 这是我的代码:

package com.example.stadtfuehrer;



import java.util.ArrayList;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.view.LayoutInflater;
import android.support.v4.app.*;



public class SightsAdapter  extends RecyclerView.Adapter<SightsAdapter.ListItemViewHolder> {

private ArrayList<Sights> listData;

public SightsAdapter(ArrayList<Sights> list){
    this.listData = list;
}

@Override
public int getItemCount(){
    return listData.size();
}

@Override
public void onBindViewHolder(ListItemViewHolder holder, int position){
    Sights sight = listData.get(position);
    holder.textViewSightsName.setText(sight.getName());

}

@Override
public ListItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem_recyclerview, parent, false);
    //itemView.setPadding(10, 5, 10, 5);
    return new ListItemViewHolder(itemView);
}



public static class ListItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public TextView textViewSightsName;

    public ListItemViewHolder(View itemView){
        super(itemView);
        textViewSightsName = (TextView) itemView.findViewById(R.id.name_listitem);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
        getSupportFragmentManager().beginTransaction().replace(R.id.container,new FragmentSightDetailed() ).commit();
        Toast.makeText(itemView.getContext(), "Item clicked. "+textViewSightsName.getText(), Toast.LENGTH_SHORT).show();

    }

}   
}

MainActivity main; MainActivity主;

public ViewAdapter(Context context, List<Information> data,MainActivity ma)
{
    inflater=LayoutInflater.from(context);
    this.data=data;
    this.context=context;
    main=ma;
}
main.getSupportFragmentManager().beginTransaction().replace(R.id.container,new Fragment1()).commit();

This works fine for me,just pass MainActivity.this to the parameterised constructor of Adapter.It might be help for you. 这对我来说很好,只需将MainActivity.this传递给Adapter的参数化构造函数即可。这可能对您有所帮助。

FragmentOne -->RecyclerViewAdapter onClcik() -->FragmentTwo FragmentOne-> RecyclerViewAdapter onClcik()-> FragmentTwo

After a lot of searching i have solved this issue 经过大量搜索,我已经解决了这个问题

FragmentOne fragmentOne;

public RecyclerViewAdapter(Context mContext, List<GetSetModel> list, FragmentOne fragmentOne) {
this.mContext = mContext;
this.list = list;
this.fragmentOne = fragmentOne;
}

**Now in OnClick() method open New Fragment like this below**

Fragment fragment = new FragmentTwo();
Bundle args = new Bundle();
args.putString("data", "This data has sent to FragmentTwo");
fragment.setArguments(args);
FragmentTransaction transaction =     frgmentOne.getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.addToBackStack(null);
transaction.commit();

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

相关问题 通过单击recyclerview项打开片段,然后移至该片段 - open a Fragment by clicking on a recyclerview item and move to this fragment 如何在单击RecyclerView项时打开一个片段,该片段在另一个片段中实现了,可以再次使用ViewPager进行滑动? - How to open a fragment on clicking to RecyclerView item which is implemented in another fragment which is again can be swiped by using ViewPager? 单击RecyclerView中的项目后分段交易 - Fragment transaction after clicking item in RecyclerView 如何通过单击RecyclerView项目打开AlertDialog? - How to open AlertDialog by clicking on RecyclerView item? 从ApI打开Fragment OnClick on Recyclerview项目 - Open Fragment OnClick on Recyclerview item from ApI 我无法在Recyclerview项目中打开片段 - I can't open fragment in Recyclerview item 片段不会在 RecyclerView 项目单击时打开 - Fragment won't open on RecyclerView item click Recyclerview item onClick 从 Main Activity Fragment 打开一个片段 - Recyclerview item onClick to open a fragment from Main Activity Fragment 如何通过单击来自recyclerview的项目来打开新活动 - How to open new activity from clicking an item from recyclerview 单击RecyclerView中的特定项目后如何打开URL? - How to open URL after clicking at a particular item in RecyclerView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM