在其他布局文件

时间:2018-03-30 19:23:22

标签: java android

我有一个活动,MainActivityNavigationDrawer。因此,当我点击NavigationDrawer中的第一个项目时,我会在Fragment中打开MainActivity。在此Fragment中,我使用ImageView来扩充布局。但是当我点击ImageView时,它不起作用。你可以解释一下,如果可能的话,为什么它不起作用?感谢。

这是onClick中的Fragment实施。

   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_stream, container, false);

    mItemsContainer = (SwipeRefreshLayout) rootView.findViewById(R.id.container_items);
    mItemsContainer.setOnRefreshListener(this);

    //Pub perso
    View rootViewx = inflater.inflate(R.layout.ad_item, container, false);
    ImageView imgFavorite = rootViewx.findViewById(R.id.adviewperso);
    imgFavorite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String url = "MYURL";

            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);


            Toast.makeText(getActivity(),
                    "Pub appuyé !",
                    Toast.LENGTH_LONG).show();
        }
    });

    //TEST2
    CardView cardtest = rootViewx.findViewById(R.id.adCard);
    cardtest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String url = "MYURL";

            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);


            Toast.makeText(getActivity(),
                    "Pub appuyé !",
                    Toast.LENGTH_LONG).show();
        }
    });
    //TEST2
    //Fin pub perso


    mMessage = (TextView) rootView.findViewById(R.id.message);

    mFabButton = (FloatingActionButton) rootView.findViewById(R.id.fabButton);
    mFabButton.setImageResource(R.drawable.ic_action_new);

    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);

    itemsAdapter.setOnMoreButtonClickListener(new AdvancedItemListAdapter.OnItemMenuButtonClickListener() {

        @Override
        public void onItemClick(View v, Item obj, int actionId, int position) {

            switch (actionId) {

                case R.id.action_repost: {

                    if (obj.getFromUserId() != App.getInstance().getId()) {

                        if (obj.getRePostFromUserId() != App.getInstance().getId()) {

                            repost(position);

                        } else {

                            Toast.makeText(getActivity(), getActivity().getString(R.string.msg_not_make_repost), Toast.LENGTH_SHORT).show();
                        }

                    } else {

                        Toast.makeText(getActivity(), getActivity().getString(R.string.msg_not_make_repost), Toast.LENGTH_SHORT).show();
                    }

                    break;
                }

                case R.id.action_share: {

                    onPostShare(position);

                    break;
                }

                case R.id.action_report: {

                    report(position);

                    break;
                }

                case R.id.action_remove: {

                    remove(position);

                    break;
                }
            }
        }
    });

    final GridLayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 1);

    mRecyclerView.setLayoutManager(mLayoutManager);

    mRecyclerView.setAdapter(itemsAdapter);

    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

            if(dy > 0) {

                visibleItemCount = mLayoutManager.getChildCount();
                totalItemCount = mLayoutManager.getItemCount();
                pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();

                if (!loadingMore) {

                    if ((visibleItemCount + pastVisiblesItems) >= totalItemCount && (viewMore) && !(mItemsContainer.isRefreshing())) {

                        loadingMore = true;
                        Log.e("...", "Last Item Wow !");

                        getItems();
                    }
                }
            }
        }
    });

    mFabButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(getActivity(), NewItemActivity.class);
            startActivityForResult(intent, STREAM_NEW_POST);
        }
    });

    if (itemsAdapter.getItemCount() == 0) {

        showMessage(getText(R.string.label_empty_list).toString());

    } else {

        hideMessage();
    }

    if (!restore) {

        showMessage(getText(R.string.msg_loading_2).toString());

        getItems();
    }

    return rootView;

}

包含adviewperso的布局如下所示。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:background="#F5F5F5"
    android:clickable="true"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:id="@+id/adCard"
        app:cardCornerRadius="@dimen/spacing_medium"
        app:cardElevation="@dimen/spacing_xsmall"
        app:cardUseCompatPadding="true">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:padding="10dp">

                <ImageView
                    android:layout_width="15dp"
                    android:layout_height="15dp"
                    android:tint="@color/sponsored"
                    android:src="@drawable/ic_sponsored"
                    android:visibility="visible" />

                <TextView
                    android:id="@+id/adTitle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="3dp"
                    android:text="@string/label_sponsored"
                    android:textAppearance="@style/TextAppearance.AppCompat.Caption"
                    android:textColor="@color/sponsored"
                    android:textStyle="normal"
                    android:visibility="visible" />
            </LinearLayout>

            <RelativeLayout
                android:minHeight="64dp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_horizontal"
                android:paddingBottom="10dp"
                android:paddingTop="10dp">

                <ImageView
                    android:id="@+id/adviewperso"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    ads:srcCompat="@drawable/pub" />

                <com.google.android.gms.ads.NativeExpressAdView
                    android:id="@+id/adView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    ads:adUnitId="@string/banner_ad_unit_id"
                    android:visibility="invisible"
                    ads:adSize="320x150">
                </com.google.android.gms.ads.NativeExpressAdView>
            </RelativeLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>    
</LinearLayout>

编译或运行应用程序时,我没有任何错误。

1 个答案:

答案 0 :(得分:0)

我会建议两件事。从布局中点击ImageView,我认为AdViewImageView重叠,这使得它无法点击。

因此RelativeLayout下的观点应如下所示。

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:minHeight="64dp"
    android:paddingBottom="10dp"
    android:paddingTop="10dp">

    <ImageView
        android:id="@+id/adviewperso"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        ads:srcCompat="@drawable/ic_add" />

    <com.google.android.gms.ads.NativeExpressAdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/adviewperso"
        android:visibility="invisible"
        ads:adSize="320x150"
        ads:adUnitId="@string/banner_ad_unit_id"/>
</RelativeLayout>

<强>更新

您将从rootView返回onCreateView。您无法在单个Fragment中扩充多个布局。因此,如果您没有从View rootViewx = inflater.inflate(R.layout.ad_item, container, false);返回相同的布局,则使用onCreateView进行充气的布局将不起作用。

我希望您的ImageView布局中包含fragment_stream,或者它包含ad_item布局。因此,如果ad_item布局中包含fragment_stream布局,那么您可以轻松获取与布局相关联的视图ID,而无需单独对其进行充气。

如果您有两个Fragment,那么您需要两个单独的Fragment课程,分别为ad_itemfragment_stream充气。然后,您使用另一个Fragment作为这两个片段的持有者将它们连接在一起。