点击RecyclerView瀑布流布局中的图片item跳转到下一个activity

图片说明

这是activity

package com.example.listview.Menu;

import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.listview.R;
import com.example.listview.Menu.MenuActivity;
import com.example.listview.Pay.PayActivity;
import com.example.listview.Menu.MenuAdapter;
import com.example.listview.table.TMenu;
import com.example.listview.Windows;
import org.litepal.crud.DataSupport;
import java.util.ArrayList;
import java.util.List;

public class MenuActivity extends AppCompatActivity {

    private MenuAdapter menuAdapter;
    private RecyclerView mRv;
    //private OnItemClickListener mOnItemClickListener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu_activity);
        ActionBar actionBar=getSupportActionBar();
        if(actionBar!=null){
            actionBar.hide();
        }
        initMenus();//初始化数据
        RecyclerView recyclerView=(RecyclerView) findViewById(R.id.rv);
        StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);//P128瀑布流布局
        recyclerView.setLayoutManager(layoutManager);
        MenuAdapter madapter=new MenuAdapter(menuList);
        recyclerView.setAdapter(madapter);

        madapter.setOnItemClick(new MenuAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View v, int position, String id) {
                Intent intent = new Intent(MenuActivity.this, PayActivity.class);
               /*传ID在Main2Activity接受*/
               /* intent.putExtra("id",id);*/
                startActivity(intent);
            }
        });
        /*adapter.setOnItemClickListener(new MenuAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //String masterPic = show.getResult().get(position).getMasterPic();
                //String name = show.getResult().get(position).getCommodityName();
                //String price =show.getResult().get(position).getPrice();
                Intent intent = new Intent(MenuActivity.this, PayActivity.class);
                //intent.putExtra("cid",masterPic);
                //intent.putExtra("name",name);
                //intent.putExtra("price",price);
                startActivity(intent);
            }
        });*/
    }

    private List<Menu> menuList =new ArrayList<>();
    private void initMenus() {
        List<TMenu> menus = DataSupport.select("name", "price","store","foodid","picture").find(TMenu.class);
        for (TMenu menu : menus) {
            int resID = getResources().getIdentifier(menu.getPicture(), "drawable", "com.example.listview");
            Menu canteen=new Menu(menu.getName(),resID);
            menuList.add(canteen);
        }
    }
}

这是Adapter

package com.example.listview.Menu;

/**
 * Created by Administrator on 2019/4/3 0003.
 */

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.example.listview.R;
import com.example.listview.Pay.PayActivity;
import com.example.listview.Menu.Menu;
import com.example.listview.Menu.MenuAdapter;

import java.util.List;

/**
 * Created by Administrator on 2019/2/8 0008.
 */

public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.ViewHolder>{
    private List<Menu> mMenuList;
    private Context mcontext;
    private OnItemClickListener mOnItemClickListener;
    OnItemClickListener listener;

    public interface OnItemClickListener {
        public void onItemClick(View v, int position);
    }

    public void setOnItemClick(OnItemClickListener listener) {
        this.listener=listener;
    }
    static class ViewHolder extends RecyclerView.ViewHolder {
        View fruitView;
        ImageView fruitImage;
        TextView fruitName;
        public ViewHolder(View view) {
            super(view);
            fruitView = view;
            fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
            fruitName = (TextView) view.findViewById(R.id.fruit_name);
        }
    }
    public MenuAdapter(List<Menu> menuList) {
        mMenuList = menuList;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.fruitView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int position = holder.getAdapterPosition();
                Menu menu = mMenuList.get(position);
                //Intent intent=new Intent(MenuActivity.this,PayActivity.class);
                //startActivity(intent);
                Toast.makeText(v.getContext(), "you clicked view" + menu.getName(), Toast.LENGTH_SHORT).show();
            }
        });
        holder.fruitImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //mOnItemClickListener.onItemClick(v, position);
                //Intent intent=new Intent();
                //Intent intent=new Intent(Context,PayActivity.class);
                //intent.putExtra("friend",msg.getFriend().toString());
                //Context.startActivity(intent);
                //int position = holder.getAdapterPosition();
                //Menu menu = mMenuList.get(position);
                //Toast.makeText(v.getContext(), "you clicked image" + menu.getName(), Toast.LENGTH_SHORT).show();
            }
        });
        return holder;
    }
    @Override
    public void onBindViewHolder(ViewHolder holder,int position){
        Menu menu = mMenuList.get(position);
        holder.fruitImage.setImageResource(menu.getImageId());
        holder.fruitName.setText(menu.getName());
        holder.fruitImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!=null){
                    /*注意参数*/
                    listener.OnItemClick(v,position);
                }
            }
        });
    }
        @Override
    public int getItemCount(){
        return mMenuList.size();
    }
}






Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐