用setonclicklistener定位java绑定项

btxsgosb  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(337)

在我的 RecyclerView 有几个 items 当使用 setOnClickListener , Fragmet 打开,但有一个问题 positionitem 未连接到 setOnClickListener position 如果我做了分类和 positionitem 改变了,这个 positionsetOnClickListener 不是,而且学位就在第一位 item 永远都是一样的 Fragmet . 如何链接它们?正如我在属性所在的文件中所理解的,我需要在 setOnClickListener ,但这只是猜测。
帮助编写代码,或者至少是一个它应该是什么样子的例子。因为我一点也不明白
现在我和swith一起改变了适配器的位置。如何将它绑定到examplelist=new arraylist<>();?中的元素;?

@Override
    public void onBindViewHolder(AttractionsViewHolder holder, int position) {
        AttractionsItem currentItem = mFavList.get(position);
        holder.card_image_1.setImageResource(currentItem.getImg1());
        holder.card_image_2.setImageResource(currentItem.getImg2());
        holder.card_text_1.setText(currentItem.get_attraction_name());
        holder.card_text_2.setText(currentItem.get_attraction_type());
        holder.card_text_3.setText(currentItem.get_attraction_cost());

        holder.Card.setOnClickListener(v -> {
            switch (position) {
                case 0: {
                    FragmentBabyIsland fragment = new FragmentBabyIsland(); // you fragment
                    FragmentManager fragmentManager = ((FragmentActivity) v.getContext()).getSupportFragmentManager(); // instantiate your view context
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.animator.nav_default_enter_anim, R.animator.nav_default_exit_anim,
                            R.animator.nav_default_pop_enter_anim, R.animator.nav_default_pop_exit_anim);
                    fragmentTransaction.replace(R.id.fragment, fragment);// your container and your fragment
                    fragmentTransaction.addToBackStack("tag");
                    fragmentTransaction.commit();
                    break;
                }
                case 1: {
                    FragmentJumpAround fragment = new FragmentJumpAround(); // you fragment
                    FragmentManager fragmentManager = ((FragmentActivity) v.getContext()).getSupportFragmentManager(); // instantiate your view context
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.animator.nav_default_enter_anim, R.animator.nav_default_exit_anim,
                            R.animator.nav_default_pop_enter_anim, R.animator.nav_default_pop_exit_anim);
                    fragmentTransaction.replace(R.id.fragment, fragment);// your container and your fragment
                    fragmentTransaction.addToBackStack("tag");
                    fragmentTransaction.commit();
                    break;
                }
                case 2: {
                    FragmentMerryGoRound fragment = new FragmentMerryGoRound(); // you fragment
                    FragmentManager fragmentManager = ((FragmentActivity) v.getContext()).getSupportFragmentManager(); // instantiate your view context
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.animator.nav_default_enter_anim, R.animator.nav_default_exit_anim,
                            R.animator.nav_default_pop_enter_anim, R.animator.nav_default_pop_exit_anim);
                    fragmentTransaction.replace(R.id.fragment, fragment);// your container and your fragment
                    fragmentTransaction.addToBackStack("tag");
                    fragmentTransaction.commit();
                    break;
                }
                case 3: {
                    FragmentFiveDCinema fragment = new FragmentFiveDCinema(); // you fragment
                    FragmentManager fragmentManager = ((FragmentActivity) v.getContext()).getSupportFragmentManager(); // instantiate your view context
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.animator.nav_default_enter_anim, R.animator.nav_default_exit_anim,
                            R.animator.nav_default_pop_enter_anim, R.animator.nav_default_pop_exit_anim);
                    fragmentTransaction.replace(R.id.fragment, fragment);// your container and your fragment
                    fragmentTransaction.addToBackStack("tag");
                    fragmentTransaction.commit();
                    break;
                }
            }
        });
    }

这个 Fragment 其中 RecyclerView 位于

public class FragmentAttractionRecyclerView extends Fragment {

    private RecyclerView mRec;
    private AttractionsAdapter adapter;
    private ArrayList<AttractionsItem> exampleList;
    private RecyclerView.LayoutManager mLayoutManager;
    private String[]mArraysNames= new String[]{"Baby островок","Виражи","Вокруг света","5D кинотеатр"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_attraction_test_2, container, false);

    }

    @SuppressLint("InflateParams")
    @Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
        createExampleList();
        buildRecyclerView();

        ImageView sort_alphabet = requireView().findViewById(R.id.sort_alphabet);

        sort_alphabet.setOnClickListener(v -> {
            sortArray();
            sortArrayList();
        });
    }

    private void sortArray(){
        Arrays.sort(mArraysNames);
        adapter.notifyDataSetChanged();
    }
    private void sortArrayList(){
        Collections.sort(exampleList, (o100, o200) -> o100.get_attraction_cost().compareTo(o200.get_attraction_cost()));
        adapter.notifyDataSetChanged();

    }

    public void createExampleList() {
        exampleList = new ArrayList<>();
        exampleList.add(new AttractionsItem(R.mipmap.unnamed, R.drawable.ic_kid, "Baby островок", "Детский", "60₽"));
        exampleList.add(new AttractionsItem(R.mipmap.unnamed, R.drawable.ic_kid, "Виражи", "Детский", "80₽"));
        exampleList.add(new AttractionsItem(R.mipmap.unnamed, R.drawable.ic_kid, "Вокруг света", "Детский", "50₽"));
        exampleList.add(new AttractionsItem(R.mipmap.unnamed, R.drawable.ic_interactive, "5D кинотеатр", "Интерактивный", "120₽"));
   }

    public void buildRecyclerView() {
        mRec = requireView().findViewById(R.id.attraction_recycler);
        adapter = new AttractionsAdapter(exampleList);
        mLayoutManager = new LinearLayoutManager(getContext());
        mRec.setLayoutManager(mLayoutManager);
        mRec.setAdapter(adapter);
    }
}
``` `Adapter` ```
public class AttractionsAdapter extends RecyclerView.Adapter<AttractionsAdapter.AttractionsViewHolder> {
    public ArrayList<AttractionsItem> mFavList;

    public AttractionsAdapter(ArrayList<AttractionsItem> favList) {
        mFavList = favList;
    }

    @NonNull
    @Override
    public AttractionsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_attraction, parent, false);
        AttractionsViewHolder evh = new AttractionsViewHolder(v);
        return evh;

    }

    public static class AttractionsViewHolder extends RecyclerView.ViewHolder {
        public ImageView card_image_1, card_image_2;
        public TextView card_text_1, card_text_2, card_text_3, attraction_menu_1_1, attraction_menu_1_2;
        public CardView Card;

        public AttractionsViewHolder(View itemView) {
            super(itemView);
            card_image_1 = itemView.findViewById(R.id.card_image_1);
            card_image_2 = itemView.findViewById(R.id.card_image_2);
            card_text_1 = itemView.findViewById(R.id.card_text_1);
            card_text_2 = itemView.findViewById(R.id.card_text_2);
            card_text_3 = itemView.findViewById(R.id.card_text_3);
            attraction_menu_1_1 = itemView.findViewById(R.id.attraction_menu_1_1);
            attraction_menu_1_2 = itemView.findViewById(R.id.attraction_menu_1_2);
            Card = itemView.findViewById(R.id.Card);
        }
    }

    @Override
    public void onBindViewHolder(AttractionsViewHolder holder, int position) {
        AttractionsItem currentItem = mFavList.get(position);
        holder.card_image_1.setImageResource(currentItem.getImg1());
        holder.card_image_2.setImageResource(currentItem.getImg2());
        holder.card_text_1.setText(currentItem.get_attraction_name());
        holder.card_text_2.setText(currentItem.get_attraction_type());
        holder.card_text_3.setText(currentItem.get_attraction_cost());

        holder.Card.setOnClickListener(v -> {
            switch (position) {
                case 0: {
                    FragmentBabyIsland fragment = new FragmentBabyIsland(); // you fragment
                    FragmentManager fragmentManager = ((FragmentActivity) v.getContext()).getSupportFragmentManager(); // instantiate your view context
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.animator.nav_default_enter_anim, R.animator.nav_default_exit_anim,
                            R.animator.nav_default_pop_enter_anim, R.animator.nav_default_pop_exit_anim);
                    fragmentTransaction.replace(R.id.fragment, fragment);// your container and your fragment
                    fragmentTransaction.addToBackStack("tag");
                    fragmentTransaction.commit();
                    break;
                }
                case 1: {
                    FragmentJumpAround fragment = new FragmentJumpAround(); // you fragment
                    FragmentManager fragmentManager = ((FragmentActivity) v.getContext()).getSupportFragmentManager(); // instantiate your view context
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.animator.nav_default_enter_anim, R.animator.nav_default_exit_anim,
                            R.animator.nav_default_pop_enter_anim, R.animator.nav_default_pop_exit_anim);
                    fragmentTransaction.replace(R.id.fragment, fragment);// your container and your fragment
                    fragmentTransaction.addToBackStack("tag");
                    fragmentTransaction.commit();
                    break;
                }
                case 2: {
                    FragmentMerryGoRound fragment = new FragmentMerryGoRound(); // you fragment
                    FragmentManager fragmentManager = ((FragmentActivity) v.getContext()).getSupportFragmentManager(); // instantiate your view context
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.animator.nav_default_enter_anim, R.animator.nav_default_exit_anim,
                            R.animator.nav_default_pop_enter_anim, R.animator.nav_default_pop_exit_anim);
                    fragmentTransaction.replace(R.id.fragment, fragment);// your container and your fragment
                    fragmentTransaction.addToBackStack("tag");
                    fragmentTransaction.commit();
                    break;
                }
                case 3: {
                    FragmentFiveDCinema fragment = new FragmentFiveDCinema(); // you fragment
                    FragmentManager fragmentManager = ((FragmentActivity) v.getContext()).getSupportFragmentManager(); // instantiate your view context
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.animator.nav_default_enter_anim, R.animator.nav_default_exit_anim,
                            R.animator.nav_default_pop_enter_anim, R.animator.nav_default_pop_exit_anim);
                    fragmentTransaction.replace(R.id.fragment, fragment);// your container and your fragment
                    fragmentTransaction.addToBackStack("tag");
                    fragmentTransaction.commit();
                    break;
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mFavList.size();
    }
}
``` `item` ```
public class AttractionsItem
{
    private int mImg_1,mImg_2;
    private final String mText_attraction_name;
    private final String mText_attraction_type;
    private final String mText_attraction_cost;

    public AttractionsItem(int img1,int img2, String text_attraction_name, String text_attraction_type, String text_attraction_cost)
    {
        mImg_1 = img1;
        mImg_2 = img2;
        mText_attraction_name = text_attraction_name;
        mText_attraction_type = text_attraction_type;
        mText_attraction_cost = text_attraction_cost;

    }
    public int getImg1()
    {
        return mImg_1;
    }
    public int getImg2()
    {
        return mImg_2;
    }
    public String get_attraction_name()
    {
        return mText_attraction_name;
    }
    public String get_attraction_type()
    {
        return mText_attraction_type;
    }
    public String get_attraction_cost()
    {
        return mText_attraction_cost;
    }
}
jhiyze9q

jhiyze9q1#

要获得recyclerview项的正确位置,请不要使用 position 在onbindviewholder内的onclicklistener中,使用 holder.getAdapterPosition(); 或者 holder.getBindingAdapterPosition(); 所以

holder.Card.setOnClickListener(v -> {
        switch (position) {
            case 1: 
                ...
                break;

            case 2: 
                ...
                break;
            }
            case 3: 
                ...
                break;
            }
        }
    });

使用

holder.Card.setOnClickListener(v -> {
    switch (holder.getAdapterPosition()) {
        case 1: 
            ...
            break;

        case 2: 
            ...
            break;
        }
        case 3: 
            ...
            break;
        }
    }
});

相关问题