ListView scroll interface and click event, listview scroll event

Source: Internet
Author: User

ListView scroll interface and click event, listview scroll event

MainActivity

Package com. lib. listviewtest; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. support. v7.app. appCompatActivity; import android. view. view; import android. widget. adapterView; import android. widget. arrayAdapter; import android. widget. listView; import android. widget. toast; public class MainActivity extends AppCompatActivity {private String [] data = {"Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape ", "Pineapple", "Strawberry", "Cherry", "Mango", "weijize1", "weijize2", "weijize3", "weijize4 "}; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); final ArrayAdapter
 
  
Adapter = new ArrayAdapter
  
   
(MainActivity. this, android. r. layout. simple_list_item_1, data); ListView listView = (ListView) findViewById (R. id. list_view); listView. setAdapter (adapter); listView. setOnItemClickListener (new android. widget. adapterView. onItemClickListener () {@ Override public void onItemClick (AdapterView
   Parent, View view, int position, long id) {setTitle ("click" + (position + 1) + "items"); // Toast. makeText (MainActivity. this, data [position], Toast. LENGTH_SHORT ). show (); showToast (MainActivity. this, data [position], 500) ;}}) ;}/ *** display toast, define the display length by yourself. * Param1: The activity passes in the context * param2: the content of the toast we need to display * param3: time length long type, the length of time we pass in (such as 500) */public static void showToast (final Activity activity, final String word, final long time) {activity. runOnUiThread (new Runnable () {public void run () {final Toast toast = Toast. makeText (activity, word, Toast. LENGTH_LONG); toast. show (); Handler handler = new android. OS. handler (); handler. postDelayed (new Runnable () {public void run () {toast. cancel () ;}}, time );}});}}
  
 

Activity_main.xml

 
     
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.