如何从recyclerviewAdapter调用片段

时间:2017-06-15 08:34:46

标签: android android-fragments android-recyclerview onitemclicklistener

我有一个片段,它包含recyclerview列表,我想为列表项实现onclicklistener,点击应该导致另一个片段。 有人可以帮忙吗? MyrecyclerviewAdapter

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.DataObjectHolder>  implements View.OnClickListener{
private static String LOG_TAG = "MyRecyclerViewAdapter";
private static MyClickListener myClickListener;
private ArrayList<DataObject> mDataset;

Button btn_available;
private Context context;
@Override
public void onClick(View view) {

}

public static class DataObjectHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    ImageView car_image;
    TextView name,brand;

    public DataObjectHolder(View itemView) {
        super(itemView);
        name=(TextView)itemView.findViewById(R.id.name);
        brand=(TextView)itemView.findViewById(R.id.brand);
        car_image=(ImageView)itemView.findViewById(R.id.car_image);
        itemView.setClickable(true);
        itemView.setOnClickListener(this);

        Log.i(LOG_TAG, "Adding Listener");

    }


    @Override
    public void onClick(View v) {
        Log.i(LOG_TAG,"onclick");




       //listCar1.getFragmentManager().beginTransaction().replace(R.id.containerView,listCar1).commit();
       // myClickListener.onItemClick(getPosition(), v);
    }
}
public MyRecyclerViewAdapter(ArrayList<DataObject> myDataset) {
    this.mDataset = myDataset;

}
public interface MyClickListener {


    public void onItemClick(int position, View v);
}

myfragment class

public class MyCarFragment extends Fragment implements 
AdapterView.OnItemClickListener {

String tag="MyCar fragment";

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
Button btn_available;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, @Nullable Bundle savedInstanceState) {
    View 
rootview=inflater.inflate(R.layout.recyclerview_mycar,container,false);
    mRecyclerView = (RecyclerView) 
rootview.findViewById(R.id.my_recycler_view);

    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLayoutManager);
    mAdapter = new MyRecyclerViewAdapter(getDataSet());
    mRecyclerView.setAdapter(mAdapter);
    RecyclerView.ItemDecoration itemDecoration = new 
    DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL);
    mRecyclerView.addItemDecoration(itemDecoration);


    return rootview;
}

private ArrayList<DataObject> getDataSet() {
    ArrayList<DataObject> results = new ArrayList<DataObject>();
    for (int index = 0; index < 1; index++) {
        DataObject obj = new 
DataObject("abc","benz","http://www.pngall.com/wp-
content/uploads/2016/07/Car-Free-Download-PNG.png");
        results.add(obj);
        obj = new DataObject("bbb","toyota","http://www.pngall.com/wp-
 content/uploads/2016/07/Car-Free-Download-PNG.png");
        results.add(obj);
    }
    return results;
}

1 个答案:

答案 0 :(得分:0)

来自android.developer网站:

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

将它放在你的onClickListener中,肯定你需要将它与你的代码相匹配。