0

2 つの textview と 1 つの imageview を持つアイテムのリストがあります。ArrayAdapter でリストを膨らませます。クリック時にリスト アイテムの色を変更する以外はすべて正常に動作しています。リストビューには 22 個のアイテムがあります。画面に10個のアイテムを表示し、スクロールで他のアイテムを取得する主なリストビュー.今私の問題は、0〜9(最初の10個のアイテム)アイテムの間の単一のアイテムをクリックすると、クリックすると色が適切に変わりますが、スクロールしてクリックすると(最初の 10 個のアイテムの後) 9 より大きい位置を持つアイテムで、私のアクティビティがクラッシュします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

私は自分の質問に答えています。

これが完璧な実行コードです:-

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

次のように試すことができます:

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;
    }
}

これにより、次のような動作が得られます。

  • 最初はすべてのアイテムが赤色になります。
  • 2番目の項目をクリックすると、緑色に変わります(私のコードによると)。
  • スクロールして他のアイテムをクリックすると、赤から緑に変わり続けます。
  • しかし、「緑色」のアイテムをクリックすると、選択されていないかのように再び赤色に変わります!
于 2012-01-27T10:55:46.387 に答える
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

あなたはこのようなことをすることができます

@作成時の外側

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

私が成功裏に使用したよりクリーンな (?) ソリューションは、 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 に答える
0

他にも方法があります。

まず、listviewitem_bg.xml などのセレクター 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 に答える