0

我有一个包含两个 textview 和一个 imageview 的项目列表。我用 ArrayAdapter 填充列表。除了单击时更改列表项颜色外,一切工作正常。我的列表视图中有 22 个项目。主要是列表视图在屏幕上显示 10 个项目并在滚动时获取其他项目。现在我的问题是当我单击 0-9 之间的单个项目(初始 10 个项目)时,项目会在单击时正确更改颜色,但是当我滚动并单击时在位置大于 9 的项目上(在最初的 10 个项目之后)我的活动崩溃。我指的是http://www.mail-archive.com/android-developers@googlegroups.com/msg09740.html链接来编写代码在。for loop帮助我摆脱这个问题。任何建议或解决方案将不胜感激。提前谢谢。

 @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        //  requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); // to hide the virtual keyboard
            setContentView(R.layout.defect_pic_listview);

            try{

                adapter = new MyArrayAdapter(this,makeList());
                setListAdapter(adapter);
                adapter.notifyDataSetChanged();
                getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
                getListView().setOnItemClickListener(new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
                        //   Toast.makeText(getApplicationContext(), "Item "+position+" is clicked",
                        //            Toast.LENGTH_SHORT).show();

                             System.out.println("position"+position);
                                int first = getListView().getFirstVisiblePosition();
                                System.out.println("first="+first);
                                int last = getListView().getLastVisiblePosition();
                                System.out.println("last="+last);
                                int total = last - first;
                                System.out.println("total="+total);
                                if(getListView().isItemChecked(position)){
                                    for(int i = 0 ; i <= last ; i++){
                                        System.out.println("i="+i);
                                        if(first+i == position){
                                            getListView().getChildAt(i).setBackgroundColor(Color.GREEN);
                                            System.out.println("l1="+getListView());
                                    //      l.getItemAtPosition(i);
                                    //      System.out.println("l position"+l);
                                        }
                                        else{
                                            getListView().getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
                                            System.out.println("l2="+getListView());
                                        }
                                    }
                                }
                                else{
                                    getListView().getChildAt(position - first).setBackgroundColor(Color.TRANSPARENT); 
                                }
                            }
                    });
            }
            catch(Exception e){
                Log.d("error",e.getMessage());
            }   
    }
4

6 回答 6

0

将此代码用于 for 循环。

if(getListView().isItemChecked(position))
{                                     
   for(int i = 0 ; i < total ; i++)
  {                                        
    System.out.println("i="+i); 
     if(first+i ==   position)
     { 
         getListView().getChildAt(i).setBackgroundColor(Color.GREEN); 
          System.out.println("l1="+getListView());
                                     //      l.getItemAtPosition(i); 
                                    //      System.out.println("l position"+l);
      }
      else
      {  
       getListView().getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
       System.out.println("l2="+getListView
());
  } 
} 

我认为您打算只更改单击项目的免费颜色。为此,您无需添加 for 循环。如果您的意图与我所说的相同,则仅在 for 循环的位置使用此代码。

getListView().getChildAt(position).setBackgroundColor(Color.GREEN); 
于 2012-01-27T09:41:51.150 回答
0

还有其他方法可以做。

首先,您需要添加一个选择器 xml,例如 listviewitem_bg.xml

<item android:drawable="@drawable/listview_normal" android:state_enabled="true" android:state_pressed="false"/>
<item android:drawable="@drawable/listview_press" android:state_enabled="true" android:state_pressed="true"/>

然后将其设置为列表视图单元格的背景。

于 2012-01-27T09:46:30.273 回答
0

你可以这样尝试:

public class CustomAdapter extends ArrayAdapter<String> {

    String[] array;
    Context mContext;
    LayoutInflater mInflater;
    int[] itemStates;

    public CustomAdapter(Context context, int textViewResourceId,
            String[] objects)
    {
        super(context, textViewResourceId, objects);
        array=objects;
        mContext=context;
        mInflater = LayoutInflater.from(context);
        //save all buttons state as 0(not clicked) initially
        itemStates=new int[objects.length];
        for(int i=0;i<objects.length;i++)
        {
             itemStates[i]=0;
        }  
    }

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

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

                 holder.text=(TextView)convertView.findViewById(R.id.text);
                 holder.layout=(LinearLayout)convertView.findViewById(R.id.linear_layout); // outer most linear layout iin custom_listitem xml
                 convertView.setTag(holder);
        }
        else
                holder=(ViewHolder)convertView.getTag();

        holder.text.setText(array[position]);

        if(itemStates[position]==0)
        {
            holder.layout.setBackgroundResource(R.drawable.red_gradient);  // item is not clicked/selected yet  
        }
        else
        {            
            holder.button.setBackgroundResource(R.drawable.green_gradient);  // item is clicked/selected so change its color        
        }       

        final int pos=position;
        holder.layout.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                itemStates[pos]=1;               
                holder.button.setBackgroundResource(R.drawable.green_gradient);                
            }
        });

        return convertView;
    }

    static class ViewHolder
    {
        TextView text;
        LinearLayout layout;
    }
}

这会给你这样的行为:

  • 最初,所有项目都是红色的。
  • 如果你点击第二个项目,那么它会变成绿色(根据我的代码)。
  • 现在,当您滚动并单击任何其他项目时,它们将继续从红色变为绿色。
  • 但是如果你点击“绿色”颜色的项目,它会再次变成红色,就像它没有被选中一样!
于 2012-01-27T10:55:46.387 回答
0

你可以做这样的事情

@ 在创建之外

View prevView;

然后在 oncreate

lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int arg2,
                    long arg3) {
                if (prevView != null) {
                    prevView.setBackgroundColor(Color.TRANSPARENT);
                }
                view.setBackgroundColor(Color.RED);
                prevView = view;
            }
        });
于 2012-01-27T14:59:37.280 回答
0

我正在回答我自己的问题。

这是完美的运行代码:-

getListView().setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position,
                        long id) {


            //   Toast.makeText(getApplicationContext(), "Item "+position+" is clicked",
                //            Toast.LENGTH_SHORT).show();

                        System.out.println("position="+position);
                        int first = getListView().getFirstVisiblePosition();
                        System.out.println("first="+first);
                        int last = getListView().getLastVisiblePosition();
                        System.out.println("last="+last);
                        int total = last - first;
                        System.out.println("total="+total);
                        if(getListView().isItemChecked(position)){
                            for(int i = first ; i <= last ; i++){
                                System.out.println("i="+i);
                                if(i == position){
                                    Log.w("TAG", "I am in If block");
                                    getListView().getChildAt(i-first).setBackgroundColor(Color.GREEN);
                                    System.out.println("l1="+getListView());
                            //      l.getItemAtPosition(i);
                            //      System.out.println("l position"+l);
                                }
                                else{

                                    getListView().getChildAt(i-first).setBackgroundColor(Color.TRANSPARENT);
                                    System.out.println("l2="+getListView());
                                }
                            }
                        }
                        else{
                            getListView().getChildAt(position).setBackgroundColor(Color.TRANSPARENT); 
                        } 
                    }

            });
于 2012-01-30T08:16:00.633 回答
0

我成功使用的更清洁(?)解决方案是创建一个实现 Checkable 的 LinearLayout 扩展小部件(或您用于 listitem 布局的任何根视图类型

public class CheckableLinearLayout extends LinearLayout implements Checkable {

boolean checked = false;

public CheckableLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CheckableLinearLayout(Context context) {
    super(context);
}

@Override
public void setChecked(boolean checked) {

    this.checked = checked;
    updateView();


}

/* (non-Javadoc)
 * @see android.widget.Checkable#isChecked()
 */
@Override
public boolean isChecked() {
    return this.checked;
}

/* (non-Javadoc)
 * @see android.widget.Checkable#toggle()
 */
@Override
public void toggle() {
    this.checked=!this.checked;
    updateView();

}

private void updateView() {
    if (this.checked) {
                            //Change to Whatever your checked color should be, maybe expose this as attribute so i't can be changed from xml attribute
             setBackgroundResource(R.color.default_background_color);
    } else {
        setBackgroundDrawable(null);
    }
    invalidate();
}

}

然后在您的项目布局 xml 中:

<my.app.widgets.CheckableLinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

..any views for your listitem

<my.app.widgets.CheckableLinearLayout/> 
于 2013-01-25T09:11:53.143 回答