我的应用包含一些(Home)
,它们应该水平滚动。我的textViews
首次加载但点击layout
加载另一个button
然后重新点击layout
加载第一个{{1} }} 那些button
在" big"之后开始滚动延迟(如20秒+)。我试图用layout
,textViews
等来弄清楚这个问题的来源,但没有运气。
更新:我今天注意到一些奇怪的事情。当我从第一个布局转到第二个布局时,反之亦然,尽管TextViews停止滚动,如果我关闭并重新打开手机&#39 ; s屏幕,滚动效果就像一个魅力。这对我来说似乎很奇怪,你知道造成这种情况的原因和原因吗?
CardViewActivity.java:
Layout Inspector
firstLayout.xml:
Hierarchy Viewer
SecondLayout.xml:
public class CardViewActivity extends AppCompatActivity {
private ImageView cardArtImageView;
private TextView leaderSkillDescText;
private TextView superAttackTitleText;
private TextView superAttackDescText;
private TextView passiveSkillTitleText;
private TextView passiveSkillDescText;
private TextView hpText;
private TextView attText;
private TextView defText;
private TextView costText;
private Button arrowButton;
private int selectedItemPosition;
private boolean isBtnClicked = false;
// Listener member field for each layout's button. This listener will be used recursively
private View.OnClickListener arrowButtonListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
// When the arrowButton is clicked, choose the right layout based on the button's state
int resID = isBtnClicked ? R.layout.cardview_refined : R.layout.cardview_expand_details;
setContentView(resID);
// If we're in the first layout, initialize the cardArtImageView field
if(isBtnClicked) {
cardArtImageView = findViewById(R.id.cardArtImageView);
}
viewDefinitions(!isBtnClicked);
initCardViewData(selectedItemPosition);
setSelectedViewsInit();
// Set the arrowButton's listener to this listener (recursively)
arrowButton.setOnClickListener(arrowButtonListener);
// toggle our flag field
isBtnClicked = !isBtnClicked;
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cardview_refined);
// Retrieving the data sent over from MainActivity
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle != null) {
selectedItemPosition = bundle.getInt("Card Index");
}
//Toast.makeText(this, "WIDTH: " + SCREEN_WIDTH, Toast.LENGTH_SHORT).show();
// Initializing our views
cardArtImageView = findViewById(R.id.cardArtImageView);
viewDefinitions(false);
setSelectedViewsInit();
initCardViewData(selectedItemPosition);
arrowButton.setOnClickListener(arrowButtonListener);
}
/**
* Sets the required textViews as selected to allow automatic scrolling
*/
private void setSelectedViewsInit() {
leaderSkillDescText.setSelected(true);
superAttackTitleText.setSelected(true);
superAttackDescText.setSelected(true);
if (passiveSkillTitleText != null && passiveSkillDescText != null) {
passiveSkillTitleText.setSelected(true);
passiveSkillDescText.setSelected(true);
}
}
/**
* Adds the views's definitions
*
* @param initPassiveInfo used to decide whether or not the passiveSkillDesc & ..Title != null
* so that they can be defined
*/
private void viewDefinitions(boolean initPassiveInfo) {
leaderSkillDescText = findViewById(R.id.leaderSkillDesc);
superAttackTitleText = findViewById(R.id.superAttackTitle);
superAttackDescText = findViewById(R.id.superAttackDesc);
if (initPassiveInfo) {
passiveSkillTitleText = findViewById(R.id.passiveSkillTitle);
passiveSkillDescText = findViewById(R.id.passiveSkillDesc);
} else {
Log.d("Definitions", "Passive info == null");
}
hpText = findViewById(R.id.HP);
attText = findViewById(R.id.ATT);
defText = findViewById(R.id.DEF);
costText = findViewById(R.id.COST);
arrowButton = findViewById(R.id.arrowButton);
}
/**
* Initialize the cardViewActivity's views with the data from the CardInfoDatabase.java class
*
* @param selectedItemPosition Used to initialize this activity's views if the intent was called from the MainScreen Fragment
*/
private void initCardViewData(int selectedItemPosition) {
if (cardArtImageView != null) {
cardArtImageView.setImageResource(CardInfoDatabase.cardArts[selectedItemPosition]);
}
leaderSkillDescText.setText(CardInfoDatabase.leaderSkills[selectedItemPosition]);
superAttackTitleText.setText(CardInfoDatabase.superAttacksName[selectedItemPosition]);
superAttackDescText.setText(CardInfoDatabase.superAttacksDesc[selectedItemPosition]);
if (passiveSkillTitleText != null && passiveSkillDescText != null) {
passiveSkillTitleText.setText(CardInfoDatabase.passiveSkillsName[selectedItemPosition]);
passiveSkillDescText.setText(CardInfoDatabase.passiveSkillsDesc[selectedItemPosition]);
}
hpText.setText(CardInfoDatabase.hp[selectedItemPosition].toString());
attText.setText(CardInfoDatabase.att[selectedItemPosition].toString());
defText.setText(CardInfoDatabase.def[selectedItemPosition].toString());
costText.setText(CardInfoDatabase.cost[selectedItemPosition].toString());
}
}
答案 0 :(得分:1)
我建议使用带有自定义适配器的RecyclerView,可以垂直或水平使用RecyclerView。