2013年8月1日 星期四

[ Android ] : ListView with click & long press event


after create a new project named : "ListViewSBS" :

result of this sample :
exec screenshot

click on item

long press

1. add ListView at /res/layout/activity_main.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>


</RelativeLayout>

2. add each item of your ListView :

2.1 add a new layout : /res/layout/my_listview.xml




2.2 modify "my_listview.xml", new layout with "2 TextView & 1 ImageView"

xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/RelativeLayout01"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:paddingBottom="4dip"  
    android:paddingLeft="12dip" 
    android:paddingRight="12dip"> 
   
    <TextView  
        android:text="TextView01"  
        android:layout_height="wrap_content"  
        android:textSize="20sp"  
        android:layout_width="fill_parent"  
        android:id="@+id/ItemTitle" 
        /> 
    <TextView  
        android:text="TextView02"  
        android:layout_height="wrap_content"  
        android:layout_width="fill_parent"  
        android:layout_below="@+id/ItemTitle"  
        android:id="@+id/ItemText" 
        /> 
    <ImageView  
        android:paddingTop="12dip" 
        android:layout_alignParentRight="true" 
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:id="@+id/ItemImage" 
        />  

</RelativeLayout> 

3. add icon for your ImageView :




4. source code of activity :

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        ListView list = (ListView) findViewById(R.id.listView1); 
       
        ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>(); 
        for(int i = 0; i < 10; i++) 
        { 
            HashMap<String, Object> map = new HashMap<String, Object>(); 
            map.put("ItemImage", R.drawable.ic_img);
            map.put("ItemTitle", "Title " + i); 
            map.put("ItemText", "Text Text Text Text! " + i); 
            listItem.add(map); 
        } 
        SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItem,
            R.layout.my_listview,
            new String[] {"ItemImage","ItemTitle", "ItemText"},  
            new int[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText
        ); 
        
        list.setAdapter(listItemAdapter);
       
        // add click listener
        list.setOnItemClickListener(new OnItemClickListener() { 

            @Override
            public void onItemClick(AdapterView arg0, View arg1, int arg2,
                    long arg3) {
                Toast.makeText(arg1.getContext(), "Toast......, arg2:"+arg2+", arg3:"+arg3, Toast.LENGTH_LONG).show();
            } 
        }); 
       
        // add long press listener
        list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

            @Override
            public void onCreateContextMenu(ContextMenu arg0, View arg1,
                    ContextMenuInfo arg2) {
                arg0.setHeaderTitle("ContextMenu");    
                arg0.add(0, 0, 0, "item1"); 
                arg0.add(0, 1, 0, "item2");    
            } 
        });  
    }

}

沒有留言: