简体   繁体   中英

Start activity from RecyclerView (Xamarin)

I have Xamarin app with RecyclerView.

I have TextView in block. I want to start activity on click.

Here is code:

public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    var movieViewHolder = (MovieViewHolder)holder;
    movieViewHolder.MovieNameTextView.Text = movies[position].CompanyName;
    var position_new = position + 1;
    movieViewHolder.MovieCount.Text = position_new.ToString();

    movieViewHolder.MovieNameTextView.Click += delegate {
        StartActivity(typeof(ClientLogin));
    };
}

But I have this error:

The name 'StartActivity' does not exist in the current context.
Cannot resolve symbol 'StartActivity'

在此处输入图片说明

How I can start activity from Recycler view?

尝试这个 :

movieViewHolder.MovieNameTextView.Context.StartActivity(typeof(ClientLogin));

Well yes and you should. StartActivity is a function available in the Context class, not the adapter. So grab a instance of context (maybe from the ViewHolders itemView) and call holder.ItemView.Context.StartActivity(typeof(ClientLogin)); .

You could pass in an instance of the Activity (Context) in the MoviesAdapter constructor. Save this instance in a private field and use it later when navigating to the new Activity on the click event.

Activity _context;

public MovieAdapter(Activity context)
{
    _context = context;
}

When you create the object of the Adapter you pass in the instance of the Activity:

var movieAdapter = MovieAdapter(this);

Then modify your click delegate to

movieViewHolder.MovieNameTextView.Click += delegate
{
    _context.StartActivity(typeof(ClientLogin));
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM