Telerik Listview for android刷卡自动压缩

时间:2016-04-26 09:27:29

标签: android listview xamarin telerik xamarin.android

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)
    {

    }
}

我正在使用telerik UI for android的listview示例。但是当我向左/向右滑动我的控件时,它没有显示删除或编辑按钮

 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 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 void RefreshList()
    {

    }

    public override void NotifyRefreshFinished()
    {
        base.NotifyRefreshFinished();

        SwipeRefreshBehavior swipeRefreshBehavior = new SwipeRefreshBehavior();
        swipeRefreshBehavior.EndRefresh(true);

    }

    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);
        }
    }
}

这是我正在使用我的听众课程,请告诉我如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

由于您要在OnSwipeEnded SwipeExecuteListener方法中直接删除,因此会自动删除这些行。如果您希望在滑动时按钮显示在行中,则需要在OnBindSwipeContentHolder中实施ListViewAdapter方法。以下是Telerik Documentation的示例。

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(Context);
leftButton.Text = "edit";
Button rightButton = new Button(Context);
rightButton.Text = "delete";

rightButton.Click+= (object sender, EventArgs e) => {
            Remove(position);
            NotifySwipeExecuteFinished();
        };

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

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

编辑:尝试删除SwipeExecuteListener。如果您有这些行,请尝试删除它们

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