简体   繁体   English

RecylerView Adapter 在不工作的片段中

[英]RecylerView Adapter in not working fragment

In my application there are few fragments in an activity.在我的应用程序中,活动中的片段很少。 I used recyclerview in first activity and i set the recyclerview items by using an adapter On clicking at item in list of recyclerview another activity opens.我在第一个活动中使用了 recyclerview,并通过使用适配器设置了 recyclerview 项目 单击 recyclerview 列表中的项目会打开另一个活动。 Now problem is this when i open another fragment and click on its interface then the activity which is opened by adapter of fragment 1 is also opened by clicking anywhere in fragment 2.现在的问题是,当我打开另一个片段并单击其界面时,通过单击片段 2 中的任意位置也可以打开由片段 1 的适配器打开的活动。

This is my adapter这是我的适配器

public class ProblematicReportAdapter extends RecyclerView.Adapter<ProblematicReportAdapter.ProblematicReportViewHolder> implements Filterable {

    private List<ProblematicReportItem> exampleList;
    private List<ProblematicReportItem> exampleListFull;
    onClickInterface onClickInterface;
    class ProblematicReportViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView imageView;
        TextView textView1;
        TextView textView2;
        String pos;
        ProblematicReportViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
            textView1 = itemView.findViewById(R.id.placename);
            textView2 = itemView.findViewById(R.id.placedescription);

            imageView.setOnClickListener(this);
            textView1.setOnClickListener(this);
            textView2.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            Intent intent=new Intent(home_Activity, ProblematicReportComplaint.class);
            intent.putExtra("maintenancetype",pos);
            home_Activity.startActivity(intent);
        }
    }
    public ProblematicReportAdapter(List<ProblematicReportItem> exampleList) {
        this.exampleList = exampleList;

        exampleListFull = new ArrayList<>(exampleList);
    }

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

    @Override
    public void onBindViewHolder(@NonNull ProblematicReportViewHolder holder, final int position) {
        ProblematicReportItem currentItem = exampleList.get(position);

        holder.imageView.setImageResource(currentItem.getImageResource());
        holder.textView1.setText(currentItem.getText1());
        holder.textView2.setText(currentItem.getText2());

      holder.pos=holder.textView1.getText().toString();
    }

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

This is my fragment 1 code这是我的片段 1 代码

 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = (RecyclerView)view.findViewById(R.id.lstvw);
        ProblematicReportItems = new ArrayList<>();
//        onclickInterface = new onClickInterface() {
//            @Override
//            public void setClick(int abc) {
//
//                String title1 = ((TextView) recyclerView.findViewHolderForAdapterPosition(abc).itemView.findViewById(R.id.placename)).getText().toString();
//                adapter.notifyDataSetChanged();
//
//
//            }
//        };
        filllistItems();
        setUpRecyclerView();

    }


    private void filllistItems() {

        ProblematicReportItems.add(new ProblematicReportItem(R.drawable.ic_toolbox,home_Activity.getResources().getString(R.string.preventive),home_Activity.getResources().getString(R.string.preventive_info)));
        ProblematicReportItems.add(new ProblematicReportItem(R.drawable.ic_toolbox,home_Activity.getResources().getString(R.string.predicitive),home_Activity.getResources().getString(R.string.predicitive_info)));
        ProblematicReportItems.add(new ProblematicReportItem(R.drawable.ic_toolbox,home_Activity.getResources().getString(R.string.deferred),home_Activity.getResources().getString(R.string.deferred_info)));
        ProblematicReportItems.add(new ProblematicReportItem(R.drawable.ic_toolbox,home_Activity.getResources().getString(R.string.Breakdown),home_Activity.getResources().getString(R.string.Breakdown_info)));
        ProblematicReportItems.add(new ProblematicReportItem(R.drawable.ic_toolbox,home_Activity.getResources().getString(R.string.Condition),home_Activity.getResources().getString(R.string.Condition_info)));
        ProblematicReportItems.add(new ProblematicReportItem(R.drawable.ic_toolbox,home_Activity.getResources().getString(R.string.Corrective),home_Activity.getResources().getString(R.string.Corrective_info)));
        ProblematicReportItems.add(new ProblematicReportItem(R.drawable.ic_toolbox,home_Activity.getResources().getString(R.string.Emergency),home_Activity.getResources().getString(R.string.Emergency_info)));
        ProblematicReportItems.add(new ProblematicReportItem(R.drawable.ic_toolbox,home_Activity.getResources().getString(R.string.Scheduled),home_Activity.getResources().getString(R.string.Scheduled_info)));
        ProblematicReportItems.add(new ProblematicReportItem(R.drawable.ic_toolbox,home_Activity.getResources().getString(R.string.Precision),home_Activity.getResources().getString(R.string.Precision_info)));
        ProblematicReportItems.add(new ProblematicReportItem(R.drawable.ic_toolbox,home_Activity.getResources().getString(R.string.Reliability),home_Activity.getResources().getString(R.string.Reliability_info)));
        ProblematicReportItems.add(new ProblematicReportItem(R.drawable.ic_toolbox,home_Activity.getResources().getString(R.string.Productive),home_Activity.getResources().getString(R.string.Productive_info)));
        ProblematicReportItems.add(new ProblematicReportItem(R.drawable.ic_toolbox,home_Activity.getResources().getString(R.string.Routine),home_Activity.getResources().getString(R.string.Routine_info)));
        ProblematicReportItems.add(new ProblematicReportItem(R.drawable.ic_toolbox,home_Activity.getResources().getString(R.string.Planned),home_Activity.getResources().getString(R.string.Planned_info)));
        ProblematicReportItems.add(new ProblematicReportItem(R.drawable.ic_toolbox,home_Activity.getResources().getString(R.string.Run),home_Activity.getResources().getString(R.string.Run_info)));
        ProblematicReportItems.add(new ProblematicReportItem(R.drawable.ic_toolbox,home_Activity.getResources().getString(R.string.Periodic),home_Activity.getResources().getString(R.string.Periodic_info)));
        ProblematicReportItems.add(new ProblematicReportItem(R.drawable.ic_toolbox,home_Activity.getResources().getString(R.string.Proactive),home_Activity.getResources().getString(R.string.Proactive_info)));
        ProblematicReportItems.add(new ProblematicReportItem(R.drawable.ic_toolbox,home_Activity.getResources().getString(R.string.Reactive),home_Activity.getResources().getString(R.string.Reactive_info)));
    }
    private void setUpRecyclerView() {
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
        adapter = new ProblematicReportAdapter(ProblematicReportItems);

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
    }

and this is activity这是活动

  navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                int id = menuItem.getItemId();
                Fragment fragment = null;

                FragmentManager fragmentManager = getFragmentManager();


                if (id == R.id.nav_home) {
                    fragment = new ProblematicReportFragment();


                } else if (id == R.id.nav_gallery) {
                    Toast.makeText(HomeActivity.this, "ok", Toast.LENGTH_SHORT).show();
                }
                else if (id == R.id.nav_history) {

                }
                else if (id == R.id.nav_tools) {
                    fragment = new UserProfileFragment();


                }
                if (fragment != null) {

                    for (Fragment fragments:getSupportFragmentManager().getFragments()) {
                        getSupportFragmentManager().beginTransaction().remove(fragments).commit();
                    }

                    FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
                    transaction.add(R.id.nav_host_fragment,fragment);
                    transaction.commit();
                }

                drawer.closeDrawer(GravityCompat.START);
                return true;
            }
        });

I don't know why onclick function in adapter is also working in fragment 2我不知道为什么适配器中的 onclick 函数也在片段 2 中起作用

For some reasons seems like your fragment is still there even after you are calling remove(fragment)由于某些原因,即使在您调用remove(fragment)之后,您的片段似乎仍然存在

I would suggest something like this.我会建议这样的事情。

FragmentManager fm = getSupportFragmentManager();
 fm.beginTransaction()
          .setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
          .show(fragmentToShow)
          .hide(fragmentToHide)
          .commit();

can you try with this instead ?你可以试试这个吗?

if (fragment != null) {
FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.nav_host_fragment,fragment);
                transaction.commit();
}

There are multiple possibilities.有多种可能性。 It may be happening because of the overlapping of fragments.这可能是由于片段重叠而发生的。 Especially, when the newly added fragment is smaller in height than last fragment.特别是当新添加的片段的高度小于上一个片段时。 Used "match_parent" in the height of fragments.在片段的高度中使用了“match_parent”。 And let me know if it resolves the issue.如果它解决了问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM