0

我尝试在我的列表视图中实现 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;
            }
        }
4

3 回答 3

1

longclick您必须为 TextView 设置它,而不是设置为 ListView。在 RigaAdvAdapter 类中,您必须添加它

  textViewField.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            return false;
        }
    });
于 2013-10-28T11:06:53.583 回答
1

尝试将您的 edittext 属性设置为可单击和可聚焦为 false。由于您添加了 Editttext,因此它将始终获得第一个焦点和触摸事件。

并且要添加 Longclick 事件,TextView您必须在适配器类中实现 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;
        }
于 2013-10-28T11:10:11.967 回答
0

您应该在适配器的 getView 方法中使用 onItemLongClickListener。这个问题可以帮助Android:如何在ListView的List item中为Button设置onClick事件

于 2013-10-28T11:15:35.457 回答