首页 > 解决方案 > 如何从android中的另一个视图访问组件

问题描述

我创建了一个自定义对话框,并在其中创建了一个视图页面,它们都有不同的布局文件。显示对话框的方法在 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();
        }
    });
}

片段覆盖:

<?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>

我的自定义对话框:

<?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>

测试页适配器:

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

标签: androidandroid-layoutandroid-viewpagerandroid-view

解决方案


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

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();方法,弹出的方法就会调用,你会很容易地关闭对话框。如果您需要传递视图页面的位置,您也可以传递参数。

点击事件read more button如下:

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

希望这会帮助你。

快乐的编码......


推荐阅读