gpt4 book ai didi

android - imageView 选中状态不显示

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:10:24 25 4
gpt4 key购买 nike

在我的应用程序中,我有一个 Activity (HomeActivity) 和 3 个 fragment (所以我有 3 个按钮)。在 HomeActivity 中,我有一些按钮,点击它后应该改变他的状态。第一次运行应用程序时,我按下其中一个按钮,选择状态发生了变化(在我的例子中,白色变为绿色),但是当我按下另一个按钮时,所有按钮都未被选中(所有按钮都是白色的)。谁能告诉我这里有什么问题?

public class HomeActivity implements View.OnClickListener {

private ImageView mMessageButton;
private ImageView mMapButton;
private ImageView mSettingsButton;
private int mCurrentFragmentId = DISCOUNT;
public List<ImageView> mPageItems;
public int mSelectedPage = 0;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);

init();
}
private void init(){
mMessageButton = (ImageView)findViewById(R.id.message_icon);
mMapButton = (ImageView)findViewById(R.id.map_icon);
mSettingsButton = (ImageView)findViewById(R.id.settings_icon);
mPageItems = new ArrayList<ImageView>();

mPageItems.add(mMessageButton);
mPageItems.add(mMapButton);
mPageItems.add(mSettingsButton);

mMessageButton.setOnClickListener(this);
mMapButton.setOnClickListener(this);
mSettingsButton.setOnClickListener(this);
}

public void addPage(final DefaultHeaderFragment pDefaultFragment, final boolean isAddToBackStack){
FragmentTransaction transaction = mFragmentManager.beginTransaction();
mPageItems.get(mSelectedPage).setSelected(true);
transaction.replace(R.id.f_pager, pDefaultFragment);
if (isAddToBackStack) transaction.addToBackStack(null);
transaction.commit();
}

@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.message_icon:
mSelectedPage = 0;
MessagesFragment messagesFragment = new MessagesFragment(HomeActivity.this);
Bundle messageArgument = new Bundle();
messageArgument.putInt("fragmentId", mCurrentFragmentId);
messagesFragment.setArguments(messageArgument);
addPage(messagesFragment, true);
break;
case R.id.map_icon:
mSelectedPage = 1;
YandexMapFragment mapFragment = new YandexMapFragment(HomeActivity.this);
Bundle mapArgument = new Bundle();
mapArgument.putInt("fragmentId", mCurrentFragmentId);
mapFragment.setArguments(mapArgument);
addPage(mapFragment, true);
break;
case R.id.settings_icon:
mSelectedPage = 2;
SettingsFragment settingsFragment = new SettingsFragment(HomeActivity.this);
Bundle settingsArgument = new Bundle();
settingsArgument.putInt("fragmentId", mCurrentFragmentId);
settingsFragment.setArguments(settingsArgument);
addPage(settingsFragment, true);
break;
}
}
}

主页.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<ImageView
android:id="@+id/message_icon"
android:layout_width="wrap_content"
android:layout_toLeftOf="@+id/map_icon"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/header_margin_between_icons"
android:layout_centerInParent="true"
android:src="@drawable/button_indicator_message_button"/>
<ImageView
android:id="@id/map_icon"
android:layout_width="wrap_content"
android:layout_toLeftOf="@+id/settings_icon"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/header_margin_between_icons"
android:layout_centerInParent="true"
android:src="@drawable/button_indicator_map_button"/>
<ImageView
android:id="@id/settings_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginRight="@dimen/header_marginLeft_marginRight"
android:layout_alignParentRight="true"
android:src="@drawable/button_indicator_settings_button"/>
</RelativeLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id = "@+id/f_pager"/>

</RelativeLayout>

button_indicator_message_button

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/message_pressed"/>
<item android:state_selected="true" android:drawable="@drawable/message_pressed"/>
<item android:drawable="@drawable/message_unpressed"/>
</selector>

最佳答案

onClick 中,您应该调用 view.setSelected(true); 以强制更改 ImageView 的内容

关于android - imageView 选中状态不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17630273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com