查看分页器它们不与片段事件交互

时间:2018-07-16 19:27:27

标签: android android-viewpager

您好,当我尝试使用viewPager将片段传递给另一个片段时,我想为诸如playStore之类的评论创建视图分页器,我想捕获等级栏的onclick事件。感谢您提出的建议。我留下代码了。

video check

我的片段控制活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_commet);
    showToolbar("Comentarios", true);
    gson = new Gson();
    ReadFirebase.getInstance();

    viewPager = (ViewPager) findViewById(R.id.viewpager); // aqui creo el viewPager.
    viewPager.setAdapter(new CustomPagerAdapter(this));
    TITULO =  (TextView)   findViewById(R.id.textView1);
    ratingbar2 = (RatingBar) findViewById(R.id.ratingBar2);
    btnSendComment = (Button)  findViewById(R.id.btnSendComment);
    overlapImage = (ImageView) findViewById(R.id.overlapImage);

    Intent i = getIntent();
    Bundle bundle = i.getExtras();

    if (bundle != null) {
        RECEPTOR = bundle.getString("idPlaces");
        CATEGORY = bundle.getString("getPlacesCategory");
        RATE_COUNTER = Integer.parseInt(bundle.getString("rate_counter"));
        imagen_circle = (bundle.getString("imagen_circle"));
        Picasso.get().load(imagen_circle).into(overlapImage);
    }

    LEER_MENSAJE();
    ratingbar2.setRating(RATE_COUNTER);
    btnSendComment.setText("Valorar");

    TITULO.setText("Calificación de"+" "+ CATEGORY);

    btnSendComment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (check){
                viewPager.setCurrentItem(getItem(+1), true);
                check = false;
                btnSendComment.setText("Valorar");
            } else {
                save_comments();
                viewPager.setCurrentItem(getItem(-1), true);
                check = true;
                btnSendComment.setText("Finalizar");

            }
        }
    });

}

我的适配器:

public class CustomPagerAdapter extends PagerAdapter {

    private Context mContext;

    public CustomPagerAdapter(Context context) {
        mContext = context;
    }

    @Override
    public Object instantiateItem(ViewGroup collection, int position) {
        ModelObject modelObject = ModelObject.values()[position];
        LayoutInflater inflater = LayoutInflater.from(mContext);
        ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), collection, false);


        collection.addView(layout);
        return layout;
    }

    @Override
    public void destroyItem(ViewGroup collection, int position, Object view) {
        collection.removeView((View) view);
    }

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

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

    @Override
    public CharSequence getPageTitle(int position) {
        ModelObject customPagerEnum = ModelObject.values()[position];

        return mContext.getString(customPagerEnum.getTitleResId());
    }

}

我的模特:

public enum ModelObject {

    comentarios1(R.string.comentarios1, R.layout.fragment_comment1),
    comentarios2(R.string.comentarios2, R.layout.fragment_comment2);
  //  GREEN(R.string.green, R.layout.view_green);

    private int mTitleResId;
    private int mLayoutResId;

    ModelObject(int titleResId, int layoutResId) {
        mTitleResId = titleResId;
        mLayoutResId = layoutResId;
    }

    public int getTitleResId() {
        return mTitleResId;
    }

    public int getLayoutResId() {
        return mLayoutResId;
    }



}

0 个答案:

没有答案