0
   public override void OnBindSwipeContentHolder(ListViewHolder holder, int position)

嗨朋友我正在使用 Telerik UI for android Controls 开发 Android 应用程序请帮助我“OnBindSwipeContentHolder”未找到错误我该如何解决这个问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Com.Telerik.Widget.List;
using Android.Support.V7.Widget;
using System.Collections;

namespace test
{
 public class TelerikListView : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);


        SetContentView(Resource.Layout.ListView);
        // Create your application here
        RadListView listview = FindViewById<RadListView>(Resource.Id.listView);
        ListViewAdapter adapter = new ListViewAdapter(GetListOfCities());
        listview.SetAdapter(adapter);


        //CityAdapter adapterlist = new CityAdapter(GetListOfCities());
        //listview.SetAdapter(adapterlist);

        SelectionBehavior selectionBehavior = new SelectionBehavior();
        listview.AddBehavior(selectionBehavior);
        selectionBehavior.SetSelectionMode(SelectionBehavior.SelectionMode.Single);

        SelectionListener selectionListener = new SelectionListener(this, selectionBehavior);
        selectionBehavior.AddListener(selectionListener);

        SwipeExecuteBehavior swipeExecuteBehavior = new SwipeExecuteBehavior();
        listview.AddBehavior(swipeExecuteBehavior);

        SwipeExecuteListener swipeExecuteListener = new SwipeExecuteListener(adapter);
        swipeExecuteBehavior.AddListener(swipeExecuteListener);

        swipeExecuteBehavior.SwipeLimitStart = -50;
        swipeExecuteBehavior.SwipeLimitEnd = 50;


    }

    private List<City> GetListOfCities()
    {
        List<City> cities = new List<City>();
        cities.Add(new City("London", "United Kingdom"));
        cities.Add(new City("Berlin", "Germany"));
        cities.Add(new City("Madrid", "Spain"));
        cities.Add(new City("Rome", "Italy"));
        cities.Add(new City("Paris", "France"));
        cities.Add(new City("Hamburg", "Germany"));
        cities.Add(new City("Barcelona", "Spain"));
        cities.Add(new City("Munich", "Germany"));
        cities.Add(new City("Milan", "Italy"));
        cities.Add(new City("Cologne", "Germany"));
        return cities;
    }



    public override void OnBindSwipeContentHolder(ListViewHolder holder, int position)
    {
        RelativeLayout mainLayout = (RelativeLayout)holder.ItemView;
        LinearLayout leftLayout = (LinearLayout)mainLayout.GetChildAt(0);
        LinearLayout rightLayout = (LinearLayout)mainLayout.GetChildAt(1);

        Button leftButton = new Button(mainLayout.Context);
        leftButton.Text = "edit";

        Button rightButton = new Button(mainLayout.Context);
        rightButton.Text = "delete";

        leftLayout.RemoveAllViews();
        leftLayout.AddView(leftButton);

        rightLayout.RemoveAllViews();
        rightLayout.AddView(rightButton);
    }

}

public class CityAdapter : ListViewAdapter
{
    public CityAdapter(IList items)
        : base(items)
    {
    }

    public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        LayoutInflater inflater = LayoutInflater.From(parent.Context);
        View view = inflater.Inflate(Resource.Layout.city_list_item, parent, false);
        return new CityViewHolder(view);
    }

    public override void OnBindListViewHolder(ListViewHolder holder, int position)
    {
        CityViewHolder viewHolder = (CityViewHolder)holder;
        City city = (City)Items[position];
        viewHolder.nameView.Text = city.Name;
        viewHolder.countryView.Text = city.Country;
    }

    public class CityViewHolder : ListViewHolder
    {
        public TextView nameView;
        public TextView countryView;

        public CityViewHolder(View itemView)
            : base(itemView)
        {

            nameView = (TextView)itemView.FindViewById(Resource.Id.nameView);
            countryView = (TextView)itemView.FindViewById(Resource.Id.countryView);
        }
    }
}

public class SelectionCallback : Java.Lang.Object, ActionMode.ICallback
{
    private SelectionBehavior selectionBehavior;
    public SelectionCallback(SelectionBehavior behavior)
    {
        selectionBehavior = behavior;
    }

    public bool OnCreateActionMode(ActionMode mode, IMenu menu)
    {
        MenuInflater inflater = mode.MenuInflater;
        inflater.Inflate(Resource.Menu.menu_main, menu);
        return true;
    }

    public bool OnPrepareActionMode(ActionMode mode, IMenu menu)
    {
        return false;
    }

    public bool OnActionItemClicked(ActionMode mode, IMenuItem item)
    {
        return false;
    }

    public void OnDestroyActionMode(ActionMode mode)
    {
        selectionBehavior.EndSelection();
    }
}
public class SelectionListener : Java.Lang.Object,
SelectionBehavior.ISelectionChangedListener
{

    private ActionMode actionMode;
    private ActionMode.ICallback callback;
    private Activity activity;
    private SelectionBehavior selectionBehavior;

    public SelectionListener(Activity activity, SelectionBehavior selectionBehavior)
    {
        this.callback = new SelectionCallback(selectionBehavior);
        this.activity = activity;
        this.selectionBehavior = selectionBehavior;
    }

    public void OnSelectionStarted()
    {
        actionMode = activity.StartActionMode(callback);
    }
    public void OnItemIsSelectedChanged(int position, bool newValue)
    {
        int selectionCount = selectionBehavior.SelectedItems().Count;
        actionMode.Title = selectionCount.ToString();
    }
    public void OnSelectionEnded()
    {
        actionMode.Finish();
    }
}

/// <summary>
/// Swiping behaviour
/// </summary>
public class SwipeExecuteListener : Java.Lang.Object, SwipeExecuteBehavior.ISwipeExecuteListener
{

    private ListViewAdapter listViewAdapter;
    public SwipeExecuteListener(ListViewAdapter adapter)
    {
        listViewAdapter = adapter;
    }
    public void OnSwipeStarted(int position)
    {
    }
    public void OnSwipeProgressChanged(int position, int offset, View swipeContent)
    {
    }
    public void OnSwipeEnded(int position, int offset)
    {
        int absOffset = Math.Abs(offset);
        if (absOffset > 300)
        {
            listViewAdapter.Remove(position);
        }
        listViewAdapter.NotifySwipeExecuteFinished();
    }
    public void OnExecuteFinished(int position)
    {
    }
}
}
4

2 回答 2

1

该类Activity没有OnBindSwipeContentHolder要覆盖的方法。

在 Telerik 中,ListViewAdapter执行ListViewDataSourceAdapter以下操作:

onBindSwipeContentHolder

public void onBindSwipeContentHolder(ListViewHolder holder, int position)

由 RadListView 调用以在指定位置显示项目后面的滑动内容。此方法应更新 com.telerik.widget.list.ListViewHolder#itemView 的内容,以反映给定位置的项目后面的滑动内容。


为了实现这一点,我们需要重写 ListViewAdapter 的 onBindSwipeContentHolder(ListViewHolder holder, int position)。


我相信您在错误的类中使用了该方法,您应该在自定义列表适配器中。

public class CityAdapter : ListViewAdapter
{

~~~~
    public override void OnBindSwipeContentHolder(ListViewHolder holder, int position)
    {
        RelativeLayout mainLayout = (RelativeLayout)holder.ItemView;
        LinearLayout leftLayout = (LinearLayout)mainLayout.GetChildAt(0);
        LinearLayout rightLayout = (LinearLayout)mainLayout.GetChildAt(1);

        Button leftButton = new Button(mainLayout.Context);
        leftButton.Text = "edit";

        Button rightButton = new Button(mainLayout.Context);
        rightButton.Text = "delete";

        leftLayout.RemoveAllViews();
        leftLayout.AddView(leftButton);

        rightLayout.RemoveAllViews();
        rightLayout.AddView(rightButton);
    }

~~~~

}
于 2016-04-18T11:52:26.617 回答
1

您正在尝试覆盖从 Android继承类OnBindSwipeContentHolder的类中的方法。类只能被继承的类(Telerik 控制)覆盖。TelerikListViewActivityOnBindSwipeContentHolderListViewAdapter

所以本质上将定义OnBindSwipeContentHolderTelerikListView一个类移动到另一个CityAdapter类将解决构建错误。

于 2016-04-18T11:52:47.530 回答