2017-03-14 58 views
0

在标题。我想要有由2列组成的图片网格,每张图片必须具有相同的尺寸。用户应该能够点击每张图片进入全屏模式,然后从左到右滑动图片(在该模式下的全尺寸图片)。到目前为止,我制作了可点击元素的网格,但没有滑动它们的功能。这里是我的代码:Android画廊应用程序与滑动图片

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.miki.androidtwo.MainActivity"> 

    <GridView 
     android:id="@+id/grid" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:numColumns="2" 
     android:scrollbars="vertical" 
     android:gravity="center"> 
    </GridView> 

</RelativeLayout> 

activity_full_image.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="0dp" 
    android:paddingLeft="0dp" 
    android:paddingRight="0dp" 
    android:paddingTop="0dp" 
    tools:context="com.example.miki.androidtwo.FullImageActivity"> 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" /> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:text="I want to see other doggos" /> 
</RelativeLayout> 

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     GridView gridView = (GridView) findViewById(R.id.grid); 

     // Instance of ImageAdapter Class 
     gridView.setAdapter(new ImageAdapter(this)); 

     /** 
     * On Click event for Single Gridview Item 
     * */ 
     gridView.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(), FullImageActivity.class); 
       // passing array index 
       i.putExtra("id", position); 
       startActivity(i); 
      } 
     }); 
    } 
} 

其他类FullImageActivity和ImageAdapter;他们非常明显而且简单,所以我不会发布它们。

  1. 如何实现此滑动图片功能?

  2. 如何确定网格中图片的尺寸与设备无关? 其实我认为我硬编码它的线:

    imageView.setLayoutParams(new GridView.LayoutParams(480,402));

回答

0

如果要在他们不全屏滑动的图像,你可以使用一个HorizontalScrollView,或者如果你想滑动他们,而他们是全屏,您可以使用ViewPager,具有PagerAdapter一起。

因此,填充网格imageView.setLayoutParams是很好用来设置每个imageview的大小,就像你做的那样。当用户点击图片时,您将转到一个新的活动,该活动使用ViewPager和PagerAdapter进行设置。这样用户可以滑过全屏图像。

+0

你能给出一些更多的提示和想法如何使用ViewPager和PagerAdapter?我应该把它放在ImageView和Button中的activity_full_image.xml和FullImageActivity类中适当的java代码之间? – monterinio