我尝试在我的列表视图中实现 longClick,但我只在编辑文本字段中获得 longClick:如何仅在 textView 上获得 longClick?
谢谢,丹尼尔
其他详情:
- 主要布局是“activity_main.xml”,在列表视图“listView_listData”中,我将“row.xml”布局与“RigaAdvAdapter”类放在一起;
- 布局“row.xml”有两个字段:textView 和editText;
- 我使用 ArrayList 将要查看的数据存储在行中(用逗号分隔 - 例如“viewTextValue,editTextValue”);
- 我需要在每一行中获取 longClick 以提供更多选项;
- 我不需要editText中的longClick,否则复制粘贴不起作用;
- 将来我需要更多数据,所以我将使用 ArrayList 数组;
######################### 布局:activity_main.xml
<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>
######################### 布局: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>
主要活动
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;
}
}