如何从Android中的另一个视图访问组件 [英] How to access component from another view in android

查看:64
本文介绍了如何从Android中的另一个视图访问组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a custom dialog and inside it i create a view page which both have different layout file. Method to show the dialog is in MainActivity.

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

I inflate a layout to my adapter class where i populate the content. In this layout there is a button. My question is "How can i access the button from the layout that i inflate in my adapter class from my MainActivity. The reason I want to do this is because I want to dismiss the dialog when the button is clicked.

谢谢.

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

Here is the code of what i have done so far.

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:

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:

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:

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

推荐答案

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

Nice question. You should have to use the interface. let's try this code.

public interface OnButtonClickListner {
  public void OnButtonClick();
}

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

Now You need to pass this interface in the adapter. like this

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

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

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

Now, You need to change the constructor of Viewpager adapter. like below:

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的方法,您便可以轻松关闭该对话框.如果您需要传递视图页面的位置,也可以传递参数.

Now you just need to call this listener method from adapter like onButtonClickListner.OnButtonClick(); and the method of popup will call and you will easily dismiss the dialog. you may also pass the argument if you need to require to pass the position of view page.

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

Click event of read more button like below :

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

希望这会对您有所帮助.

Hope this will help you.

快乐的编码.....

Happy coding.....

这篇关于如何从Android中的另一个视图访问组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆