【问题标题】:trying to have a horizontal scroll view dynamic pictures试图有一个水平滚动查看动态图片
【发布时间】:2013-07-03 16:21:02
【问题描述】:

我正在尝试使用 Volley 制作一个 android 应用程序,它可以拉入任意数量的图像,然后将它们设置在水平滚动视图中。到目前为止,我已经让他们进来了,但无论我做什么,它都不会让我让它滚动。

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"
    android:orientation="horizontal"
    android:id="@+id/containerLayout" >

    <HorizontalScrollView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/textView1"
        android:id="@+id/scrollLayout">

       <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >


            <ImageView
                android:id="@+id/niv_large"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="center" />
        </LinearLayout>
    </HorizontalScrollView> 

</RelativeLayout>

Java 文件

public class AppActivity extends Activity
{

    private RequestQueue mRequestQueue;
    private ImageLoader imageLoader;
    Button button;



    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate( savedInstanceState );
        mRequestQueue = Volley.newRequestQueue(this);
        imageLoader =  new ImageLoader(mRequestQueue, new DiskBitmapCache(getCacheDir(), 0));
//        final ImageView networkimage = ImageView.class.cast(findViewById(R.id.niv_large));

//        imageLoader.get("http://files.vividscreen.com/soft/b0d52a794a2f44f1a208d1fdf6088125/The-Dark-Knight-Batman-768x1280th.jpg", ImageLoader.getImageListener(networkimage, R.drawable.ic_launcher, R.drawable.ic_launcher));

        ArrayList<String> imageUrls = new ArrayList<String>();

        imageUrls.add( "http://images.cpcache.com/merchandise/514_400x400_Peel.jpg?region=name:FrontCenter,id:28298128,w:16" );
        imageUrls.add( "http://images.cpcache.com/merchandise/514_400x400_NoPeel.jpg?region=name:FrontCenter,id:25042524,w:16" );
        imageUrls.add( "http://michaelkonik.com/wp-content/uploads/2006/06/2014-World-Cup-Logo-400x400.jpg" );
        //imageUrls.add( "http://hdwallsize.com/wp-content/uploads/2013/03/Barcelona-Wallpaper-HD-Lionel-Messi.jpg" );
        //add other image urls as above until done

        LinearLayout containerLayout = new LinearLayout(this);

        LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);



        setContentView( containerLayout, lParams);         

        for( int i = 0; i < imageUrls.size(); i++ )
        {
            ImageView image = new ImageView( this );

            //Log.e( "Checking image stuffs for null",  "Image= " + image + " url=" + imageUrls.get(i) + "imageLoader=" + ImageLoader.getImageListener( image, R.drawable.ic_launcher, R.drawable.ic_launcher) );
            imageLoader.get( imageUrls.get( i ), ImageLoader.getImageListener( image, R.drawable.ic_launcher, R.drawable.ic_launcher) );
            containerLayout.addView( image  );
        }        
                                                                              // icon loading, icon error
    }

【问题讨论】:

    标签: android image dynamic horizontalscrollview android-volley


    【解决方案1】:
    <?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"
        android:orientation="horizontal"
        android:id="@+id/containerLayout" >
    
        <HorizontalScrollView 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/textView1"
            android:id="@+id/scrollLayout">
           <LinearLayout 
                android:id="@+id/containerlLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal" />
    
        </HorizontalScrollView> 
    
    </RelativeLayout>
    

    使用 xml 并将所有图像添加到 id 为“containerLayout”的容器布局中 setContentView(main.xml) 将起作用。

    【讨论】:

      【解决方案2】:

      您显然没有使用 XML 布局文件,因为您从未加载过它。您提供自己的 LinearLayout 而不是提供 XML 布局。这就是你不能滚动的原因。

      1. 删除您实例化的 LinearLayout 并从中引用 R.id.myContainer
      2. setContentView( containerLayout, lParams); 替换为setContentView( R.layout.your_xml);

      使用 XML 布局文件时不必包含参数。

      解决办法:

      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"
          android:orientation="horizontal"
          android:id="@+id/containerLayout" >
      
          <HorizontalScrollView 
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_below="@+id/textView1"
              android:id="@+id/scrollLayout">
      
             <LinearLayout 
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="horizontal" 
                  android:id="@+id/myContainer" />
      
          </HorizontalScrollView> 
      
      </RelativeLayout>
      

      Java

      public class AppActivity extends Activity
      {
      
          private RequestQueue mRequestQueue;
          private ImageLoader imageLoader;
          Button button;
      
      
      
          @Override
          protected void onCreate(Bundle savedInstanceState)
          {
              // TODO Auto-generated method stub
              super.onCreate( savedInstanceState );
              mRequestQueue = Volley.newRequestQueue(this);
              imageLoader =  new ImageLoader(mRequestQueue, new DiskBitmapCache(getCacheDir(), 0));
      //        final ImageView networkimage = ImageView.class.cast(findViewById(R.id.niv_large));
      
      //        imageLoader.get("http://files.vividscreen.com/soft/b0d52a794a2f44f1a208d1fdf6088125/The-Dark-Knight-Batman-768x1280th.jpg", ImageLoader.getImageListener(networkimage, R.drawable.ic_launcher, R.drawable.ic_launcher));
      
              ArrayList<String> imageUrls = new ArrayList<String>();
      
              imageUrls.add( "http://images.cpcache.com/merchandise/514_400x400_Peel.jpg?region=name:FrontCenter,id:28298128,w:16" );
              imageUrls.add( "http://images.cpcache.com/merchandise/514_400x400_NoPeel.jpg?region=name:FrontCenter,id:25042524,w:16" );
              imageUrls.add( "http://michaelkonik.com/wp-content/uploads/2006/06/2014-World-Cup-Logo-400x400.jpg" );
              //imageUrls.add( "http://hdwallsize.com/wp-content/uploads/2013/03/Barcelona-Wallpaper-HD-Lionel-Messi.jpg" );
              //add other image urls as above until done
      
              // Get a reference to the LinearLayout inside the HorizontalScrollView
              LinearLayout container = (LinearLayout) findViewById(R.id.myContainer);
      
              // Replaced container with R.layout.your_xml where 'your_xml' refers to your XML file. Parameters are also not needed since they are as attributes in the XML tag
              setContentView( R.layout.your_xml);         
      
              for( int i = 0; i < imageUrls.size(); i++ )
              {
                  ImageView image = new ImageView( this );
      
                  //Log.e( "Checking image stuffs for null",  "Image= " + image + " url=" + imageUrls.get(i) + "imageLoader=" + ImageLoader.getImageListener( image, R.drawable.ic_launcher, R.drawable.ic_launcher) );
                  imageLoader.get( imageUrls.get( i ), ImageLoader.getImageListener( image, R.drawable.ic_launcher, R.drawable.ic_launcher) );
      
                  // Add the view to myContainer using the referece
                  myContainer.addView( image );
      
                  //containerLayout.addView( image  );
              }        
      
              // icon loading, icon error
          }
      }
      

      【讨论】:

      • LinearLayout myContainer = findViewById(R.id.myContainer);这行给我一个错误,说不能从 View 转换为 LinearLayout
      • 没错,你应该把它转换成LinearLayout。我忘了包括那个。我会编辑答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多