У меня есть эти 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 исчезли.