我需要向该应用添加部分,但该应用未运行吗?

时间:2019-06-13 16:16:35

标签: java android

我想向我的应用添加新部分,但是当我运行该应用时,新部分没有运行? 如您所见,我有六个部分,我添加了一个新部分,但自然会与我一起出现在列表中,按一下它不会给我带来任何好处。我是否需要在适配器中添加一些东西,以便它起作用并带我到本节

据我所知,我有一个“活动”开关(位置),我试图添加一个新的案件,但没有用。 !! 如果要添加页面代码 ListOfBookActivity

MainActivity

public class MainActivity extends AppCompatActivity {
    List<Book>list;
    RecyclerView recyclerView;
    BookAdapter adapter;
    private TextView title_text;
    private MyDrawerLayout mDrawerLayout;
    private NavigationView mNavigationView;
    private View navHeader;
    Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        title_text = findViewById(R.id.title);

         toolbar = findViewById(R.id.toolbar);
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
//        toolbar.setTitle(getString(R.string.conatact_us));
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("");
//        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        title_text.setText("head");

        recyclerView=findViewById(R.id.recyclerView);
        setBookRecycler();
        list.add(new Book("s1"));
        list.add(new Book("s2"));
        list.add(new Book("s3"));
        list.add(new Book("s4"));
        list.add(new Book("s5"));
        list.add(new Book("s6"));
        list.add(new Book("news"));

        adapter=new BookAdapter(list,MainActivity.this,true);
        recyclerView.setAdapter(adapter);


        mDrawerLayout = (MyDrawerLayout) findViewById(R.id.drawer_layout);
        mNavigationView = (NavigationView) findViewById(R.id.nav_view);
        navHeader = mNavigationView.getHeaderView(0);

        // Navigation view header

//          imgNavHeaderBg = (ImageView) navHeader.findViewById(R.id.img_header_bg);

        navHeader.findViewById(R.id.position0).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,WordActivity.class));
            }
        });

        navHeader.findViewById(R.id.position1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivity(new Intent(MainActivity.this,WordActivity.class));



            }
        });

        navHeader.findViewById(R.id.position2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivity(new Intent(MainActivity.this,WordActivity.class));

            }
        });
        navHeader.findViewById(R.id.position3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,WordActivity.class));

            }
        });
        setUpNavigationView();
    }
    private void setUpNavigationView() {
        //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            // This method will trigger on item Click of navigation menu
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {

                //Check to see which item was being clicked and perform appropriate action
                switch (menuItem.getItemId()) {
                    //Replacing the main content with ContentFragment Which is our Inbox View;
//                    case R.id.nav_1:
//                        Toast.makeText(TimelineActivity.this, "test 1", Toast.LENGTH_SHORT).show();
//                        break;
//                    case R.id.nav_2:
//                        Toast.makeText(TimelineActivity.this, "test 2", Toast.LENGTH_SHORT).show();
//
//                        break;
//                    case R.id.nav_3:
//                        Toast.makeText(TimelineActivity.this, "test 3", Toast.LENGTH_SHORT).show();
//
//                        break;
//                    case R.id.nav_4:
//                        Toast.makeText(TimelineActivity.this, "test 4", Toast.LENGTH_SHORT).show();
//
//                        break;
//                    case R.id.nav_5:
//                        Toast.makeText(TimelineActivity.this, "test 5", Toast.LENGTH_SHORT).show();
//
//                        break;
//                    default:
//                        navItemIndex = 0;
                }

                //Checking if the item is in checked state or not, if not make it in checked state
                if (menuItem.isChecked()) {
                    menuItem.setChecked(false);
                } else {
                    menuItem.setChecked(true);
                }
                menuItem.setChecked(true);


                return true;
            }
        });


        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,toolbar, R.string.open, R.string.close) {

            @Override
            public void onDrawerClosed(View drawerView) {
                // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
                super.onDrawerOpened(drawerView);
            }
        };
        //Setting the actionbarToggle to drawer layout
        mDrawerLayout.setDrawerListener(actionBarDrawerToggle);
        //calling sync state is necessary or else your hamburger icon wont show up
        actionBarDrawerToggle.syncState();

    }
    private void setBookRecycler() {
        list = new ArrayList<>();
        adapter = new BookAdapter(list, this,true);
        recyclerView.setHasFixedSize(true);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(), 1);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(adapter);

    }
}

BookAdapter

public class BookAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Book> moviesList;
    private Activity context;
    private Boolean bool;


    public BookAdapter(List<Book> moviesList, Activity context,Boolean bool) {
        this.moviesList = moviesList;
        this.context = context;
        this.bool=bool;

    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.row_category, parent, false);
        return new MovieRow(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

        final Book temp = moviesList.get(position);
        MovieRow vh = (MovieRow) holder;
        vh.mName.setText(temp.getName());
        switch (position){
                case 0:
                vh.card.setBackgroundColor(context.getResources().getColor(R.color.c1));
                break;
                case 1:
                    vh.card.setBackgroundColor(context.getResources().getColor(R.color.c2));
                break;
                case 2:
                    vh.card.setBackgroundColor(context.getResources().getColor(R.color.c3));
                    break;
                case 3:
                    vh.card.setBackgroundColor(context.getResources().getColor(R.color.c4));
                    break;
                case 4:
                    vh.card.setBackgroundColor(context.getResources().getColor(R.color.c5));
                    break;
                case 5:
                    vh.card.setBackgroundColor(context.getResources().getColor(R.color.c6));
                    break;
            case 6:
                vh.card.setBackgroundColor(context.getResources().getColor(R.color.c1));
                break;
            case 7:
                vh.card.setBackgroundColor(context.getResources().getColor(R.color.c2));
                break;
            case 8:
                vh.card.setBackgroundColor(context.getResources().getColor(R.color.c3));
                break;
            case 9:
                vh.card.setBackgroundColor(context.getResources().getColor(R.color.c4));
                break;
            case 10:
                vh.card.setBackgroundColor(context.getResources().getColor(R.color.c5));
                break;
            case 11:
                vh.card.setBackgroundColor(context.getResources().getColor(R.color.c6));
                break;

        }
        vh.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bool&&position<4){
                Intent intent=new Intent(context, SelectTypeActivity.class);
                intent.putExtra("position",position+1+"");
                intent.putExtra("name",moviesList.get(position).getName());
                context.startActivity(intent);
                } else if (!bool){
                    Intent intent=new Intent(context, PDFViewActivity.class);
                    intent.putExtra("position",position+1+"");
                    intent.putExtra("path",moviesList.get(position).getPath());
                    context.startActivity(intent);
                }else if (position==4||position==5){
                    Intent intent=new Intent(context, ListOfBookActivity.class);
                    intent.putExtra("position",position+1+"");
                    intent.putExtra("path",moviesList.get(position).getPath());
                    context.startActivity(intent);
                }

            }
        });

    }

    @Override
    public int getItemCount() {
        return moviesList == null ? 0 : moviesList.size();
    }

    public class MovieRow extends RecyclerView.ViewHolder {

        private TextView mName;
        public ConstraintLayout card;
        public CardView cardView;

        public MovieRow(View itemView) {
            super(itemView);
            mName = itemView.findViewById(R.id.nameCategory);
            card = itemView.findViewById(R.id.card);
            cardView = itemView.findViewById(R.id.cardView);

        }
    }


}

0 个答案:

没有答案