我尝试在列表视图中实现longClick但是我只在编辑文本字段中获得longClick:如何只在textView上获取longClick?
谢谢Daniele
其他细节:
<LinearLayout 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:layout_below="@+id/textView1"
android:layout_toRightOf="@+id/textView1"
android:orientation="vertical" >
<ListView
android:id="@+id/listView_listData"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:listSelector="@drawable/list_selector"
android:fadingEdge="none" >
</ListView>
</LinearLayout>
######################### LAYOUT:row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:shrinkColumns="*" android:stretchColumns="*"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:id="@+id/RelativeLayout_rowData"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textViewField"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:padding="3dip"
android:layout_width="0dp"
android:textColor="#666666"
android:focusable="false" />
<EditText
android:id="@+id/editTextField"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:inputType="text">
<requestFocus />
</EditText>
</TableRow>
</TableLayout>
MainActivity
public class MainActivity extends Activity
{
ArrayList<String> arrayDataSource= new ArrayList<String>();
...
I fill the array "arrayDataSource" with a string as "text,another text"
...
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView_listData =(ListView)findViewById(R.id.listView_listData);
listView_listData.setAdapter(new RigaAdvAdapter());
listView_listData.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int pos, long arg3)
{
mostraToast("hh long click"+String.valueOf(pos));
return true;
}
});
}
适配器类
public class RigaAdvAdapter extends BaseAdapter
{
@Override
public int getCount() {return arrayDataSource.size();}
@Override
public Object getItem(int position) {return position;}
@Override
public long getItemId(int position) {return position;}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row, null);
}
TextView textViewField = (TextView)convertView.findViewById(R.id.textViewField);
EditText editTextField = (EditText)convertView.findViewById(R.id.editTextField);
String StringData=arrayDataSource.get(position);
String[] ArrayStringData=StringData.split(",",-1);
String textViewField_val=ArrayStringData[0];
String editTextField_val=ArrayStringData[1];
textViewField.setText(textViewField_val);
editTextField.setText(editTextField_val);
return convertView;
}
}
答案 0 :(得分:1)
不必将longclick
设置为ListView,而是必须为TextView设置它。在RigaAdvAdapter类中,您必须添加此
textViewField.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return false;
}
});
答案 1 :(得分:1)
尝试将edittext属性设置为clickable并将focusable设置为false。正如您已添加Editttext所以它将始终获得第一个焦点和触摸事件。
要在TextView
上添加Longclick事件,您必须在适配器类中实现longclick侦听器。
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row, null);
}
TextView textViewField = (TextView)convertView.findViewById(R.id.textViewField);
EditText editTextField = (EditText)convertView.findViewById(R.id.editTextField);
textViewField.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return false;
}
});
String StringData=arrayDataSource.get(position);
String[] ArrayStringData=StringData.split(",",-1);
String textViewField_val=ArrayStringData[0];
String editTextField_val=ArrayStringData[1];
textViewField.setText(textViewField_val);
editTextField.setText(editTextField_val);
return convertView;
}
答案 2 :(得分:0)
您应该在适配器的getView方法中使用onItemLongClickListener。这个问题可以帮助Android : How to set onClick event for Button in List item of ListView