如何从Android中的另一个视图访问组件

时间:2019-01-23 06:13:01

标签: android android-layout android-viewpager android-view

我创建了一个自定义对话框,并在其中创建了一个视图页面,它们都有不同的布局文件。显示对话框的方法在MainActivity中。

我将布局填充到我的适配器类中,在其中填充内容。在此布局中,有一个按钮。我的问题是“如何从MainActivity的适配器类中填充的布局访问按钮。之所以这样做,是因为我想在单击按钮时关闭对话框。

谢谢。

这是我到目前为止所做的代码。

MainActivity:

public void ShowPromoOverlay(){

    promoOverlay = new Dialog(this);
    promoOverlay.requestWindowFeature(Window.FEATURE_NO_TITLE);
    promoOverlay.setContentView(R.layout.fragment_overlay);


    final List<Promotion> pageArr = new ArrayList<>();
    int maxcounter = 5;
    if (promotionList.size() < maxcounter) {
        maxcounter = promotionList.size();
    }
    for (int counter = 0; counter < maxcounter; counter++){
        pageArr.add(promotionList.get(counter));
    }

    TestPagerAdapter adapter = new TestPagerAdapter(this, pageArr);
    cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager 
    pager = promoOverlay.findViewById(R.id.overlayPager);
    pager.setPageMargin(50);
    pager.setAdapter(adapter);

    com.viewpagerindicator.CirclePageIndicator indicator = 
    promoOverlay.findViewById(R.id.overlay_page_indicator);
    indicator.setViewPager(pager);
    indicator.setCurrentItem(0);

    promoOverlay.show();

    View inlcudeLayout = findViewById(R.id.my_custom_dialog_layout);
    ImageView closeBtn = (ImageView) inlcudeLayout.findViewById(R.id.closeBtn);
    closeBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            promoOverlya.dismiss();
        }
    });
}

fragment_overlay:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_over"
android:background="@color/bg_orange"
android:layout_width="match_parent"
android:layout_height="match_parent">

<cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/overlayPager">

    <include layout="@layout/my_custom_dialog" />

</cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager>

<com.viewpagerindicator.CirclePageIndicator
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/overlay_page_indicator"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="70dp">

</com.viewpagerindicator.CirclePageIndicator>
</RelativeLayout>

my_custom_dialog:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_custom_dialog_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">

<android.support.v7.widget.CardView
    android:layout_width="310dp"
    android:layout_height="300dp"
    android:layout_centerInParent="true"
    android:layout_marginTop="40dp"
    app:cardBackgroundColor="@color/white"
    app:cardCornerRadius="15dp">

    <ImageView
        android:id="@+id/closeBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_clear_grey_600_24dp"
        android:layout_marginTop="7dp"
        android:layout_marginRight="7dp"
        android:elevation="5dp"
        android:layout_gravity="right"/>

</android.support.v7.widget.CardView>
</RelativeLayout>

TestPagerAdapter:

public class TestPagerAdapter extends PagerAdapter {

Context context;
LayoutInflater inflater;
List<Promotion> pageArr;

public TestPagerAdapter(Context context, List<Promotion> pageArr){
    this.context = context;
    this.pageArr = pageArr;

    inflater = ((Activity) context).getLayoutInflater();
}

public TestPagerAdapter(Context context){
    this.context = context;

    inflater = ((Activity) context).getLayoutInflater();

}
@Override
public int getCount(){
    return pageArr.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position){
   View view = inflater.inflate(R.layout.my_custom_dialog, container, 
false);
    TextView prTitle = view.findViewById(R.id.title_pr);
    TextView prDescription = view.findViewById(R.id.description_pr);
    TextView readMoreBtn = view.findViewById(R.id.readMore);

    view.setTag(position);
    ((ViewPager) container).addView(view);


    final Promotion promotion = pageArr.get(position);

    prTitle.setText(promotion.getTitle());
    prDescription.setText(promotion.getDescription());

    return view;

}

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

@Override
public void destroyItem(View container, int position, Object object) {
    ((ViewPager) container).removeView((View) object);
}
}

1 个答案:

答案 0 :(得分:0)

好问题。您必须使用该界面。让我们尝试一下这段代码。

public interface OnButtonClickListner {
  public void OnButtonClick();
}

现在,您需要在适配器中传递此接口。像这样

TestPagerAdapter adapter = new TestPagerAdapter(this, pageArr TestPagerAdapter adapter = new TestPagerAdapter(this,new OnButtonClickListner()
{

    @Override
    public void OnButtonClick() {
    // dismiss dialog here
    }
});

现在,您需要更改Viewpager适配器的构造函数。如下所示:

public TestPagerAdapter(Context context, List<Promotion> pageArr,OnButtonClickListner listner){
    this.context = context;
    this.pageArr = pageArr;
    this.onButtonClickListner=listner;
    inflater = ((Activity) context).getLayoutInflater();
}

现在,您只需要从onButtonClickListner.OnButtonClick();之类的适配器中调用此侦听器方法,就会调用popup的方法,并且可以轻松关闭对话框。如果您需要传递视图页面的位置,也可以传递参数。

read more button的点击事件,如下所示:

readMoreBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onButtonClickListner.OnButtonClick();
        }
    });

希望这会对您有所帮助。

快乐的编码.....