gpt4 book ai didi

Android:使用不同图像的 Activity 的下一个和上一个按钮

转载 作者:行者123 更新时间:2023-11-30 01:34:03 25 4
gpt4 key购买 nike

完整图像 Activity 的 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="800">

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:clickable="true"
android:focusable="true"
android:weightSum="200">

<Button
android:layout_width="140dp"
android:layout_height="wrap_content"
android:text="Previous"
android:id="@+id/textView2"
android:layout_gravity="center"/>
<Button
android:layout_width="140dp"
android:layout_height="wrap_content"
android:text="Next"
android:id="@+id/Next"
android:layout_gravity="center" />
</LinearLayout>

</LinearLayout>

Activity

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final GridView gv = (GridView)findViewById(R.id.grid_view);
gv.setAdapter(new ImageAdapter(this));

gv.setOnItemClickListener(new AdapterView.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);
}
});

这是我的全图 Activity 课

ImageAdapter imageAdapter; ImageView imageView;
int position;

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

Button next = (Button)findViewById(R.id.Next);
imageView = (ImageView) findViewById(R.id.full_image_view);
Intent i = getIntent();
position = i.getExtras().getInt("id");

imageAdapter = new ImageAdapter(this);
imageView.setImageResource(imageAdapter.mThumbIds[position]);

next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(imageAdapter.mThumbIds[position] > imageAdapter.mThumbIds.length - 1) {
position++;
imageView.setImageResource(imageAdapter.mThumbIds[position]);
}
else{
Log.d("TAG", "Reached Last Record");
}
if(position < imageAdapter.mThumbIds.length - 1) {
position++;
}
else{
Log.d("TAG", "Reached Last Record");
}
}
});

为了让事情顺利进行,我应该如何以及在何处实现下一个和上一个按钮逻辑。任何指导将不胜感激。

最佳答案

我想通了......需要不断递增或递减保存图像的数组的监听器。

next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageView.setImageResource(imageAdapter.mThumbIds[++position]);
}});

previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageView.setImageResource(imageAdapter.mThumbIds[--position]);
}});

关于Android:使用不同图像的 Activity 的下一个和上一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35342324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com