Viewpager2 从 Activity 上的选定项目获取数据

Viewpager2 get data from selected item on Activity

我想在 activity 上用 toast 显示来自 viewpager2 的所选项目描述。

如何从 viewpager 获取数据?

我可以显示项目位置,但我想用 toast 显示所选项目的描述。

抱歉我的英语不好。

感谢您的帮助。

我的代码是这样的;

public class MainActivity extends AppCompatActivity {
    private ViewPager2 viewPager2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager2 = findViewById(R.id.viewPager2);
        PagerAdapter pagerAdapter = new PagerAdapter(fetchDummyData());
        viewPager2.setAdapter(pagerAdapter);
        viewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);

        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);

            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);

                Toast.makeText(MainActivity.this, "selected item description should be here", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);

            }
        });
    }

    private List<PagerM> fetchDummyData() {
        List<PagerM> pagerMList = new ArrayList<>();
        String[] dummyArr = getResources().getStringArray(R.array.array_str_values);
        for (String str : dummyArr) {
            PagerM pagerM = new PagerM();
            pagerM.setPagerDescription(str);
            pagerMList.add(pagerM);
        }
        return pagerMList;
    }
}

array_str_values:

<string-array name="array_str_values">
    <item>car</item>
    <item>red</item>
    <item>new</item>
</string-array>

我的适配器:

public class PagerAdapter extends RecyclerView.Adapter {
    private List<PagerM> pagerMList;

    class PagerViewHolder extends RecyclerView.ViewHolder {
        private TextView txtDescription;

        public PagerViewHolder(@NonNull View itemView) {
            super(itemView);
            txtDescription = itemView.findViewById(R.id.txtDescription);
        }
    }

    public PagerAdapter(List<PagerM> pagerMList) {
        this.pagerMList = pagerMList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_pager, parent, false);
        return new PagerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        PagerViewHolder viewHolder = (PagerViewHolder) holder;
        PagerM pagerM = pagerMList.get(position);
        viewHolder.txtDescription.setText(pagerM.getPagerDescription());
    }

    @Override
    public int getItemCount() {
        return pagerMList.size();
    }
}

PagerM:

public class PagerM {
    private String pagerDescription;

    public String getPagerDescription() {
        return pagerDescription;
    }

    public void setPagerDescription(String pagerDescription) {
        this.pagerDescription = pagerDescription;
    }
}

制作列表pagerMList一个你activity的字段。

private List<PagerM> pagerMList = new ArrayList<>();

然后在你的onPageSelected方法中按位置获取数据。

public class MainActivity extends AppCompatActivity {
private ViewPager2 viewPager2;
private List<PagerM> pagerMList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    fetchDummyData();
    viewPager2 = findViewById(R.id.viewPager2);
    PagerAdapter pagerAdapter = new PagerAdapter(pagerMList);
    viewPager2.setAdapter(pagerAdapter);
    viewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);

    viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            super.onPageScrolled(position, positionOffset, positionOffsetPixels);

        }

        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);

            Toast.makeText(MainActivity.this, pagerMList.get(position).getPagerDescription(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            super.onPageScrollStateChanged(state);

        }
    });
}

private List<PagerM> fetchDummyData() {
    String[] dummyArr = getResources().getStringArray(R.array.array_str_values);
    for (String str : dummyArr) {
        PagerM pagerM = new PagerM();
        pagerM.setPagerDescription(str);
        pagerMList.add(pagerM);
    }
    return pagerMList;
}

}