在標題。我想要有由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;他們非常明顯而且簡單,所以我不會發布它們。
如何實現此滑動圖片功能?
如何確定網格中圖片的尺寸與設備無關? 其實我認爲我硬編碼它的線:
imageView.setLayoutParams(new GridView.LayoutParams(480,402));
你能給出一些更多的提示和想法如何使用ViewPager和PagerAdapter?我應該把它放在ImageView和Button中的activity_full_image.xml和FullImageActivity類中適當的java代碼之間? – monterinio