我一直到处都在寻找,但是当单击“回收者”视图中的项目时,我无法找出如何进行碎片交易。所以我在徘徊,如果有人知道如何使用c#
谢谢
回收站适配器
class FavouritesPageAdapter : RecyclerView.Adapter
{
FavouritesFragment favFragment = new FavouritesFragment();
private List<Favourites> mFavourites;
private RecyclerView mRecyclerView;
public FavouritesPageAdapter(List<Favourites> favourites, RecyclerView recyclerView)
{
mFavourites = favourites;
mRecyclerView = recyclerView;
}
public override int ItemCount
{
get { return mFavourites.Count; }
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
FavouritesViewHolder viewHolder = holder as FavouritesViewHolder;
viewHolder.mCalcLabel.Text = mFavourites[position].CalcName;
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
View favouritesCard = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.favourites_cards, parent, false);
TextView calcName = favouritesCard.FindViewById<TextView>(Resource.Id.calculatorLabel);
FavouritesViewHolder view = new FavouritesViewHolder(favouritesCard) { mCalcLabel = calcName };
return view;
}
public class FavouritesViewHolder : RecyclerView.ViewHolder
{
public View mMainView { get; set; }
public TextView mCalcLabel { get; set; }
public FavouritesViewHolder (View view) : base(view)
{
mMainView = view;
}
}
}
答案 0 :(得分:0)
如果用户在点击fragment
项目时需要打开新的recyclerView
,请尝试使用您的OnBindViewHolder
替代方法进行操作
public class FavouritesPageAdapter : RecyclerView.Adapter
{
FavouritesFragment favFragment = new FavouritesFragment();
private List<Favourites> mFavourites;
private RecyclerView mRecyclerView;
Activity _activity;
public FavouritesPageAdapter(Activity activity, List<Favourites> favourites, RecyclerView recyclerView)
{
_activity=activity;
mFavourites = favourites;
mRecyclerView = recyclerView;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
FavouritesViewHolder viewHolder = holder as FavouritesViewHolder;
viewHolder.mCalcLabel.Text = mFavourites[position].CalcName;
var clickedItem = mFavourites[position];
viewHolder.mMainView.click+=delegate
{
var objFrg = new MyFragment();
var fragmentTx =_activity.SupportFragmentManager.BeginTransaction();
fragmentTx.Replace(Resource.Id.crealtabcontent, objFrg, "MyFrgTag").AddToBackStack("myFrgBackStack");
fragmentTx.Commit();
};
}
}
您的片段应该像这样
public class MyFragment :Android.Support.V4.App.Fragment
{
}
要获得点击的项目,请使用position
方法中的OnBindViewHolder
。 _activity
是您在调用适配器时必须通过的活动
var fvrtAdapter = new FavouritesPageAdapter(this.Activity,favourites,recyclerView);