来自Imageview的Toast消息(Cardview布局)

时间:2018-01-29 17:48:30

标签: android android-layout imageview android-cardview cardview

尝试使用不同的方式设置toast时单击imageview但它不会出现(当单击图像视图时它设置为壁纸)我提供了主要活动和自定义适配器。有人可以帮我这个....

主要活动

public class MainActivity extends AppCompatActivity {

private static final int PERMISSION_REQUEST_CODE = 1000 ;

private static final String TAG = "MainActivity";

private AdView mAdView;

ImageView imageView;


RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;

List<Item> items;
CustomAdapter adapter;
private long backpressedtime;




@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode)
    {
        case PERMISSION_REQUEST_CODE:
        {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(this,"Permission Denied", Toast.LENGTH_SHORT).show();
            }
            break;

        }
    }




private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String str="Click on Wallpaper to set  Wallpaper";
    Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
    imageView=(ImageView)findViewById(R.id.imageView);
    mDrawerLayout=(DrawerLayout) findViewById(R.id.dl);
    mToggle=new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    NavigationView navigationView = (NavigationView) findViewById(R.id.Navigation_v);
    setupDrawerContent(navigationView);
    ActionBar actionBar = getSupportActionBar();

    recyclerView =(RecyclerView)findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    imageView=(ImageView)findViewById(R.id.imageView);




    initItem();






    //start service and play music
    startService(new Intent(MainActivity.this, SoundService.class));






}
public void toast(View v) {
    Toast.makeText(MainActivity.this, "Wallpaper Set", Toast.LENGTH_LONG).show();
}
private void  initItem() {

    items = new ArrayList<>();

    items.add(new Item(0,"Wide","https://images8.alphacoders.com/532/thumb-1920-532407.jpg"));
    items.add(new Item(1, "Wide","https://images5.alphacoders.com/394/thumb-1920-394511.jpg"));
    items.add(new Item(1,"Wide","https://images5.alphacoders.com/408/thumb-1920-408539.jpg"));
    items.add(new Item(1,"Wide","https://images6.alphacoders.com/511/thumb-1920-511799.jpg"));
    items.add(new Item(0,"Fixed","https://initiate.alphacoders.com/images/465/cropped-1080-1920-465882.jpg?4943"));
    items.add(new Item(1,"Wide","https://images7.alphacoders.com/360/thumb-1920-360290.jpg"));
    items.add(new Item(0,"Wide","https://images.alphacoders.com/244/thumb-1920-244621.jpg"));
    items.add(new Item(0,"Wide","https://images5.alphacoders.com/429/thumb-1920-429393.jpg"));
    items.add(new Item(1,"Wide","https://images2.alphacoders.com/270/thumb-1920-270040.jpg"));
    items.add(new Item(1,"Wide","https://images2.alphacoders.com/557/thumb-1920-557022.jpg"));
    items.add(new Item(2,"Wide","https://images7.alphacoders.com/556/thumb-1920-556876.jpg"));
    items.add(new Item(0,"Wide","https://images.alphacoders.com/712/thumb-1920-712830.jpg"));
    items.add(new Item(1,"Wide","https://images5.alphacoders.com/414/thumb-1920-414512.jpg"));
    items.add(new Item(1,"Wide","https://images5.alphacoders.com/446/thumb-1920-446450.jpg"));
    items.add(new Item(1,"Wide","https://images7.alphacoders.com/527/thumb-1920-527526.png"));
    items.add(new Item(0,"Wide","https://images5.alphacoders.com/556/thumb-1920-556869.jpg"));
    items.add(new Item(0,"Wide","https://images.alphacoders.com/292/thumb-1920-292661.jpg"));
    items.add(new Item(0,"Wide","https://images7.alphacoders.com/556/thumb-1920-556872.jpg"));
    items.add(new Item(0,"Wide","https://images3.alphacoders.com/566/thumb-1920-566597.jpg"));
    items.add(new Item(1,"Wide","https://images.alphacoders.com/646/thumb-1920-646577.jpg"));
    items.add(new Item(1,"Wide","https://images8.alphacoders.com/455/thumb-1920-455578.jpg"));
    items.add(new Item(0,"Wide","https://images2.alphacoders.com/665/thumb-1920-665807.jpg"));
    items.add(new Item(0,"Fixed","https://initiate.alphacoders.com/images/566/cropped-1125-2436-566597.jpg?5615"));
    items.add(new Item(0,"Fixed","https://initiate.alphacoders.com/images/532/cropped-1080-1920-532407.jpg?3026"));
    items.add(new Item(0,"Wide","https://images7.alphacoders.com/416/thumb-1920-416733.jpg"));
    items.add(new Item(0,"Wide","https://images3.alphacoders.com/553/thumb-1920-553530.jpg"));
    items.add(new Item(0,"Fixed","https://initiate.alphacoders.com/images/712/cropped-1080-1920-712830.jpg?7992"));
    items.add(new Item(0,"Wide","https://images7.alphacoders.com/556/thumb-1920-556864.jpg"));
    items.add(new Item(0,"Wide","https://images6.alphacoders.com/418/thumb-1920-418866.jpg"));
    items.add(new Item(0,"Wide","https://images6.alphacoders.com/338/thumb-1920-338418.jpg"));
    items.add(new Item(0,"Wide","https://images7.alphacoders.com/338/thumb-1920-338412.jpg"));
    items.add(new Item(0,"Wide","https://images2.alphacoders.com/532/thumb-1920-532391.jpg"));
    items.add(new Item(0,"Fixed","https://initiate.alphacoders.com/images/338/cropped-1125-2436-338412.jpg?3814"));
    items.add(new Item(0,"Wide","https://images8.alphacoders.com/684/thumb-1920-684286.jpg"));
    items.add(new Item(0,"Fixed","https://initiate.alphacoders.com/images/532/cropped-1125-2436-532391.jpg?8831"));
    items.add(new Item(0,"Wide","https://images3.alphacoders.com/566/thumb-1920-566596.jpg"));
    items.add(new Item(0,"Wide","https://images6.alphacoders.com/416/thumb-1920-416109.png"));
    items.add(new Item(0,"Fixed","https://initiate.alphacoders.com/images/566/cropped-1080-1920-566596.jpg?6441"));
    items.add(new Item(0,"Fixed","https://initiate.alphacoders.com/images/416/cropped-1080-1920-416109.png?5716"));
    items.add(new Item(0,"Wide","https://images2.alphacoders.com/446/thumb-1920-446459.png"));
    items.add(new Item(0,"Wide","https://images6.alphacoders.com/446/thumb-1920-446461.png"));
    items.add(new Item(0,"Wide","https://images2.alphacoders.com/743/thumb-1920-743467.jpg"));
    items.add(new Item(0,"Fixed","https://initiate.alphacoders.com/images/446/cropped-1080-1920-446461.png?6653"));
    items.add(new Item(0,"Wide","https://images3.alphacoders.com/711/thumb-1920-711797.jpg"));
    items.add(new Item(0,"Fixed","https://initiate.alphacoders.com/images/743/cropped-1080-1920-743467.jpg?4344"));
    items.add(new Item(0,"Wide","https://images7.alphacoders.com/549/thumb-1920-549028.jpg"));
    items.add(new Item(0,"Fixed","https://initiate.alphacoders.com/images/549/cropped-1080-1920-549028.jpg?1365"));
    items.add(new Item(0,"Wide","https://images2.alphacoders.com/867/thumb-1920-867581.jpg"));
    items.add(new Item(0,"Wide","https://images6.alphacoders.com/532/thumb-1920-532782.jpg"));
    items.add(new Item(0,"Wide","https://images.alphacoders.com/556/thumb-1920-556862.jpg"));
    items.add(new Item(0,"Wide","https://images7.alphacoders.com/785/thumb-1920-785015.jpg"));
    items.add(new Item(0,"Fixed","https://initiate.alphacoders.com/images/785/cropped-1080-1920-785015.jpg?8463"));
    items.add(new Item(0,"Wide","https://images7.alphacoders.com/713/thumb-1920-713178.jpg"));
    items.add(new Item(0,"Fixed","https://initiate.alphacoders.com/images/532/cropped-1080-1920-532415.jpg?6884"));
    items.add(new Item(0,"Wide","https://images.alphacoders.com/525/thumb-1920-525415.jpg"));
    items.add(new Item(0,"Wide","https://images7.alphacoders.com/556/thumb-1920-556858.jpg"));
    items.add(new Item(0,"Fixed","https://initiate.alphacoders.com/images/771/cropped-1080-1920-771663.jpg?3059"));
    items.add(new Item(0,"Wide","https://images6.alphacoders.com/418/thumb-1920-418867.jpg"));
    items.add(new Item(0,"Wide","https://images7.alphacoders.com/556/thumb-1920-556871.jpg"));
    items.add(new Item(0,"Wide","https://images5.alphacoders.com/612/thumb-1920-612095.jpg"));
    items.add(new Item(0,"Wide","https://images6.alphacoders.com/712/thumb-1920-712832.jpg"));
    items.add(new Item(0,"Wide","https://images.alphacoders.com/465/thumb-1920-465882.jpg"));
    items.add(new Item(0,"Fixed","https://initiate.alphacoders.com/images/876/cropped-1080-1920-876467.jpg?1195"));
    items.add(new Item(0,"Wide","https://images4.alphacoders.com/807/thumb-1920-807741.jpg"));
    items.add(new Item(0,"Wide","https://images8.alphacoders.com/749/thumb-1920-749350.png"));
    items.add(new Item(0,"Wide","https://images5.alphacoders.com/585/thumb-1920-585994.png"));
    items.add(new Item(0,"Wide","https://images5.alphacoders.com/556/thumb-1920-556844.jpg"));
    items.add(new Item(0,"Wide","https://images8.alphacoders.com/532/thumb-1920-532400.jpg"));
    items.add(new Item(0,"Wide","https://images5.alphacoders.com/532/thumb-1920-532397.jpg"));
    items.add(new Item(0,"Fixed","https://initiate.alphacoders.com/images/532/cropped-1125-2436-532397.jpg?4991"));
    items.add(new Item(0,"Wide","https://images.alphacoders.com/532/thumb-1920-532396.jpg"));
    items.add(new Item(0,"Wide","https://images2.alphacoders.com/526/thumb-1920-526956.jpg"));
    items.add(new Item(0,"Fixed","https://initiate.alphacoders.com/images/771/cropped-1080-1920-771665.jpg?511"));
    items.add(new Item(0,"Wide","https://images.alphacoders.com/527/thumb-1920-527267.png"));
    items.add(new Item(0,"Wide","https://images8.alphacoders.com/434/thumb-1920-434313.jpg"));
    items.add(new Item(0,"Wide","https://images5.alphacoders.com/527/thumb-1920-527265.png"));
    items.add(new Item(0,"Wide","https://images6.alphacoders.com/418/thumb-1920-418868.jpg"));
    items.add(new Item(0,"Wide","https://images6.alphacoders.com/854/thumb-1920-854137.jpg"));
    items.add(new Item(0,"Wide","https://images2.alphacoders.com/556/thumb-1920-556846.jpg"));
    items.add(new Item(0,"Wide","https://images.alphacoders.com/649/thumb-1920-649420.jpg"));
    items.add(new Item(0,"Wide","https://images5.alphacoders.com/338/thumb-1920-338419.jpg"));





    adapter = new CustomAdapter(this,items);
    recyclerView.setAdapter(adapter);





}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

protected void onDestroy() {
    //stop service and stop music
    stopService(new Intent(MainActivity.this, SoundService.class));
    super.onDestroy();
}
public void selectItemDrawer(MenuItem menuItem){
    Fragment myFragment = null;
    Class fragmentClass;
    switch (menuItem.getItemId()) {

        case  R.id.walkthrough:
            fragmentClass = Walkthrough.class;
            break;

        case R.id.info:
            fragmentClass = About.class;
            break;
        default:
            fragmentClass = Walkthrough.class;


    }
    try {
        myFragment = (Fragment) fragmentClass.newInstance();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.flcontent,myFragment).commit();
    setTitle(menuItem.getTitle());
    mDrawerLayout.closeDrawers();







}
private void setupDrawerContent(NavigationView navigationView) {
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            selectItemDrawer(item);
            return true;
        }
    });
}




@Override
public void onBackPressed() {


    final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setMessage("Are you sure you want to exit?");
    builder.setCancelable(true);
    builder.setNegativeButton("No Stay ;-)", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.cancel();
        }
    });
    builder.setPositiveButton("Yes :'-(", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });
    AlertDialog alertDialog = builder.create();
    alertDialog.show();

}

自定义适配器

公共类CustomAdapter扩展了RecyclerView.Adapter {

Context context;
List<Item> itemList;



public CustomAdapter(Context context, List<Item> itemList) {
    this.context = context;
    this.itemList = itemList;
}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false);
    return new CustomViewHolder(itemView);

}

@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
    Item item = itemList.get(position);
    if (item.type == 0) { //new
        holder.ribbonLayout.setShowBottom(true);
        holder.ribbonLayout.setShowBottom(true);


        holder.ribbonLayout.setHeaderTextColor(Color.parseColor("white"));

        holder.ribbonLayout.setHeaderText(item.headerText);
        Picasso.with(context).load(item.imageURL)
                .into(holder.imageView);
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
        public void onClick(View view) {

            try {
                Bitmap bitmap = ((BitmapDrawable)((ImageView)view).getDrawable()).getBitmap();
                WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
                wallpaperManager.setBitmap(bitmap);
            } catch (Exception e) {
                e.printStackTrace();

            }

        }
    });
    } else
        if (item.type == 1) { //hot
        holder.ribbonLayout.setShowBottom(false);
        holder.ribbonLayout.setShowBottom(false);

            holder.ribbonLayout.setHeaderTextColor(Color.parseColor("white"));



            holder.ribbonLayout.setHeaderText(item.headerText);
        Picasso.with(context).load(item.imageURL)
                .into(holder.imageView);
            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    try {
                        Bitmap bitmap = ((BitmapDrawable)((ImageView)view).getDrawable()).getBitmap();
                        WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
                        wallpaperManager.setBitmap(bitmap);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
    }




}





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

}

1 个答案:

答案 0 :(得分:0)

您可以使用present(_:animated:completion:)对象

presentViewConroller设置吐司
Custom Adapter