如何在viewpager上显示按钮

问题描述 投票:1回答:1

我有viewpager,其中每个片段都有videoview现在我想在该视图寻呼机上显示按钮。我正在使用框架布局。但仍然无法使这些按钮可见概述寻呼机。只有我的视图寻呼机中的片段可见,但我想通过viewpager寻呼机显示的按钮不可见。当活动开始时,当片段中的视频变得可见按钮不可见时,按钮显示2.3秒

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    tools:context=".Activities.MenuButtonActivity">


    <com.abp.app.ViewPager.NonSwipeableViewPager
        android:id="@+id/viewpager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <RelativeLayout
        android:id="@+id/rl_bottom_overlay"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="yyyyyyyy"
        android:visibility="visible" >

        <Button
            android:id="@+id/btn_left_arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="35dp"
            android:text="yyyyyyyy" />

        <Button
            android:id="@+id/btn_below_share"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="35dp"
            android:text="yyyyyyyy"
            android:visibility="visible" />

        <Button
            android:id="@+id/btn_right_arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="50dp"
            android:text="yyyyyyyy"
            android:layout_toRightOf="@id/btn_left_arrow" />
    </RelativeLayout>




     </FrameLayout>

这是活动代码

package com.abp.app.Activities;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.abp.app.Adapter.MyPageAdapter;
import com.abp.app.CallerFuntionClasses.FunctionsCallApiFromServer;
import com.abp.app.Fragments.ConnectivityFragment;
import com.abp.app.Fragments.LondonRoyalDocksFragment;
import com.abp.app.Fragments.MasterPlanFragment;
import com.abp.app.Fragments.OfficesFragment;
import com.abp.app.Fragments.ProjectTeamFragment;
import com.abp.app.Fragments.VideosFragment;
import com.abp.app.R;
import com.abp.app.Utilities.AppVeriableClass;
import com.abp.app.Utilities.CustomSharePreferences;
import com.abp.app.Utilities.UtilsForDownloadDataFromServer;
import com.abp.app.ViewPager.NonSwipeableViewPager;

import java.util.ArrayList;
import java.util.List;

import static android.widget.RelativeLayout.ABOVE;

public class MenuButtonActivity extends AppCompatActivity {

    private Context context;
    private Activity activity;
    private int BtnHeight = 0;
    private ProgressBar progressBar;
    private TextView tv_progressCounting;
    private static String dirPath;
    boolean doubleBackToExit = false;
    FrameLayout frameLayout;
    RelativeLayout.LayoutParams params;
    FunctionsCallApiFromServer apiFromServer;
    String accessTokenType,accessToken;
    SharedPreferences sharedPreferences;
    CustomSharePreferences customSharePreferences = new CustomSharePreferences();
    ConnectivityManager conMgr;
    NetworkInfo netInfo;
    ArrayList<String> test;
    /////////////  Fragments //////////
    MasterPlanFragment masterPlanFragment ;
    OfficesFragment officesFragment;
    ConnectivityFragment connectivityFragment ;
    VideosFragment videosFragment ;
    LondonRoyalDocksFragment londonRoyalDocksFragment;
    ProjectTeamFragment projectTeamFragment ;

    MyPageAdapter pageAdapter;
    com.abp.app.ViewPager.NonSwipeableViewPager viewpager;

    ////////////////////////////////////
    private android.support.v4.app.FragmentTransaction fragmentTransaction;
    final Handler handler = new Handler();

    private static final int[] menuBtnIdsArray = {R.id.btn_masterplan,R.id.btn_Offices,R.id.btn_connectivity,R.id.btn_londonroyaldock,R.id.btn_videos,R.id.btn_projectteam};

    private ArrayList<Button> menuBtnArrayList = new ArrayList<Button>();

    private int previousClickedButton = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manu_buttons);
        viewpager = (com.abp.app.ViewPager.NonSwipeableViewPager)findViewById(R.id.viewpager);
        viewpager.setPagingEnabled(false);



        List fragments = getFragmetns();
        pageAdapter = new  MyPageAdapter( getSupportFragmentManager(),getFragmetns());
        viewpager.setAdapter(pageAdapter);


        VariableInitialize();
//        CallAPIFromServer();
        hideStatusBar();
       // onclickBtnFrangment();
       // firstTimeAlwaysCallFragment();
    }

    private void firstTimeAlwaysCallFragment() {
        frameLayout.setLayoutParams(new RelativeLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT));
        setFragment(masterPlanFragment);
        menuBtnArrayList.get(0).setBackgroundColor(menuBtnArrayList.get(0).getContext().getResources().getColor(R.color.button_pressed));
    }

    private void VariableInitialize(){
        context = MenuButtonActivity.this;
        activity = MenuButtonActivity.this;

        ////////////// Fragment Initialize/////////////////
        masterPlanFragment = new MasterPlanFragment();
        officesFragment = new OfficesFragment();
        connectivityFragment = new ConnectivityFragment();
        londonRoyalDocksFragment = new LondonRoyalDocksFragment();
        videosFragment = new VideosFragment();
        projectTeamFragment = new ProjectTeamFragment();
        ////////////////////////////////
        conMgr =  (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        netInfo= conMgr.getActiveNetworkInfo();
        apiFromServer = new FunctionsCallApiFromServer();
        sharedPreferences = context.getSharedPreferences("AccessToken",Context.MODE_PRIVATE);
        accessToken = sharedPreferences.getString("Token",null);
        accessTokenType = sharedPreferences.getString("Type",null);
        dirPath = UtilsForDownloadDataFromServer.getRootDirPath(getApplicationContext());
       // frameLayout = (FrameLayout)findViewById(R.id.mainContainter_fragment);
        params = new RelativeLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT);
        //tv_progressCounting = (TextView)findViewById(R.id.textViewProgressCount);
        //progressBar = (ProgressBar)findViewById(R.id.progressbar);
    }


    private void onclickBtnFrangment(){
        for (int i = 0; i< menuBtnIdsArray.length; i++){
            final int currentBtnSelected = i;
            menuBtnArrayList.add((Button)findViewById( menuBtnIdsArray[i]));
            try {
                menuBtnArrayList.get(currentBtnSelected).setText(AppVeriableClass.getInstance().menuDisplayNames.get(i));

            } catch (Exception e) {
                e.printStackTrace();
            }
            menuBtnArrayList.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (menuBtnArrayList.get(currentBtnSelected).getId()) {
                        case R.id.btn_masterplan:
                            setFragment(masterPlanFragment);
                            frameLayout.setLayoutParams(new RelativeLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT));
                            selectedBtnColorChange(currentBtnSelected);

                            break;
                        case R.id.btn_Offices:
                            setFragment(officesFragment);
                            frameLayout.setLayoutParams(new RelativeLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT));
                            selectedBtnColorChange(currentBtnSelected);

                            break;

                        case R.id.btn_connectivity:
                            setFragment(connectivityFragment);
                            frameLayout.setLayoutParams(new RelativeLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT));// display full screen
                            selectedBtnColorChange(currentBtnSelected);

                            break;

                        case R.id.btn_londonroyaldock:
                            setFragment(londonRoyalDocksFragment);
                          //  params.addRule(ABOVE,R.id.linearlayout); // display show above on the menu bar as per the requirement.
                            frameLayout.setLayoutParams(params);
                            selectedBtnColorChange(currentBtnSelected);

                            break;
                        case R.id.btn_videos:
                            setFragment(videosFragment);
                           // params.addRule(ABOVE,R.id.linearlayout); // display show above on the menu bar as per the requirement.
                            frameLayout.setLayoutParams(params);
                            selectedBtnColorChange(currentBtnSelected);

                            break;
                        case R.id.btn_projectteam:
                            setFragment(projectTeamFragment);
                            frameLayout.setLayoutParams(new RelativeLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT));
                            selectedBtnColorChange(currentBtnSelected);

                            break;
                    }
                }
            });
        }
    }
    // After click the menu button this function change the color of button until you press the other button.
    private void selectedBtnColorChange(int current) {
        menuBtnArrayList.get(previousClickedButton).setBackgroundColor(menuBtnArrayList.get(previousClickedButton).getContext().getResources().getColor(R.color.button_default));
        menuBtnArrayList.get(previousClickedButton).setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT));
        menuBtnArrayList.get(current).setBackgroundColor(menuBtnArrayList.get(current).getContext().getResources().getColor(R.color.button_pressed));
        if (BtnHeight == menuBtnArrayList.get(current).getHeight()){
            LinearLayout.LayoutParams params = new  LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, BtnHeight);
            menuBtnArrayList.get(current).setLayoutParams(params);
        }
        else{
            BtnHeight = menuBtnArrayList.get(current).getHeight() + 10;
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, BtnHeight);
            menuBtnArrayList.get(current).setLayoutParams(params);
        }
        previousClickedButton = current;
    }

    private void hideStatusBar(){
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }



    @Override
    public void onBackPressed(){

        if (doubleBackToExit){
            super.onBackPressed();
            return;
        }
        else
            Toast.makeText(context, "Click again to exit", Toast.LENGTH_SHORT).show();
        this.doubleBackToExit = true;


        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                doubleBackToExit = false;
            }
        },2000);
    }

    private void CallAPIFromServer(){
        if (netInfo != null) {
            apiFromServer.getMediaResourceOffices(context, activity, accessTokenType, accessToken);
            apiFromServer.getMediaResourceConnectivity(context, accessTokenType, accessToken);
            apiFromServer.getMediaResourceLondonRoyalDock(context, accessTokenType, accessToken);
            apiFromServer.getMediaResouceVideo(context, accessTokenType, accessToken);
        }

    }

    public void onResume() {
        super.onResume();
        conMgr =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        netInfo= conMgr.getActiveNetworkInfo();
    }


    private void setFragment(Fragment fragment){
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.mainContainter_fragment,fragment);
        fragmentTransaction.commit();
    }

    private List<Fragment> getFragmetns()
    {
        List<Fragment> flist = new ArrayList<>();
        flist.add(MasterPlanFragment.NewIntance());
        flist.add(OfficesFragment.NewIntance());
        flist.add(ConnectivityFragment.NewIntance());
        flist.add(VideosFragment.NewIntance());
        flist.add(ProjectTeamFragment.NewIntance());


        return flist;
    }
}
android android-viewpager android-framelayout
1个回答
0
投票

试试这个解决方案

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activities.MenuButtonActivity">

    <com.abp.app.ViewPager.NonSwipeableViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <LinearLayout
        android:id="@+id/rl_bottom_overlay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="horizontal"
        android:text="yyyyyyyy"
        android:visibility="visible">

        <Button
            android:id="@+id/btn_left_arrow"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="yyyyyyyy"/>

        <Button
            android:id="@+id/btn_below_share"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="yyyyyyyy"
            android:visibility="visible"/>

        <Button
            android:id="@+id/btn_right_arrow"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="yyyyyyyy"/>
    </LinearLayout>

</FrameLayout>

结果是。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.