OnItemClick interface and OnItemLongClick interface

ListView list item click event


OnItemClick interface
(1) Overview The
    OnItemClick interface is responsible for monitoring the list item click event.

(2) Common method
void onItemClick(AdapterView<?> parent, View view, int position, long id)
    Function: Monitor the event when the user clicks the list item

Description:
1) Parameter -parent: the current list object, such as the ListView object.
2) Parameter -view: The list item object that was clicked.
3) Parameter -position: the index value of the clicked view in the adapter.
4) Parameter -id: the index value of the clicked list item in the list.
position has the same value as id.


OnItemLongClick interface
(1) Overview The
    OnItemLongClick interface is responsible for monitoring the event of the user long pressing the list item.

(2) Common method
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
    Function: monitor the user's long-pressed list item event.

Parameter description: See the description of the parameters in onItemClick(). .Return

: Return true means that this method handles the long press event, and the event will not dispatch other controls

file processing. If it returns false, the long press event will also be dispatched to other controls for processing.

Description: Long press refers to the event that touches the control for more than 0.5 seconds.



Brief summary:
    OnItemClick is the listener for short press operations.
    OnItemLongClick is the listener


instance for long press operations:

package com.jxust.day_05_01_listviewdemo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	ListView mlvGeneral;
	List<GeneralBean> mGenerals; // Represents a collection of ten military strategists
	GeneralAdapter mAdapter;
	int[] resid = { R.drawable.baiqi, R.drawable.caocao, R.drawable.chengjisihan,
			R.drawable.hanxin, R.drawable.lishimin, R.drawable.nuerhachi, R.drawable.sunbin,
			R.drawable.sunwu, R.drawable.yuefei, R.drawable.zhuyuanzhang };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView(R.layout.activity_main);
		initData(); // initialize data
		initView();
		setListener(); //Set the listener
	}

	private void setListener() {
		setOnItemClickListener();
		setOnItemLongClickListener();
	}

	// long press operation
	private void setOnItemLongClickListener() {
		mlvGeneral.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position,
						long id) {
				Toast.makeText(MainActivity.this, mGenerals.get(position).getName()+"被长按", 2000).show();
				//The function of false is to tell the system that it has not processed it and hand it over to other listeners, so that the listener for the short press operation is reached.
				//The role of true is to tell the system that it has been processed
				return true;	
			}
		});
	}

	// short press
	private void setOnItemClickListener() {
		mlvGeneral.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Toast.makeText(MainActivity.this, mGenerals.get(position).getName()+"被短按", 2000).show();
			}
		});
	}

	private void initView() {
		mlvGeneral = (ListView) findViewById(R.id.lvGeneral);
		mAdapter = new GeneralAdapter(); // create the adapter
		// Associate with ListView
		mlvGeneral.setAdapter(mAdapter);
	}

	private void initData() {
		// Convert the string array in the resource to a Java array
		String[] names = getResources().getStringArray(R.array.generals);
		mGenerals = new ArrayList<GeneralBean>();
		for (int i = 0; i < names.length; i++) {
			GeneralBean bean = new GeneralBean(resid[i], names[i]);
			mGenerals.add(bean);
		}

	}

	// Define the adapter class that represents the military strategist
	class GeneralAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return mGenerals.size();
		}

		@Override
		public Object getItem(int position) {
			return mGenerals.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// Get the layout of the ListView and convert it to an object of type View
			View layout = View.inflate(MainActivity.this, R.layout.item_generals, null); // root represents the parent container
			// Find the ImageView that displays the fighter's avatar
			// The role of layout is to convert the target to layout.item_generals
			ImageView ivThumb = (ImageView) layout.findViewById(R.id.ivThumb);
			// Find the TextView that displays the military strategist's name
			TextView tvName = (TextView) layout.findViewById(R.id.tvName);
			// Get the strategist object whose subscript is position in the strategist
			GeneralBean bean = (GeneralBean) getItem(position);
			// Display the fighter's avatar
			ivThumb.setImageResource(bean.getResid());
			// Display the military strategist's name
			tvName.setText(bean.getName());

			return layout;
		}

	}
}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326779927&siteId=291194637
Recommended