2014-10-30 14 views
8

Tôi đã làm theo một hướng dẫn tốt, với một số thay đổi, để tạo một ListView tùy chỉnh cho phép tôi hiển thị nhiều hình ảnh cho mỗi hàng trong ListView của tôi. Những gì tôi muốn làm là làm nổi bật bằng cách chọn nhiều mục trong danh sách và sau đó thực hiện một số loại hành động cho tất cả các mục danh sách đó bằng cách chọn một tùy chọn trên Trình đơn tùy chọn của tôi. Tuy nhiên, vấn đề tôi dường như đang chạy vào là tôi không thể chọn nhiều mục, mặc dù tôi đã thêm android:choiceMode="multipleChoice" vào ListView trong tệp .xml. Tôi nhận ra rằng điều này có thể được thực hiện bằng cách sử dụng hộp kiểm hoặc nút radio, nhưng tôi muốn tránh điều đó. Tôi đã đính kèm mã nguồn của mình bên dưới. Bất kỳ trợ giúp sẽ được đánh giá cao. Cuối cùng, nhờ vào số http://custom-android-dn.blogspot.com/ để có hướng dẫn tuyệt vời. Cảm ơn.Chọn/đánh dấu nhiều mục trong chế độ xem danh sách với bộ điều hợp tùy chỉnh - Android

CustomlistviewActivity.java

package DucNguyen.example.customlistview; 

public class CustomlistviewActivity extends Activity { 

    private ListView listViewFootballLegend; 
    private Context ctx; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_customlistview);   
     ctx=this; 
     List<FootballLegend> legendList= new ArrayList<FootballLegend>(); 
     legendList.add(new FootballLegend("Pele","October 23, 1940 (age 72)","pele","brazil")); 
     legendList.add(new FootballLegend("Diego Maradona","October 30, 1960 (age 52)","maradona","argentina")); 
     legendList.add(new FootballLegend("Johan Cruyff","April 25, 1947 (age 65)","cruyff","netherlands")); 
     legendList.add(new FootballLegend("Franz Beckenbauer","September 11, 1945 (age 67)","beckenbauer","germany")); 
     legendList.add(new FootballLegend("Michel Platini","June 21, 1955 (age 57)","platini","france")); 
     legendList.add(new FootballLegend("Ronaldo De Lima","September 22, 1976 (age 36)","ronaldo","brazil")); 

     listViewFootballLegend = (ListView) findViewById(R.id.FootballLegend_list); 
     listViewFootballLegend.setAdapter(new FootballLegendListAdapter(ctx, R.layout.legend_row_item, legendList)); 

     // Click event for single list row 
     listViewFootballLegend.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       FootballLegend o = (FootballLegend) parent.getItemAtPosition(position); 
       Toast.makeText(CustomlistviewActivity.this, o.getName().toString(), Toast.LENGTH_SHORT).show(); 
      } 
     });  
    } 
} 

FootballLegendListAdapter.java

package DucNguyen.example.customlistview; 

public class FootballLegendListAdapter extends ArrayAdapter<FootballLegend> { 
    private int    resource; 
    private LayoutInflater inflater; 
    private Context   context; 
    public FootballLegendListAdapter (Context ctx, int resourceId, List<FootballLegend> objects) { 
     super(ctx, resourceId, objects); 
     resource = resourceId; 
     inflater = LayoutInflater.from(ctx); 
     context=ctx; 
    } 
    @Override 
    public View getView (int position, View convertView, ViewGroup parent) { 
     convertView = (RelativeLayout) inflater.inflate(resource, null); 
     FootballLegend Legend = getItem(position); 
       TextView legendName = (TextView) convertView.findViewById(R.id.legendName); 
     legendName.setText(Legend.getName()); 

     TextView legendBorn = (TextView) convertView.findViewById(R.id.legendBorn); 
     legendBorn.setText(Legend.getNick()); 

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

     ImageView NationImage = (ImageView) convertView.findViewById(R.id.Nation); 
     uri = "drawable/" + Legend.getNation(); 
     imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName()); 
     image = context.getResources().getDrawable(imageResource); 
     NationImage.setImageDrawable(image); 

     return convertView; 
    } 
} 

FootballLegend.java

package DucNguyen.example.customlistview; 
public class FootballLegend { 
    public FootballLegend(String name, String born, String image, String nation) { 
     super(); 
     this.name = name; 
     this.born = born; 
     this.image = image; 
     this.nation = nation; 
    } 
    private String name; 
    private String born; 
    private String image; 
    private String nation; 

    public String getName() { 
     return name; 
    } 
    public void setName(String nameText) { 
     name = nameText; 
    } 
    public String getNick() { 
     return born; 
    } 
    public void setNick(String born) { 
     this.born = born; 
    } 
    public String getImage() { 
     return image; 
    } 
    public void setImage(String image) { 
     this.image = image; 
    } 
     public String getNation() { 
     return nation; 
    } 
    public void setNation(String image) { 
     this.image = nation; 
    } 
} 

legend_row_item.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/legendImage" 
      android:layout_width="50dip" 
      android:layout_height="50dip" /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/legendName" 
     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="15dip" 
     android:textStyle="bold"/> 

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

    <ImageView 
     android:id="@+id/Nation" 
     android:layout_width="45dp" 
     android:layout_height="30dp" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="5dp" 
     android:layout_centerVertical="true" /> 

</RelativeLayout> 

activity_customlistview.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/FootballLegend_list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:paddingTop="5dp" 
     android:choiceMode="multipleChoice" > 
    </ListView> 

</LinearLayout> 

Trả lời

8
  1. Gốc View của từng hạng mục, bạn sẽ hiển thị trong danh sách phải thực hiện Checkable. Khi triển khai giao diện này cũng cập nhật trạng thái có thể kéo của Chế độ xem. Xem this answer để biết cách thực hiện điều đó.

  2. Đặt <selector> drawable làm nền cho gốc này View. Màu sắc/bản vẽ khác nhau cho trạng thái đã chọn và trạng thái bình thường.

  3. Đặt chế độ lựa chọn của ListView thành lựa chọn đơn hoặc đa lựa chọn.

  4. Trong bộ điều hợp danh sách, cung cấp chế độ xem mục với Chế độ xem dưới dạng được tạo dưới dạng bố cục gốc ở trên.

Bây giờ, ListView sẽ quản lý trạng thái đã chọn/không được kiểm tra trên chế độ xem mục. Ngoài ra, bạn có thể gọi getCheckedItemIds() hoặc getCheckedItemPositions() trên ListView để nhận các mục hiện được chọn.

+0

Nếu tôi thực hiện có thể kiểm tra cho danh sách của mình, danh sách sẽ không có hộp kiểm không? Đó là những gì tôi đang cố gắng tránh. Cảm ơn – Willis

+1

@Willis No. 'Checkable' là Giao diện Java. Bất cứ điều gì thực hiện nó, hứa hẹn sẽ có các phương thức 'setChecked (boolean)' và 'toggle()'. Hoàn toàn tùy thuộc vào bạn những gì bạn muốn hiển thị trong Chế độ xem. –

+0

Cảm ơn bạn. Tôi tin rằng đây là những gì tôi cần. – Willis

Các vấn đề liên quan