我有一個片段,其中包含recyclerview列表,我想爲列表項目實現一個onclicklistener,並且點擊應該導致另一個片段。 任何人都可以幫忙嗎? MyrecyclerviewAdapter如何從recyclerviewAdapter調用片段
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類
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;
}
任何人都可以告訴我一個解決方案嗎? – Anu