2

我想在带有 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();
    }
}

寻呼机:

public class PagerM {
    private String pagerDescription;

    public String getPagerDescription() {
        return pagerDescription;
    }

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

1 回答 1

2

使 List pagerMList 成为您活动的字段。

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;
}

}

于 2019-11-19T12:03:27.533 回答