【问题标题】:How to add ViewPager that contain images into listview?如何将包含图像的 ViewPager 添加到列表视图中?
【发布时间】:2014-01-09 01:36:08
【问题描述】:

开发人员好日子,我已经使用基本适配器实现了 listview,现在我想在 listview 行项目中添加一个视图分页器,目前位于第 2 行。但我遇到了错误。我认为,这是关于列表视图上的 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。

【问题讨论】:

  • 有人可以帮忙吗?
  • 查看此链接。可能会有所帮助stackoverflow.com/questions/14920459/…
  • 始终,在发布堆栈跟踪时,突出显示引发异常的行(适配器的 getView() 方法中的行 402)。

标签: android eclipse android-listview android-viewpager


【解决方案1】:

你可以做一个解决方法。

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

在其中添加一个带有 Horizo​​ntallScroll 的视图。可以通过对应View的ID触发的点击事件。

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

请注意,您必须将孩子的宽度(在您的案例中为图片)设置为 match_parent。使用 Layout(具有 match_parent)并在此 Layout 中放置带有 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 中的 Layout 创建一个 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>

现在在您的代码中,您可以动态创建 AdapterView(就像您拥有的那样)并将其添加到您的 Horizo​​ntalScrollView 的 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);
        }

在此您使用其中的线性布局初始化您的滚动视图。特别是您在线性布局中添加了 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;
}

}

【讨论】:

  • 我尝试实现水平滚动视图,但我仍然对如何使用包含 listview 扩展基本适配器的布局来填充水平滚动视图的布局感到困惑。你能给我一个示例如何使用水平滚动视图实现上述视图吗?
  • 请查看我的编辑答案。随时问更多。
  • 我已经为水平滚动视图布局和包含图像视图的布局创建了 2 个 xml。现在我应该在我的 ListviewAdapter 中为这 2 个 xml 充气吗?还是我应该创建另一个扩展 AdapterView 的类?
  • 我无法用我的 ListViewAdapter 为这 2 个 xml 充气。因为 ListviewAdapter 使用另一种布局。我尝试创建一个名为 viewimagepage 的子类来扩展 AdapterView,但混淆了如何实现该适配器,您能帮帮我吗?
  • 好的,您的层次结构如下:ListView -> Horizo​​ntal -> CustomeImageView。请注意,您创建了您的 CustomeImageView,然后膨胀您的 Horizo​​ntalScrollView 并将 CustomeImageView 添加到您的 Horizo​​ntalScrollView,之后,您将膨胀的 Horizo​​ntalSV 添加到您的 ListAdapter。或者你还有其他问题吗?没错,Horizo​​ntalSV 需要一个适配器,CustomeView 也需要一个适配器。
猜你喜欢
  • 2016-05-15
  • 1970-01-01
  • 1970-01-01
  • 2017-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多