リストアイテム内の単一のビューを処理する

こんにちは、Habrahabr!



この記事では、リスト項目の特定の部分をクリックする方法を説明します。 猫の下で誰が興味を持っています。



背景


最近、私はタスクに直面しました。リストアイテムの別のビューをクリックする機能を実現することです。 問題は、リスト項目のいずれかの部分をクリックしたときにonListItemClick()メソッドが呼び出されることです(どのビューが指の下にあっても)。 解決策を探して、私は有用なものを見つけられませんでした。 いくつかの実験を行った後、私はまだ目標を達成しました。



プロジェクトの作成


プロジェクトを作成します。 名前、パッケージ、バージョンANDROID-あなたの裁量で。 まず、リストアイテムとアクティビティのマークアップ:



res / layout / row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@ +id/text_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <Button android:id="@+id/btn_position" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> </LinearLayout>
      
      







MainActivity.java



 import android.os.Bundle; import android.app.ListActivity; public class MainActivity extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] objects = new String[] {"iOS", "Windows Mobile", "Windows Phone", "Android", "Symbian", "Java", "Ubuntu", "Windows", "Linux", "Mac OS", "DOS", "ChromeOS"}; MyListAdapter adapter = new MyListAdapter(this, objects); setListAdapter(adapter); } }
      
      







特に、ListActivityを使用してレッスンを短縮します。 もちろん、カスタムアダプタを使用します。 SimpleAdapterとそのArrayList <Map <String、Object >>またはBaseAdapterを必要なメソッドでだまさないために、ArrayAdapterからアダプターを継承します。 ViewHolderは追加しませんでした。今は必要ありません。 最後に、アダプターコード自体:



MyListAdapter.java

 import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MyListAdapter extends ArrayAdapter<String> implements OnClickListener { private Context context; private String[] objects; public MyListAdapter(Context c, String[] data) { super(c, IGNORE_ITEM_VIEW_TYPE, data); context = c; objects = data; } public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = LayoutInflater.from(context); View view = inflater.inflate(R.layout.row, null, true); TextView text_view = (TextView) view.findViewById(R.id.text_view); text_view.setText(objects[position]); text_view.setOnClickListener(this); Button btn_position = (Button) view.findViewById(R.id.btn_position); btn_position.setTag(String.valueOf(position)); btn_position.setOnClickListener(this); return view; } public void onClick(View view) { if (view.getId() == R.id.btn_position) { Toast.makeText(context, "position = " + view.getTag().toString(), Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context, "onListItemClick", Toast.LENGTH_SHORT).show(); } }
      
      







ここで行ったことを理解しましょう。 getViewメソッド(int position、View convertView、ViewGroup parent)では、各リストアイテムのビューを作成し、同じ場所でボタンを見つけてクリックハンドラーを割り当てます。 ボタンを押すと、この要素の位置とともにトーストが表示されます。 ご覧のとおり、すべてが非常に簡単です! しかし、同時に、 onListItemClick(ListView l、View v、int position、long id)何らかの理由(少なくとも私にとって)で機能しなくなります。 したがって、目的のボタンとアダプター自体の他のすべてのビューをクリックする処理をコーディングする必要があります:他のすべてのビューを別のLinearLayoutまたはRelativeLayoutに移動し、リスナーのみをそれに割り当てるとさらに良いでしょう。



まあ、それは基本的にそれです。 この投稿が誰かを助けてくれたら嬉しいです。



All Articles