0

J'essaie de faire une application Android avec Volley qui peut tirer autant d'images que je veux, puis les mettre dans une vue de défilement horizontal. Jusqu'à présent, je les ai fait entrer mais peu importe ce que je fais, il ne me laisse pas le faire défiler.en essayant d'avoir un défilement horizontal voir des images dynamiques

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> 

fichier 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 
    } 

Répondre

0
<?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> 

utilisation xml et ajouter toutes les images à la disposition du conteneur qui a id "ContainerLayout" setContentView (main.xml) fonctionnera.

0

Vous ne travaillez manifestement pas avec le fichier de mise en page XML, puisque vous ne le chargez jamais. Vous fournissez votre propre LinearLayout au lieu de fournir la mise en page XML. C'est pourquoi vous ne pouvez pas faire défiler.

  1. Retirez le LinearLayout instancier et de faire une référence à partir R.id.myContainer
  2. Remplacer setContentView(containerLayout, lParams); avec setContentView(R.layout.your_xml);

Vous ne devez pas inclure les paramètres lors de l'utilisation d'une mise en page XML fichier.

La solution:

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 
    } 
} 
+0

LinearLayout myContainer = findViewById (R.id.myContainer); cette ligne me jette une erreur en disant ne peut pas convertir de View à LinearLayout – user2554063

+0

C'est correct, vous devriez le convertir en LinearLayout. J'ai oublié d'inclure cela. Je vais éditer la réponse. –

Questions connexes