如何改变一个TextView的背景色内recyclerview xamarin机器人

问题描述 投票:-2回答:1

它基本上是一个recyclerview,我与一些textviews填补表现出一定的类别。我有机会获得recyclerview内点击项目的位置,但我怎么能得到实际的TextView设置背景颜色的参考?

这里是我的代码

        RecyclerView CategoriesRecyclerView;
        RecyclerView.LayoutManager CategoriesLayoutManager;
        CategoriesAdapter CategoriesAdapter;
        List<Category> categories;



      protected override void OnCreate(Bundle bundle)
           {
            base.OnCreate(bundle);


                        // Get our RecyclerView layout:
                        CategoriesRecyclerView = FindViewById<RecyclerView>(Resource.Id.CategoriesRecyclerView);


                        //............................................................
                        // Layout Manager Setup:

                        // Use the built-in linear layout manager:
                        CategoriesLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.Horizontal, true);


                        // Plug the layout manager into the RecyclerView:
                        CategoriesRecyclerView.SetLayoutManager(CategoriesLayoutManager);


                        //............................................................
                        // Adapter Setup:


                        CategoriesAdapter = new CategoriesAdapter(categories);

                        // Register the item click handler (below) with the adapter:
                        CategoriesAdapter.ItemClick += CategoriesOnItemClick;

                        // Plug the adapter into the RecyclerView:
                        CategoriesRecyclerView.SetAdapter(CategoriesAdapter);

            }

              void CategoriesOnItemClick(object sender, int position)
                    {


                       //here I want the reference to the textview 
                       // ((TextView).SetBackgroundColor(Color.Aqua);

                        Toast.MakeText(this, "This is category " + categories[position].Id + categories[position].Name, ToastLength.Short).Show();
                    }
android xamarin
1个回答
0
投票

我找到了答案,我的问题的情况下,它会帮助别人。您使用OnBindViewHolder的RecyclerView.Adapter的方法,使所有的textviews的参考名单。然后用位置来得到一个点击。

 // ADAPTER

// Adapter to connect the data set to the RecyclerView: 
public class CategoriesAdapter : RecyclerView.Adapter
{
    // Event handler for item clicks:
    public event EventHandler<int> ItemClick;

    // Underlying data set 
    public List<Category> Categories;

  public List<TextView> TextViews = new List<TextView>();

    // Load the adapter with the data set at construction time:
    public CategoriesAdapter(List<Category> categories)
    {
        this.Categories = categories;
    }

    // Create a new  CardView (invoked by the layout manager): 
    public override RecyclerView.ViewHolder  OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        // Inflate the CardView for the photo:
        View itemView = LayoutInflater.From(parent.Context).
            Inflate(Resource.Layout.CategoryTextView, parent, false);

        // Create a ViewHolder to find and hold these view references, and 
        // register OnClick with the view holder:
        CategoryViewHolder vh = new CategoryViewHolder(itemView, OnClick);


        return vh;
    }

    // Fill in the contents (invoked by the layout manager):
    public override void  OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        var vh = holder as CategoryViewHolder;



        vh.CategoryTextView.Text = Categories[position].Name;

        TextViews.Add(vh.CategoryTextView);



        if (position == 0)
        {
            vh.CategoryTextView.SetBackgroundColor(Color.Aqua); 
        }


        vh.CategoryTextView.Click += (sender, e) =>
        {

            foreach (var textView in TextViews)
            {
                textView.SetBackgroundColor(Color.White);
            }

            ((TextView)sender).SetBackgroundColor(Color.Aqua);



        };

    }

    // Return the number of photos available in the photo album:
    public override int ItemCount
    {
        get { return Categories.Count; }
    }

    // Raise an event when the item-click takes place:
    void OnClick(int position)
    {
        if (ItemClick != null)
            ItemClick(this, position);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.