📜 ⬆️ ⬇️

Handling a click on a separate View in the list item

Hello, Habrahabr!

In this article I will describe how to handle clicking on a separate part of the list item. Anyone interested, please under the cat.

PREHISTORY

Recently, I faced a task: to implement the ability to click on a separate View in the list item. The difficulty was that the onListItemClick () method is called when you click on any part of the list item (no matter which View is under the finger). Looking for a solution, I did not find anything useful. After a few experiments, I still reached the goal.

CREATION OF THE PROJECT

Create a project. The name, package, version of ANDROID is at your discretion. First, the markup for the list item and the Activity:
')
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); } } 


I specifically use ListActivity to shorten the lesson. We will use, of course, our custom adapter. In order not to fool around with SimpleAdapter and its ArrayList <Map <String, Object >> or BaseAdapter with its mandatory methods, I inherit my adapter from ArrayAdapter. I did not add the ViewHolder - we do not need it now. Well, finally, the adapter code itself:

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(); } } 


Let's understand what we are here. In the getView method (int position, View convertView, ViewGroup parent) we create a view for each element of the list and find the button and assign a click handler to it. When the button is pressed, a toast appears with the position of this element. As you can see, everything is very simple! True, for some reason, onListItemClick (ListView l, View v, int position, long id) stops working (at least for me). Therefore, it is necessary to code the processing of clicking on the desired button and on all the other View in the adapter itself :(. But you can do, as I wrote above, assign all OnClickListener elements, and then watch - if our button, then one action, otherwise - another. And it is even better to put all other views into a separate LinearLayout or RelativeLayout and assign the listener only to it.

Well, in general, that's all. I would be glad if this post helps someone.

Source: https://habr.com/ru/post/180969/


All Articles