应用recyclerviewer时,工具栏正在消失

时间:2018-05-03 15:18:03

标签: android

我有这个方法,当它被称为工具栏消失时(回收者的文本在它上面),当我评论方法时,工具栏是可见的。 你可以帮忙解决它吗?

private void initViews() {

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.card_recycler_view);
    recyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());

    recyclerView.setLayoutManager(layoutManager);

    mydb = new DbHandler(this);

    ArrayList<ListMainItem> listmainitem = mydb.getAllItemss("N");
    ArrayList<ListMainItem> listmainitemheader = mydb.getAllItemss("Y");

    MainItemsRCVAdapter mainitemadRCVapter = new MainItemsRCVAdapter(MainActivity.this, listmainitem, listmainitemheader);
    recyclerView.setAdapter(mainitemadRCVapter);


}

这是主要活动

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <android.support.v7.widget.RecyclerView
        android:id="@+id/card_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/app_bar_main"/>
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />


</android.support.v4.widget.DrawerLayout>

这是mainitem标题

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:background="@color/colorPrimaryDark"
    android:layout_height="255dp">

        <TextView
            android:id="@+id/toptext"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:textSize="23dp"
            android:text="New Games"
            android:background="@color/colorAccent"
            android:textColor="@color/colorText"
            android:gravity="center"
            android:layout_weight="0.7"
            />


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view_horizontal"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="185dp"
        android:layout_below="@+id/toptext"
        android:paddingTop="8dp">
    </android.support.v7.widget.RecyclerView>
    <TextView
        android:id="@+id/bottomtext"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:textSize="23dp"
        android:text="Used Games"
        android:layout_below="@+id/recycler_view_horizontal"
        android:background="@color/colorAccent"
        android:textColor="@color/colorText"
        android:gravity="center" />
</RelativeLayout>

这是Mainitemrecyclerview适配器

public class MainItemsRCVAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final Context mContext;
    private ArrayList<ListMainItem> itemMain;
    private ArrayList<ListMainItem> itemHeader;
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;

    public MainItemsRCVAdapter( Context context ,ArrayList<ListMainItem> listMainItem ,ArrayList<ListMainItem> listMainItemHeader) {
        setItemHeader(listMainItemHeader);
        setItemMain(listMainItem);

        mContext = context;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            //inflate your layout and pass it to view holder
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_item_adapter, parent, false);

            MainItemsViewholder vh = new MainItemsViewholder(v, new MainItemsViewholder.IMyViewHolderClicks() {
                public void onPotato(View caller) { Log.d("VEGETABLES", "Poh-tah-tos");
                    Intent intent = new Intent(mContext,SingleItemActivity.class);
                    mContext.startActivity(intent);
                };
                public void onTomato(ImageView callerImage) {
                    Log.d("VEGETABLES", "To-m8-tohs");


                    Intent intent = new Intent(mContext,SingleItemActivity.class);
                    mContext.startActivity(intent);
                }
            });
            return new MyViewHolder(v);
        } else if (viewType == TYPE_HEADER) {
            //inflate your layout and pass it to view holder
            View v =  LayoutInflater.from(parent.getContext()).inflate(R.layout.main_item_header, parent, false);

            return new MyViewHolderHeader(v);
        }
        throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            String name = getItemMain().get(position-1).geImgName();
            Integer price = getItemMain().get(position-1).getPrice();
            String convert_price = "Price: " + price.toString() + "$";

            Log.d("###", "Setting name: " + name);
            Log.d("###", "Setting URL " + name);
            Log.d("###", "Setting price " + price + "p" + convert_price);
            ((MyViewHolder) holder).getmDataTextView().setText(name);
            ((MyViewHolder) holder).getmPriceTextView().setText(convert_price);

/*            Picasso.with(mContext)
                    .load(R.drawable.img1)
                    .into(((MyViewHolder) holder).getmDataImageView());*/
            Picasso.with(mContext)
                    .load(R.drawable.ww2)
                    .into(((MyViewHolder) holder).getmDataImageView());
        } else if (holder instanceof MyViewHolderHeader) {
            //cast holder to VHHeader and set data for header.
            Log.d("####", "HEADER");
        }
    }
    private class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView mDataTextView;
        private  TextView mPriceTextView;

        private  ImageView mDataImageView;
        private  RecyclerView mainViewRecyclerViewItems;


        public MyViewHolder(View v) {
            super(v);

/*            mainViewRecyclerViewItems = (RecyclerView) v.findViewById(R.id.main_recycler_view);
            MainItemsAdapter mAdapter = new MainItemsAdapter(getDataHorizontal(),mContext);
            LinearLayoutManager layoutManager  = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);

            mainViewRecyclerViewItems.setLayoutManager(layoutManager);
            mainViewRecyclerViewItems.setAdapter(mAdapter);*/

            mDataTextView = (TextView) v.findViewById(R.id.gamename_mg);
            mPriceTextView=(TextView) v.findViewById(R.id.price_mg);
            mDataImageView = (ImageView) v.findViewById(R.id.img_mg);

        }
        public TextView getmPriceTextView() {
            return mPriceTextView;
        }

        public TextView getmDataTextView() {
            return mDataTextView;
        }
        public ImageView getmDataImageView()
        {
            return mDataImageView;
        }
    }

    private class MyViewHolderHeader extends RecyclerView.ViewHolder {
        private final RecyclerView mHeaderRecyclerView;

        public MyViewHolderHeader(View v) {
            super(v);

            mHeaderRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view_horizontal);
            HeaderItemsAdapter mAdapter = new HeaderItemsAdapter(getItemHeader(),mContext);
            LinearLayoutManager layoutManager
                    = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);

            mHeaderRecyclerView.setLayoutManager(layoutManager);
            mHeaderRecyclerView.setAdapter(mAdapter);
        }
    }

    @Override
    public int getItemCount() {
        return itemMain.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (isPositionHeader(position)) {
            return TYPE_HEADER;
        }

        return TYPE_ITEM;
    }

    private boolean isPositionHeader(int position) {
        return position == 0;
    }

    public void setItemMain(ArrayList<ListMainItem> itemmain) {
        this.itemMain = itemmain;
    }

    public void setItemHeader(ArrayList<ListMainItem> itemheader) {
        this.itemHeader = itemheader;
    }

    public ArrayList<ListMainItem> getItemHeader() {
        return itemHeader;
    }



    public ArrayList<ListMainItem> getItemMain() {
        return itemMain;
    }



}

编辑:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

   <!-- <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />-->

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/card_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"/>
    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />


</android.support.v4.widget.DrawerLayout>

1 个答案:

答案 0 :(得分:1)

DrawerLayout的工作方式 - 在其内部,您将定义两个布局。第一个将用作主要内容,第二个是抽屉的内容。

当您将框架布局声明为内容并将工具栏和回收者视图放在那里时,它们会重叠(即使没有抽屉也会发生同样的事情)。从FrameLayout上的Android文档:&#34;子视图以堆栈形式绘制,最近添加的子项位于顶部。&#34;

FrameLayout替换为RelativeLayout(您甚至已经android:layout_below="@+id/toolbar")或LinearLayout,它应该有效。