代码之家  ›  专栏  ›  技术社区  ›  Jithin Varghese

如何使用json中的viewflipper为图像和文本制作幻灯片

  •  0
  • Jithin Varghese  · 技术社区  · 9 年前

    //主活动.xml

    <ViewFlipper 
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <RelativeLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="match_parent"
        android:layout_height="279dp"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:background="@android:color/background_light"
        android:gravity="center" >
    
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/imageView7"
            android:layout_marginLeft="14dp"
            android:layout_marginTop="14dp"
            android:text="Event Name" />
    
        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView3"
            android:layout_below="@+id/textView3"
            android:text="Venue, Date"
            android:textSize="12sp" />
    
        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView4"
            android:layout_below="@+id/textView4"
            android:text="Description"
            android:textSize="12sp" />
    
        <ImageView
            android:id="@+id/imageView7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView2"
            android:layout_marginTop="14dp"
            android:src="@drawable/demo" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dp"
            android:text="Event of the Week" />
    
    </RelativeLayout>
    </ViewFlipper>
    

    我想为上面的布局实现图像和文本的幻灯片放映。这些值来自我的服务器,使用json。我如何实现这一点。我被困在这里了。有人知道答案吗。

    任何机构都有参考链接。。

    1 回复  |  直到 9 年前
        1
  •  1
  •   Community Stefan Steinegger    7 年前

    以下是您要遵循的基本流程。

    1.下载JSON数据 :您需要使用后台线程和HTTP客户端来执行此操作。可能 AsyncTask DefaultHttpClient

    2.解析JSON数据: 下载JSON后,需要创建 JSONObjects 并将它们解析为数据模型类以存储数据。您的模型类可能如下所示:

    public class SlideshowImage {
        public String text;
        public String imageUrl;
    } 
    

    理想情况下,你最终会得到 List 来自JSON的幻灯片图像。

    3.必要时存储数据(可选): 如果您想将这些模型类存储在数据库中,也可以这样做。但是,如果您打算每次都实时加载此信息,则可能不需要它。

    4使用“幻灯片图像”列表填充视图: 最后,您可以访问SlideshowImage类,将数据加载到TextView和ImageView中。

    SlideshowImage model = slideshowImageList.get(0);
    textView1.setText(model.text);
    

    您还需要下载imageUrls以获取 Bitmap 这样您就可以设置ImageViews。你应该参考 this 这个问题需要帮助。