Фрагмент recyclerview при щелчке, чтобы открыть другой фрагмент

#java #android #android-fragments #android-recyclerview

#java #Android #android-фрагменты #android-recyclerview

Вопрос:

Ниже приведен код для моего адаптера

DashboardAdapter.java

 public class DashboardAdapter extends RecyclerView.Adapter<DashboardAdapter.DashviewHolder> {

    ArrayList<DashModel> dashHolder;

    private LayoutInflater layoutInflater;

    public DashboardAdapter(ArrayList<DashModel> dashHolder) {
        this.dashHolder = dashHolder;
    }

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

    @Override
    public void onBindViewHolder(@NonNull DashviewHolder holder, int position) {
        holder.dashImg.setImageResource(dashHolder.get(position).getImage());
        holder.header.setText(dashHolder.get(position).getHeader());
        holder.desc.setText(dashHolder.get(position).getDesc());
    }

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

    class DashviewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ImageView dashImg;
        TextView header, desc;

        public DashviewHolder(@NonNull View itemView) {
            super(itemView);
            dashImg = itemView.findViewById(R.id.dash_slide_img);
            header = itemView.findViewById(R.id.dash_slide_txt1);
            desc = itemView.findViewById(R.id.dash_slide_txt2);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            final Fragment fm;
            switch (getLayoutPosition()) {
                case 0:
                    fm = new SavingsFragment();
                    break;

                case 1:
                    fm = new TargetFragment();
                    break;

                case 2:
                    fm = new InvestFragment();
                    break;
            }
        }
    }
}
 

DashboardFragment.java

 public class DashboardFragment extends Fragment {

    private RecyclerView recyclerView;

    ArrayList<DashModel> dashDataHolder;
    private View view;

    private DashboardAdapter dashboardAdapter;
    private FragmentManager fm;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_dashboard, container, false);
        recyclerView = view.findViewById(R.id.dash_slider_layout);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));

        dashDataHolder = new ArrayList<>();

        DashModel ob1 = new DashModel(R.drawable.ic_savings, R.string.first_slide_dash, R.string.first_slide_dash_amt);
        dashDataHolder.add(ob1);

        DashModel ob2 = new DashModel(R.drawable.ic_savings, R.string.second_slide_dash, R.string.second_slide_dash_amt);
        dashDataHolder.add(ob2);

        DashModel ob3 = new DashModel(R.drawable.ic_savings, R.string.third_slide_dash, R.string.third_slide_dash_amt);
        dashDataHolder.add(ob3);

        recyclerView.setAdapter(new DashboardAdapter(dashDataHolder));
        //fm.getFragmentManager().beginTransaction().replace(R.id.fragment_container,getTargetFragment(),"New Fragment").commit();

        return view;
    }
}
 

Затем код для моей основной деятельности, который имеет как выдвижную, так и нижнюю навигацию

 public class RetailerDashboard extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private static final String TAG = RetailerDashboard.class.getSimpleName();
    ChipNavigationBar chipNavigationBar;
    DrawerLayout drawerLayout;
    Toolbar toolbar;

    NavigationView navigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retailer_dashboard);

        /*--------------------HOOKS----------------------*/
        drawerLayout = findViewById(R.id.drawer_layout);
        navigationView = findViewById(R.id.nav_view);
        toolbar = findViewById(R.id.toolbar);
        chipNavigationBar = findViewById(R.id.bottom_bar);


        /*--------------------BOTTOM MENU----------------------*/
        chipNavigationBar.setItemSelected(R.id.menu_home, true);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, new DashboardFragment()).commit();
        bottomMenu();

        /*--------------------TOOLBAR----------------------*/
        setSupportActionBar(toolbar);

        /*--------------------NAVIGATION DRAWER MENU----------------------*/
        navigationView.bringToFront();
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,
                drawerLayout,
                toolbar,
                R.string.navigation_drawer_open,
                R.string.navigation_drawer_close);
        toggle.syncState();
        navigationView.setNavigationItemSelectedListener(this);
    }

    private void bottomMenu() {
        chipNavigationBar.setOnItemSelectedListener(new ChipNavigationBar.OnItemSelectedListener() {
            @Override
            public void onItemSelected(int id) {
                Fragment fragment = null;
                switch (id) {
                    case R.id.menu_saving:
                        fragment = new SavingsFragment();
                        break;
                    case R.id.menu_home:
                        fragment = new DashboardFragment();
                        break;
                    case R.id.menu_target:
                        fragment = new TargetFragment();
                        break;
                }
                if (fragment != null) {
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.fragment_container, fragment).commit();
                } else {

                    Log.e(TAG, "Error in creating fragment");
                }
            }
        });
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.nav_profile:
                Intent accountIntent = new Intent(RetailerDashboard.this, Profile.class);
                startActivity(accountIntent);
                break;
            case R.id.nav_account_balnce:
                Intent balanceIntent = new Intent(RetailerDashboard.this, AccountBalance.class);
                startActivity(balanceIntent);
                break;
            case R.id.nav_notifications:
                Intent notifIntent = new Intent(RetailerDashboard.this, Notifications.class);
                startActivity(notifIntent);
                break;
            case R.id.nav_settings:
                Intent settingsIntent = new Intent(RetailerDashboard.this, Settings.class);
                startActivity(settingsIntent);
                break;
            case R.id.nav_chat:
                Intent chatIntent = new Intent(RetailerDashboard.this, Chat.class);
                startActivity(chatIntent);
                break;
            case R.id.nav_faq:
                Intent helpIntent = new Intent(RetailerDashboard.this, FAQ.class);
                startActivity(helpIntent);
                break;
        }
        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
            finishAffinity();
        }
    }
}
 

Комментарии:

1. Пожалуйста, добавьте, с какой проблемой вы столкнулись во время onClick и что вы пробовали до сих пор, вместо того, чтобы просто публиковать полный код