2014-11-04 60 views
0

我遇到了一些奇怪的問題與我的列表視圖。我使用listview創建了一個列表以及一個自定義適配器,以顯示具有文本字段和多個圖像的行。我相信我有那部分工作得很好。我還在列表視圖的底部放置了兩個按鈕,以便我可以選擇行並使用兩個按鈕修改它們。我通過簡單地改變背景顏色,然後設置每行的一部分的布爾標誌來實現突出顯示,以便我可以知道哪些行突出顯示。現在我遇到兩個問題。首先,如果我選擇一行然後滾動以使該行在屏幕之外,那麼該行變爲未突出顯示,這是不好的。如果用戶再次單擊它或發出命令,我只想讓一行變爲不突出顯示。第二個問題是,一旦適配器被更新或者一行移出視圖,如果你嘗試點擊一行,它將立即取消突出顯示;這隻發生一次。發生這種情況後,您可以點擊該行並保持突出顯示。我非常感謝這方面的幫助。 此致敬禮。在列表視圖中滾動時選擇消失,以及奇怪的行爲

HelmetList.java

public class HelmetList 
{ 
    public HelmetList (String name, String address, String img_hel, String img_rec, String img_bat, 
          String img_dsk, String img_str, Boolean selected) 
    { 
     super(); 
     this.name  = name; 
     this.address = address; 
     this.img_hel = img_hel; 
     this.img_rec = img_rec; 
     this.img_bat = img_bat; 
     this.img_dsk = img_dsk; 
     this.img_str = img_str; 
     this.selected = selected; 
    } 

    private String name; 
    private String address; 
    private String img_hel; 
    private String img_rec; 
    private String img_bat; 
    private String img_dsk; 
    private String img_str; 
    private Boolean selected; 

    public String getName() 
    { 
     return name; 
    } 

    public void setName (String s_name) 
    { 
     this.name = s_name; 
    } 

    public String getAddress() 
    { 
     return address; 
    } 

    public void setAddress (String s_address) 
    { 
     this.address = s_address; 
    } 

    public String getImgHel() 
    { 
     return img_hel; 
    } 

    public void setImgHel (String s_img_hel) 
    { 
     this.img_hel = s_img_hel; 
    } 

    public String getImgRec() 
    { 
     return img_rec; 
    } 

    public void setImgRec (String s_img_rec) 
    { 
     this.img_rec = s_img_rec; 
    } 

    public String getImgBat() 
    { 
     return img_bat; 
    } 

    public void setImgBat (String s_img_bat) 
    { 
     this.img_bat = s_img_bat; 
    } 

    public String getImgDsk() 
    { 
     return img_dsk; 
    } 

    public void setImgDsk (String s_img_dsk) 
    { 
     this.img_dsk = s_img_dsk; 
    } 

    public String getImgStr() 
    { 
     return img_str; 
    } 

    public void setImgStr (String s_img_str) 
    { 
     this.img_str = s_img_str; 
    } 

    public Boolean getSelected() 
    { 
     return selected; 
    } 

    public void setSelected (Boolean s_selected) 
    { 
     this.selected = s_selected; 
    } 
} 

HelmetListAdapter.java

public class HelmetListAdapter extends ArrayAdapter<HelmetList> 
{ 
    private int resource; 
    private LayoutInflater inflater; 
    private Context context; 

    public HelmetListAdapter (Context p_context, int p_resource, List<HelmetList> p_objects) 
    { 
     super (p_context, p_resource, p_objects); 
     resource = p_resource; 
     inflater = LayoutInflater.from (p_context); 
     context = p_context; 
    } 

    @Override 
    public View getView (int position, View convertView, ViewGroup parent) 
    { 
     convertView = (RelativeLayout) inflater.inflate(resource, null); 
     HelmetList Helmet = getItem (position); 

     TextView hname = (TextView) convertView.findViewById(R.id.h_name); 
     hname.setText(Helmet.getName()); 

     TextView haddress = (TextView) convertView.findViewById(R.id.h_address); 
     haddress.setText(Helmet.getAddress()); 

     ImageView himage = (ImageView) convertView.findViewById(R.id.h_image); 
     String uri = "drawable/" + Helmet.getImgHel(); 
     int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName()); 
     Drawable image = context.getResources().getDrawable(imageResource); 
     himage.setImageDrawable(image); 

     ImageView hrec = (ImageView) convertView.findViewById(R.id.h_rec); 
     uri = "drawable/" + Helmet.getImgRec(); 
     imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName()); 
     image = context.getResources().getDrawable(imageResource); 
     hrec.setImageDrawable(image); 

     ImageView hlbat = (ImageView) convertView.findViewById(R.id.h_lb); 
     uri = "drawable/" + Helmet.getImgBat(); 
     imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName()); 
     image = context.getResources().getDrawable(imageResource); 
     hlbat.setImageDrawable(image); 

     ImageView hldsk = (ImageView) convertView.findViewById(R.id.h_ld); 
     uri = "drawable/" + Helmet.getImgDsk(); 
     imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName()); 
     image = context.getResources().getDrawable(imageResource); 
     hldsk.setImageDrawable(image); 

     ImageView hstr = (ImageView) convertView.findViewById(R.id.h_str); 
     uri = "drawable/" + Helmet.getImgStr(); 
     imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName()); 
     image = context.getResources().getDrawable(imageResource); 
     hstr.setImageDrawable(image); 

     return convertView; 

    } 
} 

MainActivity.java

public class MainActivity extends Activity 
{ 
    private ListView lvhelmets; 
    private HelmetListAdapter adhelmets; 
    private Context ctx; 
    List<Integer> selected; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ctx = this; 

     List<HelmetList> helmetlist = new ArrayList<HelmetList>(); 
     helmetlist.add(new HelmetList("Bell", "11111", "helmetpic0", "rec", 
       "bat", "mm", "str", Boolean.FALSE)); 
     helmetlist.add(new HelmetList("Shoei", "33333", "helmetpic1", "rec", 
             "bat", "mm", "str", Boolean.FALSE)); 
     helmetlist.add(new HelmetList("Harley Davidson", "55555", "helmetpic2", "rec", 
             "bat", "mm", "str", Boolean.FALSE)); 
     helmetlist.add(new HelmetList("Joe Rocket", "77777", "helmetpic3", "rec", 
       "bat", "mm", "str", Boolean.FALSE)); 


     lvhelmets = (ListView) findViewById(R.id.Helmet_list); 
     lvhelmets.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE); 

     adhelmets = new HelmetListAdapter(ctx, R.layout.row_format, helmetlist); 
     lvhelmets.setAdapter (adhelmets); 

     Button price = (Button) findViewById(R.id.bPrice); 
     Button safety = (Button) findViewById(R.id.bSafety); 

     price.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View view) 
      { 
       lvhelmets.setAdapter(adhelmets); 
       int count = lvhelmets.getCount(); 
       for (int i = 0; i < count; i++) 
       { 
        HelmetList helmet = (HelmetList) lvhelmets.getItemAtPosition(i); 
        helmet.setSelected(Boolean.FALSE); 
       } 
       adhelmets.notifyDataSetChanged(); 
      } 
     }); 

     safety.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View view) 
      { 
       lvhelmets.setAdapter(adhelmets); 
       int count = lvhelmets.getCount(); 
       for (int i = 0; i < count; i++) 
       { 
        HelmetList helmet = (HelmetList) lvhelmets.getItemAtPosition(i); 
        helmet.setSelected(Boolean.FALSE); 
       } 
       adhelmets.notifyDataSetChanged(); 
      } 
     }); 



     lvhelmets.setOnItemClickListener(new OnItemClickListener() 
     { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
      { 
       HelmetList helmet = (HelmetList) parent.getItemAtPosition(position); 

       if (!helmet.getSelected()) 
       { 
        view.setBackgroundColor(Color.LTGRAY); 
        helmet.setSelected(Boolean.TRUE); 
       } 
       else 
       { 
        view.setBackgroundColor(Color.TRANSPARENT); 
        helmet.setSelected(Boolean.FALSE); 
       } 
      } 
     }); 

    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@+id/Helmet_list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:paddingTop="5dp" 
     android:choiceMode="multipleChoice" 
     android:layout_weight="1"> 
    </ListView> 

    <LinearLayout 
     android:id="@+id/btnHolderLL" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/bPrice" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:paddingRight="1dp" 
      android:paddingLeft="1dp" 
      android:textColor="#FFFFFF" 
      android:background="#222222" 
      android:text="Price" 
      android:clickable="true" /> 

     <Button 
      android:id="@+id/bSafety" 
      android:layout_width="1dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:paddingRight="1dp" 
      android:paddingLeft="1dp" 
      android:textColor="#FFFFFF" 
      android:background="#222222" 
      android:text="Safety" 
      android:clickable="true" /> 
    </LinearLayout> 

</LinearLayout> 

row_format.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:padding="5dip" > 

    <!-- ListRow Left side Thumbnail image --> 
    <LinearLayout android:id="@+id/thumbnail" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="3dip" 
     android:layout_alignParentLeft="true" 
     android:layout_marginRight="5dip"> 
     <ImageView 
      android:id="@+id/h_image" 
      android:layout_width="50dip" 
      android:layout_height="50dip" 
      android:layout_marginLeft="5dip"/> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/h_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/thumbnail" 
     android:layout_toRightOf="@+id/thumbnail" 
     android:textColor="#040404" 
     android:typeface="sans" 
     android:textSize="20dip" 
     android:layout_marginTop="5dip" 
     android:textStyle="bold"/> 

    <TextView 
     android:id="@+id/h_address" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/h_name" 
     android:textColor="#343434" 
     android:textSize="12dip" 
     android:layout_marginTop="1dip" 
     android:layout_toRightOf="@+id/thumbnail" /> 

    <ImageView 
     android:id="@+id/h_rec" 
     android:layout_width="35dp" 
     android:layout_height="35dp" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="5dp" 
     android:layout_centerVertical="true" /> 

    <ImageView 
     android:id="@+id/h_lb" 
     android:layout_width="35dp" 
     android:layout_height="35dp" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="45dp" 
     android:layout_centerVertical="true" /> 

    <ImageView 
     android:id="@+id/h_ld" 
     android:layout_width="35dp" 
     android:layout_height="35dp" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="85dp" 
     android:layout_centerVertical="true" /> 

    <ImageView 
     android:id="@+id/h_str" 
     android:layout_width="35dp" 
     android:layout_height="35dp" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="125dp" 
     android:layout_centerVertical="true" /> 
</RelativeLayout> 

回答

0

這是因爲認爲回收。

在方法getView()的適配器,添加以下代碼片段:

if (!helmet.getSelected()) { 
    convertView.setBackgroundColor(Color.LTGRAY); 
} else { 
    convertView.setBackgroundColor(Color.TRANSPARENT); 
} 

你可能要重寫的方式你的看法回收利用;你實施的那個根本沒有效果。

的第一步將是補充一點:

@Override 
public View getView (int position, View convertView, ViewGroup parent) { 
    if (convertView == null) { 
    convertView = inflater.inflate(resource, parent, false); 
    } 
    // continue the rest of the cell data filling 
} 
+0

感謝您的答覆。我對Android編程相當陌生。當你談論我實現視圖回收的方式時,你的意思是什麼?謝謝 – Willis 2014-11-04 17:38:50