如何通过从片段上的recyclerView中选择项目来执行操作

时间:2016-07-12 15:45:13

标签: java android android-layout android-studio android-fragments

我正在使用Android Studio中的导航抽屉模板

我只使用一个活动...片段被替换为一个活动

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        // here I call the fragment

        FragmentManager fm=getFragmentManager();
        fm.beginTransaction().replace(R.id.content_main_frame, new homeFragment()).commit();
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
public class homeFragment extends Fragment{

    RecyclerView recyclerView;
    String[] fields={"ALV","NIJU","AJITH","ASWIN"};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        //for getting recyclerview id

        recyclerView= (RecyclerView) rootView.findViewById(R.id.recyclerView_home);

        // for specifying layout manager

        LinearLayoutManager manager=new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(manager);

        homeListAdapter adapter = new homeListAdapter(this.getActivity(), fields);
        recyclerView.setAdapter(adapter);

        recyclerView.setHasFixedSize(true);

        return rootView;
    }
}
public class homeListAdapter extends RecyclerView.Adapter<homeListAdapter.homeViewHolder> {

    private Context context;
    private String[] field;

    public homeListAdapter(Context context, String[] field){
        this.context=context;
        this.field=field;


    }

    @Override
    public homeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater=LayoutInflater.from(parent.getContext());
        View view = inflater.from(parent.getContext()).inflate(R.layout.item_home,parent,false);

        homeViewHolder viewHolder= new homeViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(homeViewHolder holder, int position) {

        holder.card_home_title.setText(field[position]);

    }

    @Override
    public int getItemCount() {

        return field.length;
    }

    // View holder

    public static class homeViewHolder extends RecyclerView.ViewHolder{

        public CardView card_view;
        public TextView card_home_title;

        public homeViewHolder(View itemView) {
            super(itemView);
            card_view = (CardView) itemView.findViewById(R.id.card_view);
            card_home_title = (TextView) itemView.findViewById(R.id.card_text);
        }
    }

}
public class ItemHome implements Serializable {

    public int id;

    public String title;
}

当我按ALV时,我想替换为此alvfragment,当我按Niju时,我想显示此Nijufragment

截图:

This is the screenshot of the app

1 个答案:

答案 0 :(得分:0)

如果我理解正确你想要从导航抽屉中替换片段,那么你需要转到MainActivty这是一个如何做到这一点的例子:

  public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
//use here the desired nav
        } else if (id == R.id.nav_ALV) {
        FragmentManager fm=getFragmentManager();
        fm.beginTransaction().replace(R.id.content_main_frame, new ALVFragment).commit();