gpt4 book ai didi

java - Android java - 为2个或更多viewpagers制作1个ImageAdapter(当前为2个viewpagers制作2个适配器)

转载 作者:太空宇宙 更新时间:2023-11-04 09:07:38 24 4
gpt4 key购买 nike

我制作了一个 ImageAdapter 类,其中包含图像和一个包含 2 个按钮的 Activity ,单击每个按钮都会打开一个带有 viewpager 的新 Activity 。两个viewpager有不同的图像,这就是我缺乏经验的地方。

我采取了简单的方法,复制了 ImageAdapter(将其命名为 ImageAdapter2)并将其链接到第二个 View 页面。 visual representation

它工作得很好,但我想做的是清理它并通过 1 个适配器和 1 个 viewpager 来完成这一切。我尝试通过 Intent getStringExtra 来完成此操作,但它无法识别 Intent 方法。这是我的类(class):

ImageAdapter(ImageAdapter2 是相同的,只是 sliderImageId 中的图像不同):

    package hr.cnzd.prepoznajmoenasilje;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;


public class ImageAdapter extends PagerAdapter {

Context mContext;

ImageAdapter(Context context){
this.mContext = context;
}

@Override
public boolean isViewFromObject(View view, Object o){
return view == ((ImageView) o);
}

private int[] sliderImageId = new int[]{
R.drawable.djeca01, R.drawable.djeca02, R.drawable.djeca03, R.drawable.djeca04, R.drawable.djeca05, R.drawable.djeca06, R.drawable.djeca07, R.drawable.djeca08
};

@Override
public Object instantiateItem(ViewGroup viewGroup, int position){
ImageView imageView = new ImageView(mContext);
imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(sliderImageId[position]);
((ViewPager) viewGroup).addView(imageView, 0);
return imageView;
}


@Override
public void destroyItem(ViewGroup viewGroup, int position, Object o){
((ViewPager) viewGroup).removeView((ImageView) o);
}

@Override
public int getCount(){
return sliderImageId.length;
}

}

带有按钮的 Activity :

package hr.cnzd.prepoznajmoenasilje;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import androidx.appcompat.app.AppCompatActivity;
public class MA_Savjeti extends AppCompatActivity {
private ImageButton imageButtonDjeca;
private ImageButton imageButtonOdrasli;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ma__savjeti);

imageButtonDjeca = findViewById(R.id.imgBtnDjeca);
imageButtonDjeca.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openActivitySavjetiDjeca();
}
});

imageButtonOdrasli = findViewById(R.id.imgBtnOdrasli);
imageButtonOdrasli.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openActivitySavjetiOdrasli();
}
});
}

public void openActivitySavjetiDjeca() {
Intent intent = new Intent(this, Savjeti_Djeca.class);
intent.putExtra("savjeti", "djeca");
startActivity(intent);
}

public void openActivitySavjetiOdrasli() {
Intent intent = new Intent(this, Savjeti_Odrasli.class);
intent.putExtra("savjeti", "odrasli");
startActivity(intent);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
}

使用viewpager的 Activity (使用viewpager的其他 Activity 具有相同的代码,只是调用ImageAdapter2)

package hr.cnzd.prepoznajmoenasilje;

import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

public class Savjeti_Djeca extends AppCompatActivity {

ViewPager viewPager;
ImageAdapter imageAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.savjeti_djeca);
viewPager = findViewById(R.id.viewpager);
imageAdapter = new ImageAdapter(this);
viewPager.setAdapter(imageAdapter);
}

}

TLDR:我有 2 个 ImageAdapter 和 2 个 Viewpager,但我想通过 1 个 ImageAdapter 和 1 个 Viewpager 来实现(之前 Activity 中的 2 个按钮决定在 viewpager 上显示哪组图像)

最佳答案

使用ViewPager2而不是这种方式解决了它。

关于java - Android java - 为2个或更多viewpagers制作1个ImageAdapter(当前为2个viewpagers制作2个适配器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60040318/

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