如何将包含图像的ViewPager添加到listview中?

时间:2014-01-09 01:36:09

标签: android eclipse android-listview android-viewpager

美好的一天开发人员,我已经使用基础适配器实现了listview,现在我想将一个视图寻呼机添加到listview行项目中,目前在第2行。但是我面临错误。我想,这是关于listview的MeasureSpec。

这是使用BaseAdapter的ListView适配器:

public class ListViewAdapter extends BaseAdapter {
    ...
    @Override
    public View getView(int pos, View convertView, ViewGroup parent) {
        View v = convertView;
        TextView txt_urutan;
        TextView txt_menu;
        TextView txt_price;
        ImageView img_menu;
        ImageView img_arrow;

        if(v==null){
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.list_makanan, parent, false);
        }
        img_menu = (ImageView)v.findViewById(R.id.picture_menu_on_listview);
        txt_urutan = (TextView)v.findViewById(R.id.urutan_makanan_ayam_listview);
        txt_menu = (TextView)v.findViewById(R.id.nama_makanan_ayam_listview);
        txt_price = (TextView)v.findViewById(R.id.harga_makanan_ayam_listview);
        img_arrow = (ImageView)v.findViewById(R.id.arrow_on_listview);

        txt_urutan.setText(urutan_makanan_sambal[pos]);
        txt_menu.setText(jenis_menu_sambal[pos]);
        txt_price.setText(price_menu_sambal[pos]);
        img_menu.setImageResource(pic_menu.getResourceId(pos, -1));
        img_arrow.setImageResource(R.drawable.nav_icon);

        return v;
    }
}

编辑:

这是horizo​​ntalscrollview.java:

public class HorizontalScrollViewAdapter extends BaseAdapter{
LayoutInflater inflater;
    Context context;

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ImageView img;
        if(v==null){
            inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.horizontal_scroll_view, parent, false);
        }
        img =  (ImageView)v.findViewById(R.id.image_view1);
        return v;
    }
}

这是siteContentView类:

public class SiteContentPreview{
    public View getView(Context context, View v, SiteContentData siteContent){
        View result = v;
        if(result!=null){
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            result = inflater.inflate(R.layout.custom_image, null);
        }
        TextView siteNumber = (TextView)result.findViewById(R.id.custom_image_view_text);
        ImageView siteImage = (ImageView)result.findViewById(R.id.custom_image_view);
        if(siteContent != null){
            siteNumber.setText(siteContent.getSiteNumber());
            siteImage.setImageResource(Integer.parseInt(siteContent.getSiteContentFrame()));
        }
        return result;
    }
}

这是siteContentData类:

public class SiteContentData{
    String holdString;
    String holdImagePath;

    public getSiteNumber(){
        return holdString;
    }

    public getSiteContentFrame(){
        return holdImagePath;
    }
}

这是horizo​​ntal_scroll_view.xml:

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

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="50dp" >

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

</LinearLayout>

这是scroll_view.xml(用于保存要放置在水平滚动视图中的图像):

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

<ImageView android:id="@+id/image_view1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/sambal_bajak_laut"/>

<ImageView android:id="@+id/image_view2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/sambal_bawang"/>
</LinearLayout>

任何帮助或想法都将非常感激。环境:Eclipse,ADT,Genymotion。

1 个答案:

答案 0 :(得分:1)

你可以做一个解决方法。

您只需添加一个由BaseAdapter扩展的视图,而不是添加ViewPager。

在那里你添加一个View with Horizo​​ntallScroll。您可以使用相应视图的ID触发Click事件。

通过这个例子,你可以看到Horizo​​ntall如何实现:http://examples.javacodegeeks.com/android/core/ui/horizontalscrollview/android-horizontalscrollview-example/

请注意,您必须将您的孩子宽度(在您的案例中为图片)设置为match_parent。使用布局(具有match_parent)并在此布局中使用wrap_content放置图片,无需拉伸图片。

编辑1:

首先创建Horizo​​ntalScrollView布局,如下所示:

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

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="50dp" >

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

现在,您要为要放置在LinearLayout中的布局创建AdapterView

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/yourimage" />

</LinearLayout>

现在,在您的代码中,您可以从Horizo​​ntalScrollView动态创建AdapterView(就像您一样)到Linearlayout。

编辑2

public class HorizontalScrollViewAdapter extends BaseAdapter {
        ...
        @Override
        public View getView(int pos, View convertView, ViewGroup parent) {
            View v = convertView;
            ImageView img_menu;
            if(v==null){
            inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.horiscvi, parent, false);
            }
            // at this you must add your CustomeViewLayout
            return v;
        }
    }

适用于CustomeView的适配器

public class CustomeViewAdapter extends BaseAdapter {
    ...
    @Override
    public View getView(int pos, View convertView, ViewGroup parent) {
        View v = convertView;
        ImageView img_menu;
        if(v==null){
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.list_makanan, parent, false);
        }
        img_menu = (ImageView)v.findViewById(R.id.picture_menu_on_listview);
        v.addView(img_menu);
        return v;
    }
}

编辑3

好的,现在在您的AdapterView中,您有一个类似于以下内容的部分:

ArrayList<SiteContentData> siteContentData = new ArrayList<SiteContentData>();
        siteContentData.add(new SiteContentData("one", "yourpicone"));
        siteContentData.add(new SiteContentData("two", "yourpictwo"));
        siteContentData.add(new SiteContentData("three", "yourpicthree"));
        siteContentData.add(new SiteContentData("four", "yourpicfour"));

        horizontalScrollView = ((HorizontalScrollView) findViewById(R.id.horizontalScrollView1));
        sitePreview = ((LinearLayout) findViewById(R.id.hori_linear));

        for (SiteContentData siteContData : siteContentData)
        {
            View convertView;
            LayoutInflater inflaterCustomeShow =
                    (LayoutInflater) getApplicationContext().getSystemService(
                            Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflaterCustomeShow.inflate(R.layout.customeimage, null);
            View sitePreviewView =
                    SiteContentPreview.getView(getApplicationContext(), convertView, siteContData);

            sitePreview.addView(sitePreviewView);
        }

在此处,您可以使用此内部的linearlayout初始化您的滚动视图。特别是在linearlayout中添加CustomeView。 SiteContentData只是一个包含两个字符串的pojo,第一个是示例文本,第二个是项目中图像的路径。

SiteContentPreview看起来像这样:

public class SiteContentPreview
{
    public static View getView(Context context, View convertView, SiteContentData siteContent)
    {
        View result = convertView;
        if (result == null)
        {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            result = inflater.inflate(R.layout.customeimage, null);
        }

        TextView siteNumber = ((TextView) result.findViewById(R.id.custome_imageview_text));
        ImageButton siteImage = ((ImageButton) result.findViewById(R.id.custome_image_view));
        if (siteContent != null)
        {
            siteNumber.setText(siteContent.getSiteNumber());
            siteImage.setImageResource(Integer.parseInt(siteContent.getSiteContentFrame()));
        }

        return result;
    }
}

只是简单的分配。

这就是全部。

您正确的Horizo​​ntalScrollView布局,这里是CustomeViewImage的布局:

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

    <ImageView
        android:id="@+id/custome_image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/custome_imageview_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

这就是全部。请享用。希望你达到你想要的效果。

public class SiteContentData
{
private String siteNumber;
private String siteContentFrame;

public SiteContentData(String siteNumber, String siteContentFrame)
{
this.siteNumber = siteNumber;
this.siteContentFrame = siteContentFrame;
}

public String getSiteNumber()
{
return siteNumber;
}

public String getSiteContentFrame()
{
return siteContentFrame;
}

}