<small id='TFz9H'></small><noframes id='TFz9H'>

<tfoot id='TFz9H'></tfoot>

    1. <legend id='TFz9H'><style id='TFz9H'><dir id='TFz9H'><q id='TFz9H'></q></dir></style></legend>
    2. <i id='TFz9H'><tr id='TFz9H'><dt id='TFz9H'><q id='TFz9H'><span id='TFz9H'><b id='TFz9H'><form id='TFz9H'><ins id='TFz9H'></ins><ul id='TFz9H'></ul><sub id='TFz9H'></sub></form><legend id='TFz9H'></legend><bdo id='TFz9H'><pre id='TFz9H'><center id='TFz9H'></center></pre></bdo></b><th id='TFz9H'></th></span></q></dt></tr></i><div id='TFz9H'><tfoot id='TFz9H'></tfoot><dl id='TFz9H'><fieldset id='TFz9H'></fieldset></dl></div>
          <bdo id='TFz9H'></bdo><ul id='TFz9H'></ul>

        如何在全屏中滑动选定的网格图像

        时间:2023-10-03
        <legend id='nWoRR'><style id='nWoRR'><dir id='nWoRR'><q id='nWoRR'></q></dir></style></legend>
            <bdo id='nWoRR'></bdo><ul id='nWoRR'></ul>
          • <small id='nWoRR'></small><noframes id='nWoRR'>

            <tfoot id='nWoRR'></tfoot>
          • <i id='nWoRR'><tr id='nWoRR'><dt id='nWoRR'><q id='nWoRR'><span id='nWoRR'><b id='nWoRR'><form id='nWoRR'><ins id='nWoRR'></ins><ul id='nWoRR'></ul><sub id='nWoRR'></sub></form><legend id='nWoRR'></legend><bdo id='nWoRR'><pre id='nWoRR'><center id='nWoRR'></center></pre></bdo></b><th id='nWoRR'></th></span></q></dt></tr></i><div id='nWoRR'><tfoot id='nWoRR'></tfoot><dl id='nWoRR'><fieldset id='nWoRR'></fieldset></dl></div>

                  <tbody id='nWoRR'></tbody>

                  本文介绍了如何在全屏中滑动选定的网格图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我创建了一个网格视图图像应用程序,我想在图像滑动中显示选定的图像.

                  i created a grid-view image app, and i want to show selected image in image swipe.

                  实际上我在我的应用中实现了图像滑动但是问题是图像滑动从第一张图像而不是从选定图像开始.

                  actually i implemented image swipe in my app but the problem is image swipe starts from 1st image not from selected image.

                  示例:

                  如果我选择第三张图片,图片滑动应该从第三张图片而不是第一张图片开始.

                  if i choose 3rd image image swipe should starts from 3rd image not from 1st image.

                  我的代码:

                  MainActivity.java

                  package com.td.gridview;
                  
                  import android.app.Activity;
                  import android.content.Context;
                  import android.content.Intent;
                  import android.os.Bundle;
                  import android.view.LayoutInflater;
                  import android.view.View;
                  import android.view.ViewGroup;
                  import android.view.ViewGroup.LayoutParams;
                  import android.view.ViewTreeObserver;
                  import android.widget.AdapterView;
                  import android.widget.BaseAdapter;
                  import android.widget.GridView;
                  import android.widget.ImageView;
                  import android.widget.RelativeLayout;
                  import android.widget.TextView;
                  import android.widget.AdapterView.OnItemClickListener;
                  
                  public class MainActivity extends Activity {
                  
                      private GridView photoGrid;
                      private int mPhotoSize, mPhotoSpacing;
                      private ImageAdapter imageAdapter;
                  
                      // Some items to add to the GRID
                      private static final String[] CONTENT = new String[] 
                              { 
                              "pic1",
                              "pic2",
                              "pic3",
                              "pic4",
                              "pic5",
                              "pic6",
                              "pic7",
                              "pic8",
                              "pic9"
                              };
                      static final int[] ICONS = new int[] 
                              { 
                              R.drawable.pic1,
                              R.drawable.pic2,
                              R.drawable.pic3,
                              R.drawable.pic4,
                              R.drawable.pic5,
                              R.drawable.pic6,
                              R.drawable.pic7,
                              R.drawable.pic8,
                              R.drawable.pic9
                              };
                  
                      @Override
                      protected void onCreate(Bundle savedInstanceState) {
                          super.onCreate(savedInstanceState);
                          setContentView(R.layout.activity_main);
                  
                          // get the photo size and spacing
                          mPhotoSize = getResources().getDimensionPixelSize(R.dimen.photo_size);
                          mPhotoSpacing = getResources().getDimensionPixelSize(R.dimen.photo_spacing);
                  
                          // initialize image adapter
                          imageAdapter = new ImageAdapter();
                  
                          photoGrid = (GridView) findViewById(R.id.albumGrid);
                  
                          //start sent image to full screen             
                  
                          /**
                           * On Click event for Single Gridview Item
                           * */
                          photoGrid.setOnItemClickListener(new OnItemClickListener() {
                              @Override
                              public void onItemClick(AdapterView<?> parent, View v,
                                      int position, long id) {
                  
                                  // Sending image id to FullScreenActivity
                                  Intent i = new Intent(getApplicationContext(), SwipeActivity.class);
                                  // passing array index
                                  i.putExtra("id", position);
                                  startActivity(i);
                              }
                          });
                          //end sent image to full screen
                  
                          // set image adapter to the GridView
                          photoGrid.setAdapter(imageAdapter);
                  
                          // get the view tree observer of the grid and set the height and numcols dynamically
                          photoGrid.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                              @Override
                              public void onGlobalLayout() {
                                  if (imageAdapter.getNumColumns() == 0) {
                                      final int numColumns = (int) Math.floor(photoGrid.getWidth() / (mPhotoSize + mPhotoSpacing));
                                      if (numColumns > 0) {
                                          final int columnWidth = (photoGrid.getWidth() / numColumns) - mPhotoSpacing;
                                          imageAdapter.setNumColumns(numColumns);
                                          imageAdapter.setItemHeight(columnWidth);
                  
                                      }
                                  }
                              }
                          });
                      }
                  
                      // ///////// ImageAdapter class /////////////////
                      public class ImageAdapter extends BaseAdapter {
                          private LayoutInflater mInflater;
                          private int mItemHeight = 0;
                          private int mNumColumns = 0;
                          private RelativeLayout.LayoutParams mImageViewLayoutParams;
                  
                          public ImageAdapter() {
                              mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                              mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                                      LayoutParams.MATCH_PARENT);
                          }
                  
                          public int getCount() {
                              return CONTENT.length;
                          }
                  
                          // set numcols
                          public void setNumColumns(int numColumns) {
                              mNumColumns = numColumns;
                          }
                  
                          public int getNumColumns() {
                              return mNumColumns;
                          }
                  
                          // set photo item height
                          public void setItemHeight(int height) {
                              if (height == mItemHeight) {
                                  return;
                              }
                              mItemHeight = height;
                              mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
                              notifyDataSetChanged();
                          }
                  
                          public Object getItem(int position) {
                              return position;
                          }
                  
                          public long getItemId(int position) {
                              return position;
                          }
                  
                          public View getView(final int position, View view, ViewGroup parent) {
                  
                              if (view == null)
                                  view = mInflater.inflate(R.layout.photo_item, null);
                  
                              ImageView cover = (ImageView) view.findViewById(R.id.cover);
                              TextView title = (TextView) view.findViewById(R.id.title);
                  
                              cover.setLayoutParams(mImageViewLayoutParams);
                  
                              // Check the height matches our calculated column width
                              if (cover.getLayoutParams().height != mItemHeight) {
                                  cover.setLayoutParams(mImageViewLayoutParams);
                              }
                  
                              cover.setImageResource(ICONS[position % ICONS.length]);
                              title.setText(CONTENT[position % CONTENT.length]);
                  
                              return view;
                          }
                      }
                  
                  }
                  

                  SwipeActivity.java

                      package com.td.gridview;
                  
                  import android.app.Activity;
                  import android.content.Context;
                  import android.content.Intent;
                  import android.os.Bundle;
                  import android.support.v4.view.PagerAdapter;
                  import android.support.v4.view.ViewPager;
                  import android.view.View;
                  import android.view.ViewGroup;
                  import android.widget.ImageView;
                  import com.td.gridview.MainActivity;
                  
                  
                  
                  public class SwipeActivity extends Activity 
                  {   
                  
                    @Override
                    public void onCreate(Bundle savedInstanceState) {
                      super.onCreate(savedInstanceState);
                      setContentView(R.layout.swipe_view);
                  
                      int[] icons = MainActivity.ICONS;    
                  
                      //get image position
                  
                      Intent n = getIntent();
                      int position= n.getIntExtra("id", position);
                  
                      ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);    
                      ImagePagerAdapter adapter = new ImagePagerAdapter(SwipeActivity.this , MainActivity.ICONS[position]);
                      viewPager.setAdapter(adapter);
                      viewPager.setCurrentItem(position);
                    }
                  
                    private class ImagePagerAdapter extends PagerAdapter 
                    {
                        int[] icons = MainActivity.ICONS;    
                  
                      @Override
                      public int getCount() 
                      {
                        return icons.length;
                      }
                  
                      @Override
                      public boolean isViewFromObject(View view, Object object) 
                      {
                        return view == ((ImageView) object);
                      }
                  
                      @Override
                      public Object instantiateItem(ViewGroup container, int position) 
                      {
                        Context context = SwipeActivity.this;
                        ImageView imageView = new ImageView(context);
                  //      int padding = context.getResources().getDimensionPixelSize(
                  //          R.dimen.padding_large);
                  //      imageView.setPadding(padding, padding, padding, padding);
                        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                        imageView.setImageResource(icons[position]);
                        ((ViewPager) container).addView(imageView, 0);
                        return imageView;
                      }
                  
                      @Override
                      public void destroyItem(ViewGroup container, int position, Object object) 
                      {
                        ((ViewPager) container).removeView((ImageView) object);
                      }
                    }
                  }
                  

                  推荐答案

                  经过太多的废话/错误,我终于得到了答案,而且非常简单......;)

                  // get intent data
                      Intent i = getIntent();
                  
                      // Selected image id
                      int position = i.getExtras().getInt("id");
                  
                  ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
                      ImagePagerAdapter adapter = new ImagePagerAdapter();
                      viewPager.setAdapter(adapter);
                      viewPager.setCurrentItem(position);
                  

                  这是简单的答案,而不是这段代码:

                  ImagePagerAdapter adapter = new ImagePagerAdapter(mContext , icons[position]);
                  

                  这行得通.

                  viewPager.setCurrentItem(position);
                  

                  下面是我的坑应用代码:

                  MainActivity.java

                  package com.td.gridview;
                  
                  import android.app.Activity;
                  import android.content.Context;
                  import android.content.Intent;
                  import android.os.Bundle;
                  import android.view.LayoutInflater;
                  import android.view.View;
                  import android.view.ViewGroup;
                  import android.view.ViewGroup.LayoutParams;
                  import android.view.ViewTreeObserver;
                  import android.widget.AdapterView;
                  import android.widget.BaseAdapter;
                  import android.widget.GridView;
                  import android.widget.ImageView;
                  import android.widget.RelativeLayout;
                  import android.widget.TextView;
                  import android.widget.AdapterView.OnItemClickListener;
                  
                  public class MainActivity extends Activity {
                  
                  
                      private GridView photoGrid;
                      private int mPhotoSize, mPhotoSpacing;
                      private ImageAdapter imageAdapter;
                  
                      // Some items to add to the GRID
                      private static final String[] CONTENT = new String[] 
                              { 
                              "Akon", "Justin Bieber", "AlRight", "Big Sean",
                              "Britney Spears", "Hilary", "Micheal Buble", "Akon", "Justin Bieber", "AlRight", "Big Sean",
                              "Britney Spears", "Hilary", "Micheal Buble", "Britney Spears", "Hilary", "Micheal Buble", "Akon",
                              "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary", "Micheal Buble", "Akon",
                              "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary", "Micheal Buble", "Akon",
                              "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary", "Micheal Buble", "Britney Spears",
                              "Hilary", "Micheal Buble", "Akon", "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary",
                              "Micheal Buble" 
                              };
                      static final int[] ICONS = new int[] 
                              { 
                              R.drawable.cover_akon, R.drawable.cover_justin,
                              R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary,
                              R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin, R.drawable.cover_alright,
                              R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb,
                              R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon,
                              R.drawable.cover_justin, R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney,
                              R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin,
                              R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary,
                              R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin, R.drawable.cover_alright,
                              R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb,
                              R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon,
                              R.drawable.cover_justin, R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney,
                              R.drawable.cover_hilary, R.drawable.cover_mb 
                              };
                  
                      @Override
                      protected void onCreate(Bundle savedInstanceState) {
                          super.onCreate(savedInstanceState);
                          setContentView(R.layout.activity_main);
                  
                  
                  
                          // get the photo size and spacing
                          mPhotoSize = getResources().getDimensionPixelSize(R.dimen.photo_size);
                          mPhotoSpacing = getResources().getDimensionPixelSize(R.dimen.photo_spacing);
                  
                          // initialize image adapter
                          imageAdapter = new ImageAdapter();
                  
                          photoGrid = (GridView) findViewById(R.id.albumGrid);
                  
                          //start sent image to full screen             
                  
                          /**
                           * On Click event for Single Gridview Item
                           * */
                          photoGrid.setOnItemClickListener(new OnItemClickListener() {
                              @Override
                              public void onItemClick(AdapterView<?> parent, View v,
                                      int position, long id) {
                  
                                  // Sending image id to FullScreenActivity
                                  Intent i = new Intent(getApplicationContext(), SwipeActivity.class);
                                  // passing array index
                                  i.putExtra("id", position);
                                  startActivity(i);
                              }
                          });
                          //end sent image to full screen
                  
                          // set image adapter to the GridView
                          photoGrid.setAdapter(imageAdapter);
                  
                          // get the view tree observer of the grid and set the height and numcols dynamically
                          photoGrid.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                              @Override
                              public void onGlobalLayout() {
                                  if (imageAdapter.getNumColumns() == 0) {
                                      final int numColumns = (int) Math.floor(photoGrid.getWidth() / (mPhotoSize + mPhotoSpacing));
                                      if (numColumns > 0) {
                                          final int columnWidth = (photoGrid.getWidth() / numColumns) - mPhotoSpacing;
                                          imageAdapter.setNumColumns(numColumns);
                                          imageAdapter.setItemHeight(columnWidth);
                  
                                      }
                                  }
                              }
                          });
                      }
                  
                      // ///////// ImageAdapter class /////////////////
                      public class ImageAdapter extends BaseAdapter {
                          private LayoutInflater mInflater;
                          private int mItemHeight = 0;
                          private int mNumColumns = 0;
                          private RelativeLayout.LayoutParams mImageViewLayoutParams;
                  
                          public ImageAdapter() {
                              mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                              mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                                      LayoutParams.MATCH_PARENT);
                          }
                  
                          public int getCount() {
                              return CONTENT.length;
                          }
                  
                          // set numcols
                          public void setNumColumns(int numColumns) {
                              mNumColumns = numColumns;
                          }
                  
                          public int getNumColumns() {
                              return mNumColumns;
                          }
                  
                          // set photo item height
                          public void setItemHeight(int height) {
                              if (height == mItemHeight) {
                                  return;
                              }
                              mItemHeight = height;
                              mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
                              notifyDataSetChanged();
                          }
                  
                          public Object getItem(int position) {
                              return position;
                          }
                  
                          public long getItemId(int position) {
                              return position;
                          }
                  
                          public View getView(final int position, View view, ViewGroup parent) {
                  
                              if (view == null)
                                  view = mInflater.inflate(R.layout.photo_item, null);
                  
                              ImageView cover = (ImageView) view.findViewById(R.id.cover);
                              TextView title = (TextView) view.findViewById(R.id.title);
                  
                              cover.setLayoutParams(mImageViewLayoutParams);
                  
                              // Check the height matches our calculated column width
                              if (cover.getLayoutParams().height != mItemHeight) {
                                  cover.setLayoutParams(mImageViewLayoutParams);
                              }
                  
                              cover.setImageResource(ICONS[position % ICONS.length]);
                              title.setText(CONTENT[position % CONTENT.length]);
                  
                              return view;
                          }
                      }
                  
                  }
                  

                  SwipeActivity.java

                      package com.td.gridview;
                  
                      import android.app.Activity;
                      import android.content.Context;
                      import android.content.Intent;
                      import android.os.Bundle;
                      import android.support.v4.view.PagerAdapter;
                      import android.support.v4.view.ViewPager;
                      import android.view.View;
                      import android.view.ViewGroup;
                      import android.widget.ImageView;
                  
                  
                  
                      public class SwipeActivity extends Activity 
                      {   
                  
                        @Override
                        public void onCreate(Bundle savedInstanceState) {
                          super.onCreate(savedInstanceState);
                          setContentView(R.layout.swipe_view);
                  
                       // get intent data
                          Intent i = getIntent();
                  
                          // Selected image id
                          int position = i.getExtras().getInt("id");
                  
                      ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
                      ImagePagerAdapter adapter = new ImagePagerAdapter();
                      viewPager.setAdapter(adapter);
                      viewPager.setCurrentItem(position);
                    }
                  
                    private class ImagePagerAdapter extends PagerAdapter 
                    {
                        int[] icons = MainActivity.ICONS;    
                  
                      @Override
                      public int getCount() 
                      {
                        return icons.length;
                      }
                  
                      @Override
                      public boolean isViewFromObject(View view, Object object) 
                      {
                        return view == ((ImageView) object);
                      }
                  
                      @Override
                      public Object instantiateItem(ViewGroup container, int position) 
                      {
                        Context context = SwipeActivity.this;
                        ImageView imageView = new ImageView(context);
                  //      int padding = context.getResources().getDimensionPixelSize(
                  //          R.dimen.padding_large);
                  //      imageView.setPadding(padding, padding, padding, padding);
                        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                        imageView.setImageResource(icons[position]);
                        ((ViewPager) container).addView(imageView, 0);
                        return imageView;
                      }
                  
                      @Override
                      public void destroyItem(ViewGroup container, int position, Object object) 
                      {
                        ((ViewPager) container).removeView((ImageView) object);
                      }
                    }
                  }
                  

                  activity_main.xml

                  <GridView xmlns:android="http://schemas.android.com/apk/res/android"
                      android:id="@+id/albumGrid"
                      style="@style/PhotoGrid"
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent"
                      android:background="@color/black"
                      android:columnWidth="@dimen/photo_size"
                      android:horizontalSpacing="@dimen/photo_spacing"
                      android:numColumns="auto_fit"
                      android:padding="4dp"
                      android:scrollbars="none"
                      android:stretchMode="columnWidth"
                      android:verticalSpacing="@dimen/photo_spacing" />
                  

                  photo_item.xml

                  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@+id/album_item"
                  android:layout_width="match_parent"
                  android:layout_height="fill_parent"
                  android:orientation="vertical" >
                  
                  <ImageView
                      android:id="@+id/cover"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent" />
                  
                  <LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_alignBottom="@+id/cover"
                      android:background="#70000000"
                      android:padding="6dp" >
                  
                      <TextView
                          android:id="@+id/title"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:ellipsize="end"
                          android:singleLine="true"
                          android:textColor="@color/white"
                          android:textSize="12sp"
                          android:textStyle="bold" />
                  </LinearLayout>
                  </RelativeLayout>
                  

                  swipe_view.xml

                  <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
                      android:id="@+id/view_pager"
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent" />
                  

                  这篇关于如何在全屏中滑动选定的网格图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在视图寻呼机中自动更改图像,当它到达最后一张图像时,它应该自动到达第一张图像 下一篇:将数据从 Activity 发送到 TabLayout 片段

                  相关文章

                  • <bdo id='9Js3v'></bdo><ul id='9Js3v'></ul>

                      <small id='9Js3v'></small><noframes id='9Js3v'>

                      <tfoot id='9Js3v'></tfoot>
                      <legend id='9Js3v'><style id='9Js3v'><dir id='9Js3v'><q id='9Js3v'></q></dir></style></legend>
                      <i id='9Js3v'><tr id='9Js3v'><dt id='9Js3v'><q id='9Js3v'><span id='9Js3v'><b id='9Js3v'><form id='9Js3v'><ins id='9Js3v'></ins><ul id='9Js3v'></ul><sub id='9Js3v'></sub></form><legend id='9Js3v'></legend><bdo id='9Js3v'><pre id='9Js3v'><center id='9Js3v'></center></pre></bdo></b><th id='9Js3v'></th></span></q></dt></tr></i><div id='9Js3v'><tfoot id='9Js3v'></tfoot><dl id='9Js3v'><fieldset id='9Js3v'></fieldset></dl></div>