Xamarin ListActivity onTouchEvent(或任何其他触摸侦听器)
Xamarin ListActivity onTouchEvent (or any other touch listener)
我正在尝试学习 Xamarin,作为一个非常基础的第一个应用程序,我正在尝试使用列表适配器。列表显示得很好,但我想对其中的项目做一些事情,比如滑动等。我尝试了下面的代码,但似乎没有调用 OnTouchEvent(可能是因为它没有附加到列表视图,而是附加到something "below" 它?当我尝试在 MainActivity 中使用它时它运行良好)。我设法使用了 OnListItemClick 但这对我来说还不够。我怎样才能使 OnTouchEvent 工作或者我必须监听触摸事件的其他可能性?
感谢
public class SomeList: ListActivity
{
string[] someArr;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
someArr = getArr();
ListAdapter = new ArrayAdapter<String>(this, Android.Resource.Layout.SwipeListView, someArr);
}
public override bool OnTouchEvent(MotionEvent e) {
doSomething(e);
}
}
出于您的原因,您可以先使用列表视图。
ListView
由以下部分组成:
Rows – The visible representation of the data in the list.
Adapter – A non-visual class that binds the data source to the list
view.
Fast Scrolling – A handle that lets the user scroll the length of the
list.
Section Index – A user interface element that floats over the
scrolling rows to indicate where in the list the current rows are
located.
要创建简单列表,请执行以下操作:
为您的 listview
和 listview items
创建 xml:
ListView
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#000000"
android:id="@+id/listView1" />
</LinearLayout>
ListViewItem
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:id="@+id/relativeLayout"
android:padding="10dp">
<TextView
android:text="Name"
android:textColor="@android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtName" />
</RelativeLayout>
Listview
需要一个适配器来显示项目,所以我们应该创建一个:
public class ListAdapter : BaseAdapter<string>
{
List<string> _list;
Activity _context;
public ListAdapter(Activity context, List<string> list)
{
_context = context;
_list = list;
}
public override long GetItemId(int position)
{
return position;
}
public override Android.Views.View GetView(int position, Android.Views.View convertView, Android.Views.ViewGroup parent)
{
View view = convertView;
if (view == null)
{
view = _context.LayoutInflater.Inflate(Resource.Layout.ListItem, null);
}
TextView txtName = view.FindViewById<TextView>(Resource.Id.txtName);
var item = _list[position];
txtName.Text = item.UserName;
return view;
}
public override int Count
{
get
{
return _list.Count;
}
}
public override string this[int index]
{
get
{
return _list[index];
}
}
}
然后在您的 Activity
中将您的适配器设置为 listview
:
ListView _listView;
ListAdapter _adapter;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.List);
_listView = FindViewById<ListView>(Resource.Id.listView1);
_listView.ItemClick += ItemClick;
_adapter = new ListAdapter(this, myList);
_listView.Adapter = _adapter;
}
void ItemClick (object sender, AdapterView.ItemClickEventArgs e)
{
//Do stuff on item click.
}
就是这样。
详细教程请查看Xamarin Documentation
更多链接:
Swipe To Refresh
对于更复杂的功能和灵活性,请检查 RecyclerView
我正在尝试学习 Xamarin,作为一个非常基础的第一个应用程序,我正在尝试使用列表适配器。列表显示得很好,但我想对其中的项目做一些事情,比如滑动等。我尝试了下面的代码,但似乎没有调用 OnTouchEvent(可能是因为它没有附加到列表视图,而是附加到something "below" 它?当我尝试在 MainActivity 中使用它时它运行良好)。我设法使用了 OnListItemClick 但这对我来说还不够。我怎样才能使 OnTouchEvent 工作或者我必须监听触摸事件的其他可能性?
感谢
public class SomeList: ListActivity
{
string[] someArr;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
someArr = getArr();
ListAdapter = new ArrayAdapter<String>(this, Android.Resource.Layout.SwipeListView, someArr);
}
public override bool OnTouchEvent(MotionEvent e) {
doSomething(e);
}
}
出于您的原因,您可以先使用列表视图。
ListView
由以下部分组成:
Rows – The visible representation of the data in the list.
Adapter – A non-visual class that binds the data source to the list view.
Fast Scrolling – A handle that lets the user scroll the length of the list.
Section Index – A user interface element that floats over the scrolling rows to indicate where in the list the current rows are located.
要创建简单列表,请执行以下操作:
为您的 listview
和 listview items
创建 xml:
ListView
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#000000"
android:id="@+id/listView1" />
</LinearLayout>
ListViewItem
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:id="@+id/relativeLayout"
android:padding="10dp">
<TextView
android:text="Name"
android:textColor="@android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtName" />
</RelativeLayout>
Listview
需要一个适配器来显示项目,所以我们应该创建一个:
public class ListAdapter : BaseAdapter<string>
{
List<string> _list;
Activity _context;
public ListAdapter(Activity context, List<string> list)
{
_context = context;
_list = list;
}
public override long GetItemId(int position)
{
return position;
}
public override Android.Views.View GetView(int position, Android.Views.View convertView, Android.Views.ViewGroup parent)
{
View view = convertView;
if (view == null)
{
view = _context.LayoutInflater.Inflate(Resource.Layout.ListItem, null);
}
TextView txtName = view.FindViewById<TextView>(Resource.Id.txtName);
var item = _list[position];
txtName.Text = item.UserName;
return view;
}
public override int Count
{
get
{
return _list.Count;
}
}
public override string this[int index]
{
get
{
return _list[index];
}
}
}
然后在您的 Activity
中将您的适配器设置为 listview
:
ListView _listView;
ListAdapter _adapter;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.List);
_listView = FindViewById<ListView>(Resource.Id.listView1);
_listView.ItemClick += ItemClick;
_adapter = new ListAdapter(this, myList);
_listView.Adapter = _adapter;
}
void ItemClick (object sender, AdapterView.ItemClickEventArgs e)
{
//Do stuff on item click.
}
就是这样。 详细教程请查看Xamarin Documentation
更多链接: Swipe To Refresh
对于更复杂的功能和灵活性,请检查 RecyclerView