为具有不同行布局的listView创建自定义适配器

时间:2014-09-16 11:48:51

标签: android android-listview custom-adapter

我想在ListView中添加不同的行。在第一行我想显示一个textView。在第二个两个imageViews和第三个单一的textView中。然后是带有图标的文本列表。我知道这可以使用自定义CustomAdapter。但不了解如何继续。
我的CustomAdapter:

       public class CustomList extends ArrayAdapter<String>
 {
 private final Activity context;
 private final String[] web;
 private final Integer[] imageId;

 public CustomList(Activity context,
        String[] web, Integer[] imageId) {
    super(context, R.layout.drawer_list_item, web);
    this.context = context;
    this.web = web;
    this.imageId = imageId;
   }
    @Override
    public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();

    View rowView= inflater.inflate(R.layout.drawer_list_item, null, true);
    TextView txtTitle = (TextView) rowView.findViewById(R.id.text1);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.drawer_imgIcom);
    txtTitle.setText(web[position]);
    imageView.setImageResource(imageId[position]);

    return rowView;
  }

3 个答案:

答案 0 :(得分:0)

在drawer_list_item xml文件中,根据需要创建所需的所有元素,文本,图像等。然后在膨胀之后的getView()中,简单地隐藏并根据位置显示这些元素。

请记住隐藏和显示,因为Android会重新使用这些视图。

此外,您应该考虑使用viewHolder,因为这样可以提高性能。

答案 1 :(得分:0)

在getview(),

中尝试此代码
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();

View rowView= inflater.inflate(R.layout.drawer_list_item, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.text1);
ImageView imageView = (ImageView) rowView.findViewById(R.id.drawer_imgIcom);
ImageView imageView1 = (ImageView) rowView.findViewById(R.id.drawer_imgIcom1);

 if(position == 0)
 {
   txtTitle.setText(web[position]);
   imageView.setvisibility(View.GONE);
   imageView1..setvisibility(View.GONE);
 }
else if(Position == 1){
txtTitle.setText(web[position]);
imageView.setvisibility(View.Visible);
imageView1..setvisibility(View.Visible);
imageView.setImageResource(imageId[position]);
imageView1.setImageResource(imageId1[position]); 

}else
{
 txtTitle.setText(web[position]);
 imageView.setvisibility(View.GONE);
 imageView1..setvisibility(View.GONE);
 }

 return rowView;}

然后在xml布局中使用相应的视图。

答案 2 :(得分:0)

首先,您需要自定义视图(包含您出于此目的所需的视图)

示例:

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

    <ImageView
        android:id="@+id/photo"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:background="@android:color/holo_blue_light"
        android:contentDescription="@string/cd_pr_photo"
        android:scaleType="centerCrop" />

    <!--Invisible divider so we can put our views just in the
    right place :) -->
    <View
        android:id="@+id/divider"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@id/photo" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/divider"
        android:layout_margin="10dp"
        android:text="@string/sample_title"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:textStyle="bold|italic" />

    <ImageButton
        android:id="@+id/share"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/divider"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_margin="10dp"
        android:background="@android:drawable/ic_menu_share"
        android:contentDescription="@string/cd_the_sharebutton" />

</RelativeLayout>

确定布局后,可以在CustomAdapterClass中扩展ArrayAdapter。

示例:

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

        //In case we don't have an inflated view, inflate.
        if (v == null) {
            v = View.inflate(getContext(), R.layout.custom_puertorico_layout, null);
        }

        //The domain object with data.
        PuertoRico p = getItem(position);


        //Photo of Puerto Rico
        final ImageView photo = (ImageView) v.findViewById(R.id.photo);
        photo.setImageDrawable(p.getPicture());

        //Given Title
        TextView title = (TextView) v.findViewById(R.id.title);
        title.setText(p.getTitle());

        //Extra stuff...
        ImageButton share = (ImageButton) v.findViewById(R.id.share);



        return v;
    }

这个简单的示例和完整的代码可以在https://github.com/JRSosa/PRPics

找到

希望它有所帮助。