必须继承 IBeginDragHandler, IDragHandler, IEndDragHandler 这几个接口,否则拖动的时候会响应列表元素的点击 事件。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using SimpleJSON;
using YanhuaMMO;
public class DynamicHeightList : MonoBehaviour , IBeginDragHandler, IDragHandler, IEndDragHandler
{
public Vector2 minCellSize;
public int maxRowCount = 5;
public int columeCount = 1;//列数
public Vector2 spaceSize;
private List<DynamicInfinityItem> itemList;
public GameObject renderGo;
protected IList mDataProviders;
private Vector2 lastDownMousePos;
private bool isMouseDown = false;
public int elasticDist = 25;
private int validCount = 0;
private float viewheight;
private float zuni = 0.95f;
private Coroutine guanxingCo;
private List<DynamicInfinityItem> allItemRenders;
private Vector3 startDownPos;
// Use this for initialization
void Start () {
viewheight = gameObject.GetComponent<RectTransform>().sizeDelta.y;
itemList = new List<DynamicInfinityItem>();
//validCount = Mathf.Min(mDataProviders.Count, maxRowCount * columeCount);
initItem();
// updateValidItem();
//AddTriggersListener(gameObject, EventTriggerType.PointerDown, startDrag);
//AddTriggersListener(gameObject, EventTriggerType.PointerUp, stopDrag);
}
public void OnBeginDrag(PointerEventData eventData)
{
startDrag(eventData);
}
public void OnEndDrag(PointerEventData eventData)
{
stopDrag(eventData);
}
public void OnDrag(PointerEventData eventData)
{
}
public void setDataByJsonArray(string dataarr)
{
JSONArray dataList = JSON.Parse(dataarr).AsArray;
List<JSONNode> dataNode = new List<JSONNode>();
for (int i = 0; i < dataList.Count; i++)
dataNode.Add(dataList[i]);
setDataProvider(dataNode);
}
public void setDataProvider(IList dataprovider)
{
StopAllCoroutines();
mDataProviders = dataprovider;
validCount = Mathf.Min(mDataProviders.Count, maxRowCount * columeCount);
updateValidItem();
if (itemList.Count == 0)
return;
itemList[0].transform.localPosition = new Vector3(0, 0, 0);
updateItemPos();
}
private void startDrag(BaseEventData data)
{
isMouseDown = true;
if (itemList.Count == 0)
return;
lastDownMousePos = Input.mousePosition;
startDownPos = Input.mousePosition;
if (guanxingCo != null)
StopCoroutine(guanxingCo);
StopAllCoroutines();
StartCoroutine(moveContent());
}
private void stopDrag(BaseEventData data)
{
isMouseDown = false;
if (itemList.Count == 0)
return;
if (itemList[0].itemIndex == 0 && itemList[0].transform.localPosition.y < 0)