我是Android新手。我有一個導航抽屜,我在這裏爲物品使用了recyclerview。我有三個選項卡,代表viewpager中的三個片段。我的問題是,我無法通過點擊回收查看項目打開一個片段。另一個問題是我嘗試了FragmentTransaction,但不知道如何獲取片段ID。請幫我解決它。你可以給我任何教程鏈接。如何點擊recyclerview項目時打開viewpager片段
這裏是我的代碼...
在我recyclerview適配器我曾嘗試:
public void onBindViewHolder(MyViewHandler holder, int position) {
final Information current = data.get(position);
holder.title.setText(current.title);
holder.icon.setImageResource(current.iconId);
holder.title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fragmentJump(current);
}
});
}
private void fragmentJump(Information mItemSelected) {
mFragment = new FragmentIncome();
mBundle = new Bundle();
mBundle.putParcelable("item_selected_key", mItemSelected);// here my Information class is not parcelable how to solve it?
mFragment.setArguments(mBundle);
switchContent(R.id.frag1, mFragment);/// Here how to get the fragment id in R.id.frag1 of viewpager
}
public void switchContent(int id, Fragment fragment) {
if (mContext == null)
return;
if (mContext instanceof MainActivity) {
MainActivity mainActivity = (MainActivity) mContext;
Fragment frag = fragment;
mainActivity.switchContent(id, frag);
}
}
而且在MainActivity我創造了這個方法:
public void switchContent(int id, Fragment fragment) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(id, fragment, fragment.toString());
ft.addToBackStack(null);
ft.commit();
}
我跟隨此鏈接how to open a different fragment on recyclerview OnClick
請發佈你試圖打開片段? – Nisarg