【发布时间】:2019-01-23 06:13:00
【问题描述】:
我创建了一个自定义对话框,并在其中创建了一个视图页面,它们都有不同的布局文件。显示对话框的方法在 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);
}
}
【问题讨论】:
-
您不应该从您的活动中访问适配器中膨胀的按钮。与它相关的所有内容都应在适配器本身中进行管理。为什么要访问它?
-
如果适配器在
RecyclerView内,那么您可以使用它的onTouchListener以某种方式获得Button。为什么需要从 MainActivity 访问适配器的Button?可能还有其他一些方法可以做得更好。 -
原因是我想关闭我在 mainactivity 中创建的对话框。该对话框从主活动中显示,然后当用户单击适配器中的按钮时,该对话框被关闭。顺便说一句,我使用视图寻呼机适配器。
-
你在哪里调用 ShowPromoOverlay ?
-
我在 MainActivity 类中调用 ShowPromotionOverlay。
标签: android android-layout android-viewpager android-view