单击recyclerview项目打开片段

时间:2015-01-18 12:17:00

标签: android android-fragments android-recyclerview

我正在尝试通过点击recyclerview项目打开片段的最后一个视图日。我仍然得到同样的错误: 对于SightsAdapter.ListItemViewHolder类型,未定义方法getSupportFragmentManager()

我读了很多帖子,但他们无法帮助我,我对android很新。 有谁知道我做错了什么?

这是我的代码:

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();

    }

}   
}

2 个答案:

答案 0 :(得分:7)

MainActivity main;

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();

这对我来说很好,只需将MainActivity.this传递给Adapter.It的参数化构造函数即可。对你来说可能会有所帮助。

答案 1 :(得分:3)

FragmentOne - &gt; RecyclerViewAdapter onClcik() - &gt; FragmentTwo

经过大量搜索,我已经解决了这个问题

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();