0

[TextView TextView ImageButton]を使用してAndroidにリストビューを持っています。ArrayAdapter を使用してリスト ビューを実装します。私の要件に従って、リストビューにはシングルプレスイベントとロングプレスイベントが必要なので、長押しにはregisterForContextMenu(getListView())を使用し、シングルプレスにはonListItemClickを使用します。これはうまくいきました。その後、リストビューのimageButtonのonClickイベントと、imageButtonのonclickを実装する必要があります。

今、私の問題は、listView の imageButton に onClick リスナーを実装した後、シングル プレス イベントと長押しイベントが機能しないことです。しかし、ImageButton onClick リスナーは正常に機能しています。

プロパティandroid:focusable="false"を imageButton に追加しましたが、役に立ちません。

ImageButton の onclick リスナーで単一および長押しイベントを取得するにはどうすればよいですか? 助けてください。

group.xml:

......
<ListView
    android:id="@id/android:list"
    android:focusable="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>
.....

group_row.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        ......
        android:focusable="false" />
    <ImageButton
        android:id="@+id/group_row_gadd"
        .....
        android:focusable="false"
        android:background="@drawable/add" />
    <TextView
        android:id="@+id/group_row_grow"
        ....
        android:focusable="false" />
</RelativeLayout>

私のarrayadapterクラス:

public class GroupAdapter extends ArrayAdapter<String> implements OnClickListener{
    ....    

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.group_row, parent, false);

        name = (TextView) rowView.findViewById(R.id.group_row_gname);
        number = (TextView) rowView.findViewById(R.id.group_row_grow);
        add = (ImageButton) rowView.findViewById(R.id.group_row_gadd);
        add.setTag(position);
        add.setOnClickListener(this);

        return rowView;
    }

    public void onClick(View v) {
        Log.e("onclick", "onclick");
        Integer position = (Integer) v.getTag();
        switch (v.getId()) {
        case R.id.group_row_gadd:

            break;
        }

    }

}

メインクラス:

public class Group extends ListActivity{
    .....   
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);
        setContentView(R.layout.group);
        registerForContextMenu(getListView());
        .....   
    }// OnCreate End

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        .....
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.group_edit_remove, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        .....
        return false;
    }   

}
4

2 に答える 2

0

ImageButton はフォーカスを取得できないためです。ListView で OnItemClickListener を設定すると、ListView の項目が最優先でフォーカスされます。そのため、ImageButton をクリックしても機能しません。GroupAdapter を変更できます。

于 2012-03-13T07:50:52.493 に答える
0

group_row.xmlandroid:focusableInTouchMode="false"のビュー要素にも設定する必要があると思います

于 2012-03-13T07:36:59.807 に答える