开发者

change image of list on list click android:

I have a ListView of 5 rows and each rows have different images. When I click on listview at any position then ListView image on that row changes only and each row have diffrent image on list.

I have ListView items containing ImageView and TextView in each rows and by default in each row I show image corresponding to text in row and when I click on any position of list then I want to change the image corresponding to list click position. I have to show two different image on list click.

In a list there are 2 images one is for event "off" and second ima开发者_JAVA技巧ge is for "on".

public void onListItemClick(ListView parent, View v, int position, long id) { 
       Toast.makeText(getApplicationContext(), "You have selected"
                    +(position+1)+"th item",  Toast.LENGTH_SHORT).show();
}![enter image description here][1]

change image of list on list click android:


There is two approaches to achieve the same

First Approach Set the background of the list item to a State List drawable. If you set this on every item, the state list will automatically mantain the state.

Second Approach

Use HashMap to store which item the user has selected and set background depending on the user selection
Example

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;

import android.app.ListActivity;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.view.ContextMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class CustomListView extends ListActivity {
    /** Called when the activity is first created. */
    Context context = null;
    HashMap<String, Boolean> userSelectionMap = new HashMap<String, Boolean>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ArrayAdapter arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, PENS);
        setListAdapter(arrayAdapter);

        getListView().setTextFilterEnabled(true);
        ListView lv = getListView();
        this.registerForContextMenu(lv);




    }



    static final String[] PENS = new String[]{

        "item1",
        "item2",
        "item3",
        "item4",
        "item5",
        "item6",




    };

   @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    System.out.println("...selected...");
    if(isItemSelectedBefore(""+position)){
        v.setBackgroundResource(R.drawable.whbk);
    }else{
        v.setBackgroundResource(R.drawable.grbk);
    }
}

  boolean isItemSelectedBefore(String position){
      boolean userSelection = false;
      if(userSelectionMap.containsKey(position)){
          userSelection = !userSelection;
      }
      userSelectionMap.put(position, userSelection);
    return userSelectionMap.get(position);

   }

}

To change backgorunf of list view

create a selector like the following list_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" 
        android:state_pressed="false"
        android:drawable="@color/android_green" />
    <item android:state_focused="true" 
        android:state_pressed="true"
        android:drawable="@color/black_alpha" />
    <item android:state_focused="false" 
        android:state_pressed="true"
        android:drawable="@color/black_alpha" />
    <item android:drawable="@color/white_alpha" />
</selector> 

xml of listview

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜