如何在标签的水平滚动视图上添加图像按钮,以便在android studio中的那些图像按钮左右滚动?

时间:2016-10-21 08:11:17

标签: android android-fragments android-toolbar horizontalscrollview

请帮忙 任何人都可以帮我解决问题

    public class FragmentForScrollBar extends Fragment {
    private HorizontalScrollView hsv;
    Button left, right;
    int currentScrollX = 0;
    Toolbar Toolbar;
    RelativeLayout RelativeLayout;
    ImageButtonhome,player,download,playlist,stream,wish,hit,instrument,news,extras;
    TextView t;
    FragmentTransaction fragmentTransaction;

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

        View view =  inflater.inflate(R.layout.fragmentforscrollbar,null);

        MyPlayer playerFragment = new MyPlayer();
        fragmentTransaction = getChildFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment, playerFragment, null);
        fragmentTransaction.commit();

        hsv = (HorizontalScrollView) view.findViewById(R.id.horizontalScrollView);

        t = (TextView)view.findViewById(R.id.toolbartitle);
        home =(ImageButton)view.findViewById(R.id.imageButtonhomea);
        player =(ImageButton)view.findViewById(R.id.imageButtonaa);
        playlist =(ImageButton)view.findViewById(R.id.imageButtonca);
        download =(ImageButton)view.findViewById(R.id.imageButtonba);
        stream =(ImageButton)view.findViewById(R.id.imageButtonda);
        wish =(ImageButton)view.findViewById(R.id.imageButtonea);
        hit =(ImageButton)view.findViewById(R.id.imageButtonfa);
        instrument =(ImageButton)view.findViewById(R.id.imageButtonga);
        news =(ImageButton)view.findViewById(R.id.imageButtonha);
        extras =(ImageButton)view.findViewById(R.id.imageButtonia);

        home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                t.setText("Home");
                Fragment Home = new Home();
                fragmentTransaction = getChildFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.fragment, Home);
                fragmentTransaction.addToBackStack(null);

                fragmentTransaction.commit();

            }
         });
        player.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                t.setText("Player");
                Fragment Myplayer = new MyPlayer();
                fragmentTransaction = getChildFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.fragment, Myplayer);
                fragmentTransaction.addToBackStack(null);

                fragmentTransaction.commit();


            }
        });
        playlist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Fragment yourPlayList = new Your_Playlist();
                fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.containerView, yourPlayList);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });
        download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                t.setText("MyDownload");
                Fragment myDownload = new Downloads();
                fragmentTransaction = getChildFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.fragment,myDownload);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });
        stream.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                t.setText("Streaming");
                Fragment uzBek1 = new UzBek();
                fragmentTransaction = getChildFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.fragment,uzBek1);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });
        wish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                t.setText("Your Wishes");
                Fragment sendWishes = new wishes();
                fragmentTransaction = getChildFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.fragment,sendWishes);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();

            }
        });
        hit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                t.setText("Hit Parade");
                Fragment hitParade = new hits();
                fragmentTransaction = getChildFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.fragment,hitParade);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });
        instrument.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                t.setText("Uzbek Instruments");
                Fragment uzBek1 = new UzBek();
                fragmentTransaction = getChildFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.fragment,uzBek1);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });
        news.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                t.setText("News");
                Fragment news = new news();
                fragmentTransaction = getChildFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.fragment,news);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });
        extras.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                t.setText("Extras");
                Fragment extras = new extras();
                fragmentTransaction = getChildFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.fragment, extras);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });
        return view;

    }
   }

sorry code was too long so i captured the image of the xml module file

请参阅上面的代码,让我知道通过图像按钮左右滚动迷你滚动布局的解决方案。     我正在使用两个箭头按钮并告诉我如果我想在滚动之间隐藏它们该怎么办。现在有人可以帮帮我

1 个答案:

答案 0 :(得分:1)

对于任何可能对同一问题感到疑惑的人:

public class FragmentForScrollBar extends Fragment {


SeekBar verticalSeekBar=null;

private HorizontalScrollView hsv, hsv2;

Toolbar Toolbar;


ImageButton home,player,download,playlist,stream,wish,hit,instrument,news,extras, bP,bN;
ImageButton homea,playera,downloada,playlista,streama,hita,wisha,instrumenta,newsa, extrasa;
TextView t;
FragmentTransaction fragmentTransaction;

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

    View view =  inflater.inflate(R.layout.fragmentforscrollbar,null);

    MyPlayer playerFragment = new MyPlayer();
    fragmentTransaction = getChildFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment, playerFragment, null);
    fragmentTransaction.commit();

hsv = (HorizontalScrollView) view.findViewById(R.id.horizontalScrollView);

    t = (TextView)view.findViewById(R.id.toolbartitle);
    home =(ImageButton)view.findViewById(R.id.imageButtonhomea);
    player =(ImageButton)view.findViewById(R.id.imageButtonaa);
    playlist =(ImageButton)view.findViewById(R.id.imageButtonca);
    download =(ImageButton)view.findViewById(R.id.imageButtonba);
    stream =(ImageButton)view.findViewById(R.id.imageButtonda);
    wish =(ImageButton)view.findViewById(R.id.imageButtonea);
    hit =(ImageButton)view.findViewById(R.id.imageButtonfa);
    instrument =(ImageButton)view.findViewById(R.id.imageButtonga);
    news =(ImageButton)view.findViewById(R.id.imageButtonha);
    extras =(ImageButton)view.findViewById(R.id.imageButtonia);


    playera =(ImageButton)view.findViewById(R.id.imageButtonaab);
    playlista =(ImageButton)view.findViewById(R.id.imageButtoncab);
    downloada =(ImageButton)view.findViewById(R.id.imageButtonbab);
    streama =(ImageButton)view.findViewById(R.id.imageButtondab);
    wisha =(ImageButton)view.findViewById(R.id.imageButtoneab);
    hita =(ImageButton)view.findViewById(R.id.imageButtonfab);
    instrumenta=(ImageButton)view.findViewById(R.id.imageButtongab);
    newsa =(ImageButton)view.findViewById(R.id.imageButtonhab);
    extrasa =(ImageButton)view.findViewById(R.id.imageButtoniab);


    verticalSeekBar=(SeekBar)view.findViewById(R.id.seekBar);


    ImageButton bP = (ImageButton) view.findViewById(R.id.left);
    ImageButton bN = (ImageButton) view.findViewById(R.id.right);

    bP.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //value 500 is arbitrarily given. if you want to achieve
            //element-by-element scroll you should get the width of the
            //previous element dynamically or if the elements of the
            //list have uniform width just put that value instead
            hsv.scrollTo(0,-500);
            //if it's the first/last element you can bPrevoius.setEnabled(false)

        }
    });


    bN.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            hsv.scrollTo(500,0);

        }
    });
    home.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t.setText("Home");
            Fragment Home = new Home();
            fragmentTransaction = getChildFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment, Home);
            fragmentTransaction.addToBackStack(null);
            // fragmentTransaction.popBackStack();
            fragmentTransaction.commit();
            //  t.setText("Player");
        }
    });
    player.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t.setText("Player");
            Fragment Myplayer = new MyPlayer();
            fragmentTransaction = getChildFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment, Myplayer);
            fragmentTransaction.addToBackStack(null);
           // fragmentTransaction.popBackStack();
            fragmentTransaction.commit();


        }
    });
    playlist.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          //  DrawerNavigation mainActivity = new DrawerNavigation();
          //  mainActivity.drawerLayout.closeDrawers();
            Fragment yourPlayList = new Your_Playlist();
            fragmentTransaction = getFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.containerView, yourPlayList);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

          // t.setText("PlayList");
        }
    });
    download.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t.setText("MyDownload");
            Fragment myDownload = new Downloads();
            fragmentTransaction = getChildFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment,myDownload);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    stream.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t.setText("Streaming");
            Fragment uzBek1 = new UzBek();
            fragmentTransaction = getChildFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment,uzBek1);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    wish.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t.setText("Your Wishes");
            Fragment sendWishes = new wishes();
            fragmentTransaction = getChildFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment,sendWishes);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

        }
    });
    hit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t.setText("Hit Parade");
            Fragment hitParade = new hits();
            fragmentTransaction = getChildFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment,hitParade);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    instrument.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t.setText("Uzbek Instruments");
            Fragment uzBek1 = new UzBek();
            fragmentTransaction = getChildFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment,uzBek1);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    news.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t.setText("News");
            Fragment news = new news();
            fragmentTransaction = getChildFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment,news);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    extras.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t.setText("Extras");
            Fragment extras = new extras();
            fragmentTransaction = getChildFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment, extras);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });

    return view;

}

}

只需在两端添加图像按钮,然后在它们上面添加seOnclickListener(),然后将它们向上滚动到所需的值,如代码所示。