Видимость в OnClickListener - PullRequest
       11

Видимость в OnClickListener

0 голосов
/ 08 сентября 2018

У меня есть эти TextViews на ImageViews. Когда вы щелкаете по ImageView, должен появиться текст в этом конкретном ImageView, а остальные из TextView в других ImageView должны исчезнуть. Как я могу это сделать?

Я знаю, что это связано с операторами if / else, но не могу понять, что именно я должен использовать с этими операторами.

public class MainActivity extends AppCompatActivity {



    //Declaring instance variable of FirebaseAuth as stated on Firebase Authentication file.
    private FirebaseAuth mAuth;
    //Declaring the Toolbar instance variable.
    private Toolbar mMainAppBar;
    //ViewPager on the activity_main.xml file
    private ViewPager mMainActivityViewPager;
    private TextView mShopTextView;
    private TextView mSocialTextView;
    private TextView mChatTextView;
    private TextView mInventoryTextView;
    private TextView mSettingsTextView;




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

        //Initializing mAuth and the .getInstance() method indicates that there is only a single instance variable for FirebaseAuth.
        mAuth = FirebaseAuth.getInstance();



        //Setting the App Bar on the MainActivity page.
        mMainAppBar = findViewById(R.id.mainToolBar);
        //setSupportActionBar a command given in AppCombatActivity to make the Actionbar a Toolbar.
        //Toolbar is a new view that was introduced to Android to make the designing of Actionbar more flexible.
        setSupportActionBar(mMainAppBar);

        mShopTextView = findViewById(R.id.shopTextView);
        mSocialTextView = findViewById(R.id.socialTextView);
        mChatTextView = findViewById(R.id.chatTextView);
        mInventoryTextView = findViewById(R.id.inventoryTextView);
        mSettingsTextView = findViewById(R.id.settingsTextView);

        mShopTextView.setVisibility(View.GONE);
        mSocialTextView.setVisibility(View.GONE);
        mChatTextView.setVisibility(View.GONE);
        mInventoryTextView.setVisibility(View.GONE);
        mSettingsTextView.setVisibility(View.GONE);



          //Setting up the clicking of ImageViews on the Bottom Navigation bar.
        //How I did it: https://teamtreehouse.com/community/starting-a-fragment-from-an-activity
        ImageView shopImageView = (ImageView) findViewById(R.id.shopImageView);

        shopImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ShopFragment shopFragment = new ShopFragment();
                FragmentTransaction transaction =
                        getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.mainActivityFrameLayout, shopFragment);
                transaction.commit();

                mShopTextView.setVisibility(View.VISIBLE);

            }
        });

        ImageView socialImageView = (ImageView) findViewById(R.id.socialImageView);

        socialImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SocialFragment socialFragment = new SocialFragment();
                FragmentTransaction transaction =
                        getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.mainActivityFrameLayout, socialFragment);
                transaction.commit();

                TextView socialTextView = (TextView) findViewById(R.id.socialTextView);
                socialTextView.setVisibility(View.VISIBLE);
            }
        });

        ImageView chatImageView = (ImageView) findViewById(R.id.chatImageView);

        chatImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ChatFragment chatFragment = new ChatFragment();
                FragmentTransaction transaction =
                        getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.mainActivityFrameLayout, chatFragment);
                transaction.commit();

                TextView chatTextView = (TextView) findViewById(R.id.chatTextView);
                chatTextView.setVisibility(View.VISIBLE);
            }
        });

        ImageView inventoryImageView = (ImageView) findViewById(R.id.inventoryImageView);

        inventoryImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                InventoryFragment inventoryFragment = new InventoryFragment();
                FragmentTransaction transaction =
                        getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.mainActivityFrameLayout, inventoryFragment);
                transaction.commit();

                TextView inventoryTextView = (TextView) findViewById(R.id.inventoryTextView);
                inventoryTextView.setVisibility(View.VISIBLE);
            }
        });

        ImageView settingsImageView = (ImageView) findViewById(R.id.settingsImageView);

        settingsImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SettingsFragment settingsFragment = new SettingsFragment();
                FragmentTransaction transaction =
                        getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.mainActivityFrameLayout, settingsFragment);
                transaction.commit();

                TextView settingsTextView = (TextView) findViewById(R.id.inventoryTextView);
                settingsTextView.setVisibility(View.VISIBLE);
            }
        });


    }

EDIT: Вот как выглядит панель: Снимок экрана .

все эти значки - ImageViews, а "hello" - TextViews. Когда я щелкаю один ImageView, я хочу, чтобы этот конкретный TextView появился, а все остальные TextView исчезли.

Ответы [ 3 ]

0 голосов
/ 08 сентября 2018
**Here I want to provide some help tips to **

if you want hide or show any element first thing is if you want show all element visible at first time you can set by default visibility to  View.VISIBLE else you can use View.Gone.
and after that in which textview or image view click you want show or hide any element you can perform inside the onClickListener() like below example.



ImageView inventoryImageView = (ImageView) findViewById(R.id.inventoryImageView);
        inventoryImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                demotext.setVisibility(View.VISIBLE);
                demoImage.setVisibility(View.Gone)
            }
        });
0 голосов
/ 08 сентября 2018

Ответ Momen Zaqout правильный
Я просто перепишу код более оптимизированным способом

открытый класс MainActivity расширяет AppCompatActivity, реализует View.OnClickListener {

//Declaring instance variable of FirebaseAuth as stated on Firebase Authentication file.
private FirebaseAuth mAuth;
//Declaring the Toolbar instance variable.
private Toolbar mMainAppBar;
//ViewPager on the activity_main.xml file
private ViewPager mMainActivityViewPager;
private  TextView mShopTextView,mSocialTextView,mChatTextView,mInventoryTextView,mSettingsTextView;
private ImageView shopImageView,socialImageView,chatImageView,inventoryImageView,settingsImageView;




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

    //Initializing mAuth and the .getInstance() method indicates that there is only a single instance variable for FirebaseAuth.
    mAuth = FirebaseAuth.getInstance();



    //Setting the App Bar on the MainActivity page.
    mMainAppBar = findViewById(R.id.mainToolBar);
    //setSupportActionBar a command given in AppCombatActivity to make the Actionbar a Toolbar.
    //Toolbar is a new view that was introduced to Android to make the designing of Actionbar more flexible.
    setSupportActionBar(mMainAppBar);

    mShopTextView = findViewById(R.id.shopTextView);
    mSocialTextView = findViewById(R.id.socialTextView);
    mChatTextView = findViewById(R.id.chatTextView);
    mInventoryTextView = findViewById(R.id.inventoryTextView);
    mSettingsTextView = findViewById(R.id.settingsTextView);

    shopImageView = (ImageView) findViewById(R.id.shopImageView);
    socialImageView = (ImageView) findViewById(R.id.socialImageView);
    chatImageView = (ImageView) findViewById(R.id.chatImageView);
    inventoryImageView = (ImageView) findViewById(R.id.inventoryImageView);
    settingsImageView = (ImageView) findViewById(R.id.settingsImageView);

    shopImageView.setOnClickListener(this);
    socialImageView.setOnClickListener(this);
    chatImageView.setOnClickListener(this);
    inventoryImageView.setOnClickListener(this);
    settingsImageView.setOnClickListener(this);

    mShopTextView.setVisibility(View.GONE);
    mSocialTextView.setVisibility(View.GONE);
    mChatTextView.setVisibility(View.GONE);
    mInventoryTextView.setVisibility(View.GONE);
    mSettingsTextView.setVisibility(View.GONE);

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.shopImageView:
                ShopFragment shopFragment = new ShopFragment();
                FragmentTransaction transaction =
                        getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.mainActivityFrameLayout, shopFragment);
                transaction.commit();

                mShopTextView.setVisibility(View.VISIBLE);
                break();

            case R.id.socialImageView:
                SocialFragment socialFragment = new SocialFragment();
                FragmentTransaction transaction =
                        getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.mainActivityFrameLayout, socialFragment);
                transaction.commit();

                mSocialTextView.setVisibility(View.VISIBLE);

                mShopTextView.setVisibility(View.GONE);
                mChatTextView.setVisibility(View.GONE);
                mInventoryTextView.setVisibility(View.GONE);
                mSettingsTextView.setVisibility(View.GONE);
                                break();


            case R.id.chatImageView:
                ChatFragment chatFragment = new ChatFragment();
                FragmentTransaction transaction =
                        getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.mainActivityFrameLayout, chatFragment);
                transaction.commit();

                mChatTextView.setVisibility(View.VISIBLE);
                mShopTextView.setVisibility(View.GONE);
                mSocialTextView.setVisibility(View.GONE);
                mInventoryTextView.setVisibility(View.GONE);
                mSettingsTextView.setVisibility(View.GONE);
                    break();


            case R.id.inventoryImageView:
                InventoryFragment inventoryFragment = new InventoryFragment();
                FragmentTransaction transaction =
                        getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.mainActivityFrameLayout, inventoryFragment);
                transaction.commit();

                mInventoryTextView.setVisibility(View.VISIBLE);
                 mShopTextView.setVisibility(View.GONE);
                mSocialTextView.setVisibility(View.GONE);
                mChatTextView.setVisibility(View.GONE);
                mSettingsTextView.setVisibility(View.GONE);
                    break();


            case R.id.settingsImageView:    
                SettingsFragment settingsFragment = new SettingsFragment();
                FragmentTransaction transaction =
                        getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.mainActivityFrameLayout, settingsFragment);
                transaction.commit();

                mSettingsTextView.setVisibility(View.VISIBLE);      
                mShopTextView.setVisibility(View.GONE);
                mSocialTextView.setVisibility(View.GONE);
                mChatTextView.setVisibility(View.GONE);
                mInventoryTextView.setVisibility(View.GONE);
                    break();

        }




}
0 голосов
/ 08 сентября 2018

Вы можете использовать этот метод, чтобы закрыть все текстовые просмотры, а затем появиться желаемый текстовый просмотр.

void changeTexViews(View desired ){

       mShopTextView.setVisibility(View.GONE);
        mSocialTextView.setVisibility(View.GONE);
        mChatTextView.setVisibility(View.GONE);
        mInventoryTextView.setVisibility(View.GONE);
        mSettingsTextView.setVisibility(View.GONE);

        desired .setVisibility(View.VISIBLE);

}

и в каждом ImageView.setOnClickListener используйте этот метод,

например

shopImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ShopFragment shopFragment = new ShopFragment();
                FragmentTransaction transaction =
                        getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.mainActivityFrameLayout, shopFragment);
                transaction.commit();

                changeTexViews(mShopTextView);
            }
        });
...