Android自定义ListView重复选择背景

时间:2012-08-19 08:35:04

标签: android listview selection

我有一个自定义适配器:

public class PhraseCustomAdapter extends BaseAdapter  
{  
public String original[];  
public String translation[];  
public String transcription[];  

public Activity context;  
public LayoutInflater inflater;  

public PhraseCustomAdapter(Activity context,String[] original, String[] translation, String[] transcription) {  
    super();  

    this.context = context;  
    this.original = original;  
    this.translation = translation;  
    this.transcription = transcription;  

    inflater = LayoutInflater.from(context);
    this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
}  

@Override  
public int getCount() {  
    // TODO Auto-generated method stub  
    return original.length;  
}  

@Override  
public Object getItem(int position) {  
    // TODO Auto-generated method stub  
     return position;
}  

public String getItemTranlation(int position) {  

    return translation[position];  
}  

public String getItemTranscription(int position) {  
    // TODO Auto-generated method stub  
    return transcription[position];  
}  

public String getItemOriginal(int position) {  
    // TODO Auto-generated method stub  
    return original[position];  
} 


@Override  
public long getItemId(int position) {  
    // TODO Auto-generated method stub  
    return position;  
}  

static class ViewHolder  
{  
    ImageView imgViewLogo;  
    TextView txtViewOriginal;  
    TextView txtViewTranslation;  
    TextView txtViewTranscription; 
}  

@Override  
public View getView(int position, View convertView, ViewGroup parent) {  

    ViewHolder holder;  
    if (convertView == null) {  
        holder = new ViewHolder();  
        convertView = inflater.inflate(R.layout.phrase_row, null);  

        holder.imgViewLogo = (ImageView) convertView.findViewById(R.id.imgViewLogo);  
        holder.txtViewOriginal = (TextView) convertView.findViewById(R.id.txtViewOriginal);  
        holder.txtViewTranslation = (TextView) convertView.findViewById(R.id.txtViewTranslation);  
        holder.txtViewTranscription = (TextView) convertView.findViewById(R.id.txtViewTranscription);   

        convertView.setTag(holder);  
    }  
    else  
        holder=(ViewHolder)convertView.getTag();  


    holder.txtViewOriginal.setText(original[position]);  
    holder.txtViewTranslation.setText(translation[position]);  
    holder.txtViewTranscription.setText(transcription[position]);

    return convertView;  
}   

}

我需要实现android4风格的多选(长按,然后点击选择项)。所以:

    lview1 = (ListView) findViewById(R.id.listViewPhrase);  
    adapter = new PhraseCustomAdapter(this, original, translation, transcription);  
    lview1.setAdapter(adapter);  
    lview1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    lview1.setMultiChoiceModeListener(new MultiChoiceModeListener() {

        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position,
                                              long id, boolean checked) {
            View view;
            if (checked){
                Log.v ("checked?", "YES");
                Log.v ("Position", Integer.toString(position));

                view = lview1.getChildAt(position);
                view.setBackgroundColor(Color.LTGRAY);


                original_list.add (adapter.getItemOriginal(position));
                translation_list.add (adapter.getItemTranlation(position));
                transcription_list.add (adapter.getItemTranscription(position));

                countSelected += 1;
            }
            if (!checked){
                Log.v ("checked?", "NO");
                Log.v ("Position", Integer.toString(position));


                for (int i = 0; i < original_list.size(); i++)
                {
                    if (original_list.get(i) == adapter.getItemOriginal(position)){
                        original_list.remove (i);
                        translation_list.remove (i);
                        transcription_list.remove (i);  
                    }
                }
                countSelected -= 1;
            }


            mode.setTitle(Integer.toString(countSelected) + " " + getString(R.string.selectItem));
        }

问题是:当我长按该项目(例如,第一项)时,第7项也会突出显示(通过更改背景)。当我尝试“取消突出显示”突出显示的第7项时,应用程序崩溃。如果我尝试单击最新项目,应用程序崩溃。 我已经阅读了一些关于View如何呈现和回收项目的文章,但我不知道任何可能的问题解决方案

UPD:“取消点亮”第7项时的LogCat输出:

    V/checked?(24966): YES
V/Position(24966): 7
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x2b542210)
AndroidRuntime(24966): FATAL EXCEPTION: main
java.lang.NullPointerException
at com.alextee.phrases.PhraseActivity$1.onItemCheckedStateChanged(PhraseActivity.java:145)
at android.widget.AbsListView$MultiChoiceModeWrapper.onItemCheckedStateChanged(AbsListView.java:5688)
at android.widget.AbsListView.performItemClick(AbsListView.java:1040)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2522)
at android.widget.AbsListView$1.run(AbsListView.java:3183)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
 android.app.ActivityThread.main(ActivityThread.java:4441)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:10)

列表视图本身很复杂,它的回收也是如此。如果您观看Android开发者视频列表视图,您将了解更多有关列表视图回收及其问题的信息 您的应用中的问题是由于列表视图回收。
当您在单击或长按时更改列表项的背景时,它会改变背景,当此项目滚出设备的可见区域时,附带此项目的视图将被回收并分配给某些项目当前可见的其他列表项。所以现在该项目也将突出显示。
该图描述了回收列表视图:

enter image description here


要突出显示列表视图中的项目,您应该执行以下操作:

  • 在列表视图上设置onItemClickListener。
  • 在onItemClick()方法中,更改视图的背景并在列表视图中保存当前突出显示的位置,并在列表适配器上调用notifyDataSetChanged()。调用notifyDataSetChanged()非常重要,因为它会重绘当前可见的项目。

onItemClick列表的代码应该是这样的:

grid[pos].setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick (AdapterView<?> parent,
                    View v, int position, long Id)
            {
                    highlighted = position;    //highlighted is a global variable
                    //container is the root view of the list row layout
                    LinearLayout container = (LinearLayout)v.findViewById(R.id.container);
                    container.setBackgroundResource(R.drawable.highlighted_backg);
                    mListAdapter.notifyDataSetChanged();

            }
   });

getView()方法应该像这样实现:

public View getView (int position, View convertView, ViewGroup parent)
{
    ViewHolder holder;

    if(convertView == null) {
        convertView = inflater.inflate(R.layout.row_item, null);
        holder = new ViewHolder();
        holder.itemName1 = (TextView)convertView.findViewById(R.id.text1);
        ...
        holder.container = (LineaLayout)convertView.findViewById(R.id.container);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    if(MainActivity.highlighted == position) {
        holder.container.setBackgroundResource(R.drawable.highlighted_backg);
    }else {
        holder.foodItemCol1.setBackgroundResource(R.drawable.normal_back);
    }

    return convertView;
}

答案 1 :(得分:1)

根据已检查状态在getView()中设置项目的背景颜色。我不知道你是否可以直接从ListView获取这个,否则你可以保持例如a Set并保持当前检查的位置。 然后在getView()中查看传递的位置是否在此集合中,这意味着它会被检查并相应地设置背景。如果没有,请将背景设置为未选中的颜色。后一个很重要,因为你可以获得一个回收的View,背景设置为'checked'颜色。

可以找到跟踪已检查项目的示例here