0

私は現在、2 つの異なるテキスト ビューとイメージビューを含む Expandablelist の子のカスタム レイアウトを使用しています。また、常に1つの子が常に存在します。アダプターでクリックリスナーを取得できますが、アクティビティでこれを取得できません。

また、onChildClick() メソッドでクリック リスナー イベントを取得しようとしましたが、機能しませんでした。

このアダプターをさまざまなアクティビティで使用しているため、アクティビティ自体のテキスト ビューの 1 つにクリック リスナーが必要です。

任意の提案をお願いします。

4

2 に答える 2

2

私はこれを行うことができます。私android:onClick="textClickHandler">はレイアウト ファイルに追加し、以下のようにリスナーをアクティビティに含めました。

public void textClickHandler(View view){

        if(view.getId() == R.id.textMore){
            Toast.makeText(mContext, "More Text Clicked.", Toast.LENGTH_LONG).show();
        }
    }
于 2012-05-28T05:01:23.617 に答える
1

You can't set the listener on a View from the Activity(if I understood what you're trying to do). One way to do it would be to:

  • make your adapter have a boolean value that you set when you instantiate the adapter(This will tell you if the adapter belongs to an Activity where the TextView should have a listener(for true, for example). If the value is false then you'll make sure not to set a listener on the TextView).
  • set the the listener on the desired TextView(only if the previous value was true, for other activities you'll simple ignore this part) in the getChildView method of your custom adapter.
  • in the onClick method for that view's OnCLickListener use the Context that you pass in the constructor of your custom adapter. Cast it to the desired activity and use it as you want. A more elegant way of doing this would be to let the desired Activity implement a custom interface(to notify you when the TextView was clicked). Then in the adapter make a field with the type of that interface that you'll set to point to your Activity when you instantiate the listener. When the TextView is clicked call the methods of that field above and notify the Activity that something happen and it should do some stuff.
于 2012-05-28T04:56:07.170 に答える