TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Bob Karanja
NA
7
707
C# recyclerview card in fragment doesnt respond to click
Jan 9 2017 1:15 PM
Hello Guys, I have the following code for a particular fragment in android. Problem is everything displays particularly nice but does not respond to the Itemclick method when I click on an item(card) on the recyclerview. Where could the problem be?
{
public class Fragment1 : SupportFragment
{
RecyclerView.LayoutManager mLayoutManager;
PhotoAlbumAdapter mAdapter;
PhotoAlbum mPhotoAlbum;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
RecyclerView recyclerView = inflater.Inflate(Resource.Layout.Fragment1, container, false) as RecyclerView;
mPhotoAlbum = new PhotoAlbum();
// Instantiate the adapter and pass in its data source:
// Plug in the linear layout manager:
mLayoutManager = new LinearLayoutManager(recyclerView.Context);
recyclerView.SetLayoutManager(mLayoutManager);
mAdapter = new PhotoAlbumAdapter(mPhotoAlbum);
mAdapter.ItemClick += OnItemClick;
// SetUpRecyclerView(recyclerView);
recyclerView.SetAdapter(mAdapter);
return recyclerView;
}
void OnItemClick(object sender, int position)
{
int photoNum = position + 1;
Toast.MakeText(Context, "This is photo number" + photoNum, ToastLength.Short);
Console.WriteLine("The Card is clicked");
}
}
public class PhotoViewHolder : RecyclerView.ViewHolder
{
public ImageView Image { get; private set; }
public TextView Caption { get; private set; }
public PhotoViewHolder(View itemView, Action<int> listener) : base(itemView)
{
Image = itemView.FindViewById<ImageView>(Resource.Id.imageView);
Caption = itemView.FindViewById<TextView>(Resource.Id.textView);
itemView.Click += (sender, e) => listener(base.Position);
}
}
public class PhotoAlbumAdapter : RecyclerView.Adapter
{
public PhotoAlbum mPhotoAlbum;
public event EventHandler<int> ItemClick;
public PhotoAlbumAdapter(PhotoAlbum photoAlbum)
{
mPhotoAlbum = photoAlbum;
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
View itemView = LayoutInflater.From(parent.Context).
Inflate(Resource.Layout.List_Item, parent, false);
PhotoViewHolder vh = new PhotoViewHolder(itemView, OnClick);
return vh;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
PhotoViewHolder vh = holder as PhotoViewHolder;
vh.Image.SetImageResource(mPhotoAlbum[position].PhotoID);
vh.Caption.Text = mPhotoAlbum[position].Caption;
}
public override int ItemCount
{
get { return mPhotoAlbum.NumPhotos; }
}
void OnClick(int position)
{
if (ItemClick != null)
ItemClick(this, position);
}
}
}
I think the problem may be "OnClick" method because the fragment is supposed to register "Context" instead of "this" which applies an activity, but context cannot be reached in the OnClick scope. How can I make it work? Or any other way this may work.
Reply
Answers (
0
)
How to Compare Prefix of barcode ?
Getting the CSS class name from HTML doc using C#