在棒棒糖和kitkat版本中工作正常但在棉花糖中引起问题

时间:2017-08-23 17:58:11

标签: android android-6.0-marshmallow android-4.4-kitkat

我一直致力于该项目..使用recyleview创建主菜单,其中cardview显示带滑动的图像。但菜单在棒棒糖和kitkat安卓版本上工作正常,但在棉花糖和Android版本以上引起问题。以下是kitkat,棒棒糖手机和棉花糖菜单的截图。寻找解决方案..希望我能找到一些......

在Marshmallow

在Lollipop和kitkat安卓版中,它运行顺畅

我的主要活动代码:

package com.example.hamdani.relativelayout;

import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.View;

import com.example.hamdani.relativelayout.menu.Menu;
import com.example.hamdani.relativelayout.menu.MenuAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainMenuActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private MenuAdapter adapter;
    private List<Menu> menuList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainmenu);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        initCollapsingToolbar();

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        menuList = new ArrayList<>();
        adapter = new MenuAdapter(this, menuList);

        RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);

        prepareAlbums();

//        try {
//            Glide.with(this).load(R.drawable.cover).into((ImageView) findViewById(R.id.backdrop));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    /**
     * Initializing collapsing toolbar
     * Will show and hide the toolbar title on scroll
     */
    private void initCollapsingToolbar() {
        final CollapsingToolbarLayout collapsingToolbar =
                (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        collapsingToolbar.setTitle(" ");
        AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
        appBarLayout.setExpanded(true);

        // hiding & showing the title when toolbar expanded & collapsed
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            boolean isShow = false;
            int scrollRange = -1;

            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (scrollRange == -1) {
                    scrollRange = appBarLayout.getTotalScrollRange();
                }
                if (scrollRange + verticalOffset == 0) {
                    collapsingToolbar.setTitle(getString(R.string.app_name));
                    isShow = true;
                } else if (isShow) {
                    collapsingToolbar.setTitle(" ");
                    isShow = false;
                }
            }
        });
    }

    /**
     * Adding few albums for testing
     */
    private void prepareAlbums() {
        int[] covers = new int[]{
                R.drawable.album1,
                R.drawable.album2,
                R.drawable.album3,
                R.drawable.album4,
                R.drawable.album5,
                R.drawable.album6,
                R.drawable.album7,
                R.drawable.album8};

        Menu a = new Menu(covers[0]);
        menuList.add(a);

        a = new Menu(covers[1]);
        menuList.add(a);

        a = new Menu(covers[2]);
        menuList.add(a);

        a = new Menu(covers[3]);
        menuList.add(a);

        a = new Menu(covers[4]);
        menuList.add(a);

        a = new Menu(covers[5]);
        menuList.add(a);

        adapter.notifyDataSetChanged();
    }

    /**
     * RecyclerView item decoration - give equal margin around grid item
     */
    public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {

        private int spanCount;
        private int spacing;
        private boolean includeEdge;

        public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
            this.spanCount = spanCount;
            this.spacing = spacing;
            this.includeEdge = includeEdge;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int position = parent.getChildAdapterPosition(view); // item position
            int column = position % spanCount; // item column

            if (includeEdge) {
                outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
                outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

                if (position < spanCount) { // top edge
                    outRect.top = spacing;
                }
                outRect.bottom = spacing; // item bottom
            } else {
                outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
                outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
                if (position >= spanCount) {
                    outRect.top = spacing; // item top
                }
            }
        }
    }

    /**
     * Converting dp to pixel
     */
    private int dpToPx(int dp) {
        Resources r = getResources();
        return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
    }

    @Override
    public void onBackPressed() {


        if (InterstitialAdClass.mInterstitialAd.isLoaded()) {
            InterstitialAdClass.mInterstitialAd.show();
        } else {

            final AlertDialog.Builder builder = new AlertDialog.Builder(MainMenuActivity.this);
            builder.setMessage("Are you want to quit the app");
            builder.setCancelable(true);
            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.cancel();
                }
            });


            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    moveTaskToBack(true);

                }
            });

            AlertDialog alertDialog = builder.create();
            alertDialog.show();

//            super.onBackPressed();
//      finish();


        }
    }
}

我的MenuAdapter代码

package com.example.hamdani.relativelayout.menu;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.hamdani.relativelayout.Calculators;
import com.example.hamdani.relativelayout.Centres;
import com.example.hamdani.relativelayout.Days;
import com.example.hamdani.relativelayout.Introduction;
import com.example.hamdani.relativelayout.Notes;
import com.example.hamdani.relativelayout.R;
import com.example.hamdani.relativelayout.Requirements;

import java.util.List;

/**
 * Created by Ravi Tamada on 18/05/16.
 */
public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.MyViewHolder> {

    private Context mContext;
    private List<Menu> menuList;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView title, count;
        public ImageView thumbnail, overflow;

        public MyViewHolder(View view) {
            super(view);
            thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
        }
    }


    public MenuAdapter(Context mContext, List<Menu> menuList) {
        this.mContext = mContext;
        this.menuList = menuList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.mainmenu_card, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final Menu menu = menuList.get(position);


        // loading menu cover using Glide library
        Glide.with(mContext).load(menu.getThumbnail()).into(holder.thumbnail);

        holder.thumbnail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (menu.getThumbnail()) {
                    case R.drawable.album1:
                        Intent intent=new Intent(mContext, Introduction.class);
                        mContext.startActivity(intent);
                        break;
                    case R.drawable.album2:
                        Intent intent1=new Intent(mContext, Requirements.class);
                        mContext.startActivity(intent1);
                        break;
                    case R.drawable.album3:
                        Intent intent2=new Intent(mContext, Days.class);
                        mContext.startActivity(intent2);

                        break;
                    case R.drawable.album4:
                        Intent intent3=new Intent(mContext, Notes.class);
                        mContext.startActivity(intent3);

                        break;
                    case R.drawable.album5:
                        Intent intent4=new Intent(mContext, Calculators.class);
                        mContext.startActivity(intent4);

                        break;
                    case R.drawable.album6:
                        Intent intent5=new Intent(mContext, Centres.class);
                        mContext.startActivity(intent5);

                        break;
                    default:
                        Toast.makeText(mContext, "Picture other", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return menuList.size();
    }
}

我的菜单代码

package com.example.hamdani.relativelayout.menu;

/**
 * Created by Lincoln on 18/05/16.
 */
public class Menu {
    private int thumbnail;

    public Menu() {
    }

    public Menu(int thumbnail) {
        this.thumbnail = thumbnail;
    }



    public int getThumbnail() {
        return thumbnail;
    }

    public void setThumbnail(int thumbnail) {
        this.thumbnail = thumbnail;
    }
}

0 个答案:

没有答案