如何在Android中创建包含固定页眉和页脚之间图像的动态ListView?

时间:2013-12-30 23:59:42

标签: android android-layout android-listview layout-inflater

我是Android开发的初学者,但不是软件开发。我一直在寻找有关如何使用图像创建动态ListView的教程,并且遇到了一些麻烦。

我最关注的教程是此链接上的#2(Custom ArrayAdapter示例):http://www.mkyong.com/android/android-listview-example/

如果我从布局xml文件中删除页眉和页脚,这可以正常工作,但我真的需要包含它们。下面我将从我的Activity java文件,我的自定义数组适配器java文件,我的布局xml文件以及输出中发布我的源代码。我认为可能有些东西我不知道或者没有理解布局效果如何。

感谢您的时间。

MainActivity.java

public class MainActivity extends ListActivity {

static final String[] MAIN_MENU = 
        new String[] { "ICD-10", "EDUCATION", "NEWS", "SERVICES", "CONTACT" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setListAdapter(new ImageAdapter(this, MAIN_MENU));

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    //get selected items
    String selectedValue = (String) getListAdapter().getItem(position);

    // to contact
    if(selectedValue.equals(MAIN_MENU[4])) {
        contactPressed(v);
    }
    // to icd-10
    else if(selectedValue.equals(MAIN_MENU[0])) {
        startActivity(new Intent(MainActivity.this, ICDActivity.class));
    }
    // to services
    else if(selectedValue.equals(MAIN_MENU[3])) {
        startActivity(new Intent(MainActivity.this, ServicesActivity.class));
    }

}

public void  contactPressed(View v) {       
    startActivity(new Intent(MainActivity.this, ContactActivity.class));
    overridePendingTransition(R.anim.slide_in_up, R.anim.stay);
}

}

ImageAdapter.java

  public class ImageAdapter extends ArrayAdapter<String>
{
private final Context context;
private final String[] values;

public ImageAdapter(Context context, String[] values) {
    super(context, R.layout.activity_main, values);
    this.context = context;
    this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.activity_main, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.lvText);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.lvImage);
    textView.setText(values[position]);

    // Change icon based on name
    String s = values[position];

    if (s.equals("ICD-10"))
    {
        imageView.setImageResource(R.drawable.icon_icd10);
    }
    else if(s.equals("EDUCATION"))
    { 
        imageView.setImageResource(R.drawable.icon_education);
    }
    else if(s.equals("NEWS"))
    {
        imageView.setImageResource(R.drawable.icon_news);
    }
    else if(s.equals("SERVICES"))
    {
        imageView.setImageResource(R.drawable.icon_services);
    }
    else if(s.equals("CONTACT"))
    {
        imageView.setImageResource(R.drawable.icon_contact);
    }

    return rowView;
}
 }

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<ImageView
    android:id="@+id/custom_background"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="fitXY"
    android:src="@drawable/gray">
</ImageView>

<RelativeLayout
    android:id="@+id/headerLayout"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:layout_alignParentTop="true" >

    <ImageView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:scaleType="fitXY"
        android:src="@drawable/logo_header" />
</RelativeLayout>

<!-- Footer aligned to bottom -->
<RelativeLayout 
    android:id="@+id/footerLayout"
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="30dp" >

    <ImageView
        android:id="@+id/footer"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:scaleType="center"
        android:src="@drawable/footer" />

    <TextView
        android:id="@+id/footerText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="14dp"
        android:onClick="contactPressed"
        android:layout_marginBottom="5dp"
        android:text="@string/footerText" />

</RelativeLayout>


<ScrollView
    android:id="@+id/scrollableContents"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/footerLayout"
    android:layout_below="@id/headerLayout" >


    <LinearLayout
        android:id="@+id/linearList"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/footerLayout"
        android:layout_below="@id/headerLayout"
        android:padding="5dp"
        tools:context=".MainActivity" >

        <!-- Making a dynamic ListView with images with the two attributes below -->

        <ImageView
            android:id="@+id/lvImage"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:src="@drawable/icon_icd10" >
        </ImageView>

        <TextView
            android:id="@+id/lvText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@+id/lvText"
            android:textSize="25sp" >
        </TextView>

    </LinearLayout>
</ScrollView>

</RelativeLayout>

我得到的输出是: enter image description here

如果我在xml中注释掉页眉和页脚代码,我会得到: enter image description here

目标是让它看起来与此类似,但每行右侧都有图片: enter image description here

感谢您的时间。

2 个答案:

答案 0 :(得分:0)

首先看起来像你有

android:layout_above="@id/footerLayout"
android:layout_below="@id/headerLayout"

@+id/scrollableContents上,这很好。但是你也嵌套在它里面的视图@+id/linearList,这是不对的。可能存在其他布局问题,但很难说。通常,这种布局对于您要实现的目标看起来非常复杂。

但是我不确定“动态ListView”是什么意思,看起来你正在尝试使用ScrollView重新创建listview的功能。我不认为你应该采取这种方法。 ListView非常灵活,您可以为项目使用自定义布局。

答案 1 :(得分:0)

所以我在搜索另一个教程时找到了解决问题的方法。我最终使用了本教程:http://www.learn2crack.com/2013/10/android-custom-listview-images-text-example.html

我的问题是当我应该在不同的layout.xml文件中创建TextView和ImageView时,我试图用同一个layout.xml文件中的TextView和ImageView替换我的ListView。下面是我正在寻找的最终代码和正确的输出。谢谢!

list_single.xml (下方)

<?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/img"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="9dp"
        android:layout_alignParentRight="true" />

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginTop="17dp"
        android:layout_marginLeft="5dp"
        android:textSize="23sp" />

</RelativeLayout>

activity_main.xml (下方)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/custom_background"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY"
        android:src="@drawable/gray">
    </ImageView>

    <ImageView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:scaleType="fitXY"
        android:src="@drawable/logo_header" />

    <!-- Footer aligned to bottom -->
    <ImageView
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_alignParentBottom="true"
        android:scaleType="center"
        android:src="@drawable/footer" />

    <TextView
        android:id="@+id/footerText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="14dp"
        android:onClick="contactPressed"
        android:text="@string/footerText" />

    <ListView
        android:id="@+id/mainMenuList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/footer"
        android:layout_below="@id/header"
        android:divider="#000000"
        android:dividerHeight="0.1dp" />

</RelativeLayout>

ImageAdapter.java (下方)

public class ImageAdapter extends ArrayAdapter<String>
{
    private final Context context;
    private final String[] values;

    public ImageAdapter(Context context, String[] values) {
        super(context, R.layout.list_single, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.list_single, null, true);
        TextView textView = (TextView) rowView.findViewById(R.id.txt);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
        textView.setText(values[position]);

        // Change icon based on name
        String s = values[position];

        if (s.equals("ICD-10"))
        {
            imageView.setImageResource(R.drawable.icon_icd10);
        }
        else if(s.equals("EDUCATION"))
        { 
            imageView.setImageResource(R.drawable.icon_education);
        }
        else if(s.equals("NEWS"))
        {
            imageView.setImageResource(R.drawable.icon_news);
        }
        else if(s.equals("SERVICES"))
        {
            imageView.setImageResource(R.drawable.icon_services);
        }
        else if(s.equals("CONTACT"))
        {
            imageView.setImageResource(R.drawable.icon_contact);
        }

        return rowView;
    }
}

MainActivity.java (下方)

public class MainActivity extends Activity {

    ListView list;
    static final String[] MAIN_MENU = 
            new String[] { "ICD-10", "EDUCATION", "NEWS", "SERVICES", "CONTACT" };

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

        list = (ListView)findViewById(R.id.mainMenuList);
        list.setAdapter(new ImageAdapter(MainActivity.this, MAIN_MENU));

        // register onClickListener to handle click events on each item
        list.setOnItemClickListener(new OnItemClickListener() {
            // argument position gives the index of item which is clicked
            public void onItemClick(AdapterView<?> arg0, View v, int position,
                    long arg3) {

                String selectedValue = MAIN_MENU[position];

                // to contact
                if(selectedValue.equals(MAIN_MENU[4])) {
                    contactPressed(v);
                }
                // to icd-10
                else if(selectedValue.equals(MAIN_MENU[0])) {
                    startActivity(new Intent(MainActivity.this, ICDActivity.class));
                }
                // to services
                else if(selectedValue.equals(MAIN_MENU[3])) {
                    startActivity(new Intent(MainActivity.this, ServicesActivity.class));
                }
            }
        });

    }

    public void  contactPressed(View v) {       
        startActivity(new Intent(MainActivity.this, ContactActivity.class));
        overridePendingTransition(R.anim.slide_in_up, R.anim.stay);
    }

}

最终输出(下) enter image description here