【问题标题】:Stucked in horizontal Image Scrolling卡在水平图像滚动中
【发布时间】:2017-01-11 00:29:31
【问题描述】:

我是一名初级 android 开发人员,正在制作一个用户可以销售和购买商品的 android 应用程序。

我卡得很厉害,请帮忙。

Here is the UI i am trying to make

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

【问题讨论】:

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


    【解决方案1】:

    我认为你尝试了错误的事情,你应该尝试使用 android ViewPager 对于您的问题,您应该查看this example ,这里是来自This Link的查看页面的示例代码

    ma​​in.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>   
    

    页面适配器类

    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

    【讨论】:

    • 非常感谢,这是查看图像的最佳方式...我可以轻松设置图像,但如何删除任何添加的图像,剩余图像会动态改变它们的位置
    • @tahanaqvi 如果有帮助,请接受答案。请参考谷歌以获得更多帮助,顺便说一句,这并不难实现
    • 它没有帮助,但我正在尝试......我已经搜索并遇到了问题。你能给我任何教程或其他东西的链接,其中在水平滚动视图中从相机或画廊中添加和删除图像
    • 很抱歉,但我的客户说我不必在视图寻呼机中查看图像,我只需要水平显示它们并删除它们而不用全屏查看。所以我的主要问题是处理来自画廊和相机的图像,如何添加然后删除任何项目并重新定位其他项目
    • @tahanaqvi 那么你应该试试recyclerview,它可以帮助你在没有viewpager的情况下完成你的任务检查详细信息的更新答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    相关资源
    最近更新 更多