被困在水平图像滚动中

时间:2016-09-02 20:09:10

标签: android image android-library horizontal-scrolling horizontalscrollview

我是初级Android开发人员并制作一个Android应用程序,用户可以在其中销售和购买物品。

我非常严重地请求帮助。

Here is the UI i am trying to make

我在这里制作水平图像滚动条。我已尝试使用线性布局中的ImageViews,回收器视图适配器(使用水平布局管理器),但它并没有解决我的问题。 基本上,此图像滚动视图中的图像来自不同的活动,也来自此(添加产品活动),用户可以从相机或图库中添加图像,也可以删除它... 所以我的问题是,有什么最简单的方法可以处理图像水平滚动吗?是否有任何图书馆可以使用,因此我可以在我的水平图像滚动视图中轻松管理图像?或者你喜欢的任何方法请跟我分享........... 致谢

1 个答案:

答案 0 :(得分:0)

我认为你尝试了错误的东西,你应该尝试使用android ViewPager 对于您的问题,您应该查看this example ,以下是This Link

中查看页面的示例代码

<强> main.xml中

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

      <HorizontalScrollView android:layout_width="fill_parent"  
        android:layout_height="wrap_content">  

        <LinearLayout android:id="@+id/_linearLayout"  
          android:layout_width="fill_parent"  
          android:layout_height="wrap_content"  
          android:orientation="horizontal">  

        </LinearLayout>  

      </HorizontalScrollView>  

      <android.support.v4.view.ViewPager android:id="@+id/_viewPager"  
          android:layout_width="fill_parent"  
          android:layout_height="fill_parent"  
          android:background="#000000"  
          android:visibility="gone" />  

 </RelativeLayout>  

<强> cell.xml

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

   <ImageView android:id="@+id/_image"   
       android:layout_width="wrap_content"   
       android:layout_height="wrap_content"/>   

   <TextView android:id="@+id/_imageName"   
       android:layout_width="wrap_content"   
       android:layout_height="wrap_content"   
       android:layout_gravity="center_horizontal"/>   

  </LinearLayout>   

页面适配器CLass

import android.app.Activity;  
 import android.os.Parcelable;  
 import android.support.v4.view.PagerAdapter;  
 import android.support.v4.view.ViewPager;  
 import android.view.View;  
 import android.widget.ImageView;  

 public class GalleryPagerAdapter extends PagerAdapter {  

      private Activity activity;  
      private int[] drawableIDs;  

      public GalleryPagerAdapter(Activity activity,int[] drawableIDs){  

           this.activity = activity;  
           this.drawableIDs = drawableIDs;  
      }  

   @Override  
   public int getCount() {  
       return drawableIDs.length;  
   }  

      /**  
       * Create the page for the given position. The adapter is responsible  
       * for adding the view to the container given here, although it only  
       * must ensure this is done by the time it returns from  
       * {@link #finishUpdate()}.  
       *  
       * @param container The containing View in which the page will be shown.  
       * @param position The page position to be instantiated.  
       * @return Returns an Object representing the new page. This does not  
       * need to be a View, but can be some other container of the page.  
       */  
   @Override  
   public Object instantiateItem(View collection, int position) {  

       ImageView imageView = new ImageView(activity);  

       imageView.setBackgroundResource(drawableIDs[position]);  

       ((ViewPager) collection).addView(imageView,0);  

       return imageView;  
   }  

      /**  
       * Remove a page for the given position. The adapter is responsible  
       * for removing the view from its container, although it only must ensure  
       * this is done by the time it returns from {@link #finishUpdate()}.  
       *  
       * @param container The containing View from which the page will be removed.  
       * @param position The page position to be removed.  
       * @param object The same object that was returned by  
       * {@link #instantiateItem(View, int)}.  
       */  
   @Override  
   public void destroyItem(View collection, int position, Object view) {  
       ((ViewPager) collection).removeView((ImageView) view);  
   }  

   @Override  
   public boolean isViewFromObject(View view, Object object) {  
       return view==((ImageView)object);  
   }  

      /**  
       * Called when the a change in the shown pages has been completed. At this  
       * point you must ensure that all of the pages have actually been added or  
       * removed from the container as appropriate.  
       * @param container The containing View which is displaying this adapter's  
       * page views.  
       */  
   @Override  
   public void finishUpdate(View arg0) {}  

   @Override  
   public void restoreState(Parcelable arg0, ClassLoader arg1) {}  

   @Override  
   public Parcelable saveState() {  
       return null;  
   }  

   @Override  
   public void startUpdate(View arg0) {}  
 }  

最后,活动

 import android.app.Activity;  
 import android.os.Bundle;  
 import android.support.v4.view.ViewPager;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.widget.ImageView;  
 import android.widget.LinearLayout;  
 import android.widget.TextView;  

 public class TestingActivity extends Activity {  

      // mainLayout is the child of the HorizontalScrollView ...  
      private LinearLayout mainLayout;  

      // this is an array that holds the IDs of the drawables ...  
      private int[] images = {R.drawable.dd1, R.drawable.dd2, 
      R.drawable.dd3, R.drawable.dd4, R.drawable.dd5, R.drawable.dd6, R.drawable.dd7};  

      private View cell;  
      private TextView text;  

      private ViewPager viewPager;  

      @Override  
      public void onBackPressed() {  

           if(viewPager != null && viewPager.isShown()){  

                viewPager.setVisibility(View.GONE);  
           }  
           else{  

                super.onBackPressed();  
           }  
      }  

   /** Called when the activity is first created. */  
   @Override  
   public void onCreate(Bundle icicle) {  
     super.onCreate(icicle);  

     setContentView(R.layout.main);  

     viewPager = (ViewPager) findViewById(R.id._viewPager);  

     mainLayout = (LinearLayout) findViewById(R.id._linearLayout);  

     for (int i = 0; i < images.length; i++) {  

          cell = getLayoutInflater().inflate(R.layout.cell, null);  

          final ImageView imageView = (ImageView) cell.findViewById(R.id._image);  
          imageView.setOnClickListener(new OnClickListener() {  

                     @Override  
                     public void onClick(View v) {  

                          viewPager.setVisibility(View.VISIBLE);  
                          viewPager.setAdapter
                          (new GalleryPagerAdapter(TestingActivity.this, images));  
                          viewPager.setCurrentItem(v.getId());  
                     }  
                });  

          imageView.setId(i);  

          text = (TextView) cell.findViewById(R.id._imageName);  

          imageView.setImageResource(images[i]);  
          text.setText("Image#"+(i+1));  

          mainLayout.addView(cell);  
       }  
   }  
 } 

如果您不想使用ViewPager,那么您应该以水平方式尝试RecyclerView,使用RecyclerView,您可以使用自动位置检查实时添加和删除视图this tutorial for more help