- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望有人能帮我解决这个问题。我有一个 ListView ,我希望列表的每一行都可以点击并且可以长时间点击。 setOnItemLongClickListener 正在工作,但我无法让该行可点击。我看过这个帖子ListView with clickable/editable widget和类似的东西,但我的行中没有可聚焦的项目,所以我不知道还有什么可以。
这是我在 xml 中对 listView 的定义:
<ListView
android:id="@id/android:list"
android:textFilterEnabled="true"
android:layout_width="match_parent"
android:fadeScrollbars="false"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
这是我的行的定义(有点难看):
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:longClickable="true"
android:descendantFocusability="blocksDescendants">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
tools:ignore="UselessParent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imageViewAgenda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/historicos"
android:src="@android:drawable/ic_menu_agenda" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/avisoVerdeFilaHist"
android:textIsSelectable="false"
style="@style/CuadroNotificacion"
android:background="@drawable/borde_blanco"/>
<TextView
android:id="@+id/avisoRojoFilaHist"
android:textIsSelectable="false"
style="@style/CuadroNotificacion"
android:background="@drawable/borde_blanco"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayoutTarea"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textViewTarea"
android:textIsSelectable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="5dp"
android:text="@string/tarea2"/>
<TextView
android:id="@+id/textViewTitulo"
android:textIsSelectable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Titulo"/>
</LinearLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textViewFil1Col1"
android:textIsSelectable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="f1c1"/>
<TextView
android:id="@+id/textViewFil1Col2"
android:textIsSelectable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="f1c2"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textViewFil2Col1"
android:textIsSelectable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="f2c1"/>
<TextView
android:id="@+id/textViewFil2Col2"
android:textIsSelectable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="f2c2"/>
</TableRow>
</TableLayout>
</LinearLayout>
</TableRow>
</TableLayout>
如果您想知道为什么我使用 tableLayout 是因为 android:stretchColumns="1"属性。 请注意,我使用 android:descendantFocusability="blocksDescendants"。以防万一。
这是我的自定义光标适配器:
public class ListaTareasCursorAdapter extends SimpleCursorAdapter implements Filterable{
private Context context;
private int layout;
private String[] from;
private int[] to;
private FragmentManager fragmentManager;
public ListaTareasCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int flags, FragmentManager fragmentManager) {
super(context, layout, c, from, to, flags);
this.context = context;
this.layout = layout;
this.from = from;
this.to = to;
this.fragmentManager=fragmentManager;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
return v;
}
@Override
public void bindView(View v, Context context, Cursor cursor) {
//Columna de la BD que queremos recuperar
String columna = null;
//Indice de la columna de BD
int nombreCol = 0;
//Resultado de obtener el indice de la columna de BD
String nombre = null;
//Nombre del textView en nuestro Layout donde queremos
//que aparezca el resultado.
TextView nombre_text = null;
String nombreCompleto = ""; //almacena nombre completo del empleado
String fechaCompleta = ""; //almacena fecha y hora
String nombreTarea = ""; //almacena nombre de la tarea
//Para cada valor de la BD solicitado, lo mostramos en el text view.
for (int i=0; i<from.length; i++){
columna = from[i];
nombreCol = cursor.getColumnIndex(columna);
nombre = cursor.getString(nombreCol);
if(nombre==null){
nombre = "--";
}
if(columna.equals(Tarea.NOMBRE)){
nombreTarea = nombre;
}
if(columna.equals(Tarea.NOMBRE_EMPLEADO)){
nombreCompleto = nombre;
}
if(columna.equals(Tarea.APELLIDO_EMPLEADO)){
nombreCompleto = nombreCompleto + " " + nombre;
nombre = nombreCompleto;
}
if(columna.equals(Tarea.FECHA)){
fechaCompleta = "Pautado: " +nombre;
}
if(columna.equals(Tarea.HORA)){
fechaCompleta = fechaCompleta + " " + nombre;
nombre = fechaCompleta;
}
if(columna.equals(Tarea.FECHA_FINALIZACION)){
nombre = "Finalizado: " + nombre;
}
nombre_text = (TextView) v.findViewById(to[i]);
if (nombre_text != null) {
nombre_text.setText(nombre);
}
}
}
}
这里是我调用听众的地方:
public class TareasActivity extends ListFragment{
....
ListView lvTareas = (ListView) view.findViewById (android.R.id.list);
final ListaTareasCursorAdapter listCursorAdapterTareas = new ListaTareasCursorAdapter(context, R.layout.activity_fila_tarea, mCursorTareas, from, to, 0,this.getFragmentManager());
lvTareas.setAdapter(listCursorAdapterTareas);
lvTareas.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub
Log.v("CLICK","pos"+" "+pos);
}
});
lvTareas.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub
Log.v("long clicked","pos"+" "+pos);
return true;
}
});
}
一定是我遗漏了什么愚蠢的东西。
任何想法将不胜感激,感谢阅读。
更多信息:我不知道这是否有用,但我正在使用 TabHost。
最佳答案
您需要重写 ListFragment 中的 onListItemClick 方法,而不是使用 onItemClick。您不需要调用 setOnItemClickListener。
关于android - setOnItemClickListener NOT WORKING 和 setOnItemLongClickListener 在 ListView 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17324076/
我遇到了这个问题,我无法收到此事件,某些东西阻止了我在 listView 中的一个项目,但我不知道是什么,我检查了对设置为 false 的项目的关注,但它没有用。还要检查将项目设置为 onlongcl
我试图让 Spinner 在用户长时间点击某个项目时表现不同。我有一些项目的微调器,我想要两件事。 当用户简单点击一个项目时,我想正常选择它。 当用户长时间点击某个项目时,我想显示对话框,其中包含“编
我在 Android ListView 中有一个消息列表。我想在长按时从列表中删除消息。我已经在列表选择器中声明了所有状态,但仍处于长按状态。长按后,列表中的特定项目不会保持焦点或选中状态。我尝试设置
这是我的代码: clientGridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
我的问题是这样的:当我长按项目时,会出现两个 View 。 请参阅下面我的 java 类代码。请帮忙。 mListView.setOnItemClickListener(new AdapterView
在下面的代码中,onItemLongClick 仅在我注释掉 onTouchListener 时才起作用。我需要能够注册滑动以及长按。我做错了什么? public class MainPageActi
我创建了一个功能,当用户简单地点击该项目时,在点击项目的屏幕上显示 Toast,一个警告: 我的 .ts 文件中的函数: itemSelected(item: string) { this.t
我正在尝试将 OnItemLongClickListener 用于 Android 上的 listView。此代码在添加到 onCreate 方法时工作正常。 mContactList.setOnIt
在 ListView 中,我同时使用了 setOnItemClickListener() 和 setOnItemLongClickListener()。当我点击一个项目时它工作正常但是当我长按一个项目
我希望有人能帮我解决这个问题。我有一个 ListView ,我希望列表的每一行都可以点击并且可以长时间点击。 setOnItemLongClickListener 正在工作,但我无法让该行可点击。我看
我正在使用 Carl Bauer (https://github.com/bauerca/drag-sort-listview) 的优秀拖动排序 ListView 来实现启用拖动排序的 ListVie
我通过扩展 BaseExpandableListAdapter 来填充展开的 ListView 。 ChildView 的代码: public View getChildView(final int
我是一名优秀的程序员,十分优秀!