Java 当用户向下滚动或添加新视图时,FloatingActionButton向下滚动
我目前正在开发一个大学应用程序,其中1个UI线程负责添加学期按钮。当应用程序旋转并单击FloatingAction按钮以添加视图时,在视图进入屏幕下方后,每次单击时按钮都会向下滚动。我试图调整我的XML,但不幸的是什么也没发生。我的问题是,当用户上下滚动时,如何使我的晶圆厂保持在屏幕上的相同位置 这是我的UI线程:Java 当用户向下滚动或添加新视图时,FloatingActionButton向下滚动,java,android,xml,Java,Android,Xml,我目前正在开发一个大学应用程序,其中1个UI线程负责添加学期按钮。当应用程序旋转并单击FloatingAction按钮以添加视图时,在视图进入屏幕下方后,每次单击时按钮都会向下滚动。我试图调整我的XML,但不幸的是什么也没发生。我的问题是,当用户上下滚动时,如何使我的晶圆厂保持在屏幕上的相同位置 这是我的UI线程: public class MainActivity extends AppCompatActivity { int counter = 0; FloatingAc
public class MainActivity extends AppCompatActivity {
int counter = 0;
FloatingActionButton addingSemester;
Button semesterButton;
LinearLayout semesterLayout;
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
AppBarLayout.LayoutParams.MATCH_PARENT,
AppBarLayout.LayoutParams.WRAP_CONTENT);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addingSemester = (FloatingActionButton) findViewById(R.id.addActionButton);
semesterLayout = (LinearLayout) findViewById(R.id.main_layout);
semesterButton = new Button(MainActivity.this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.delete) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete everything?")
.setCancelable(true)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
semesterLayout.removeAllViews();
counter = 0;
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.show();
return true;
}
return super.onOptionsItemSelected(item);
}
public void onFloatActionButtonClick(View view) {
semesterButton = new Button(MainActivity.this);
if (counter < 8) {
semesterButton.setId(counter + 1);
semesterButton.setText("Semester " + (counter + 1));
semesterButton.setBackgroundColor(getColor(R.color.colorPrimary));
semesterButton.setTextColor(Color.WHITE);
lp.setMargins(24, 24, 24, 24);
semesterButton.setLayoutParams(lp);
semesterLayout.addView(semesterButton);
counter++;
semesterButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
final Button b = (Button) v;
b.setTag(b.getText().toString());
b.setBackgroundColor(Color.RED);
b.setText("Delete");
new AlertDialog.Builder(MainActivity.this)
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this entry?")
.setCancelable(true)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
semesterLayout.removeView(b);
counter--;
for (int i = 0; i < semesterLayout.getChildCount(); i++) {
((Button) semesterLayout.getChildAt(i)).setText("Semester " + (i + 1));
}
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
b.cancelLongPress();
b.setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary));
b.setText(b.getTag().toString());
dialog.cancel();
}
})
.show();
return true;
}
});
} else if (counter == 8) {
Toast.makeText(MainActivity.this, "You cannot add more than 8 semesters", Toast.LENGTH_SHORT).show();
}
}
}
public类MainActivity扩展了AppCompatActivity{
int计数器=0;
浮动操作按钮添加学期;
按钮-按钮;
线性布局;
RelativeLayout.LayoutParams lp=新的RelativeLayout.LayoutParams(
AppBarLayout.LayoutParams.MATCH_父级,
AppBarLayout.LayoutParams.WRAP_内容);
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AddingSerm=(FloatingActionButton)findViewById(R.id.addActionButton);
semesterLayout=(LinearLayout)findviewbyd(R.id.main\u布局);
semesterButton=新按钮(MainActivity.this);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
MenuInflater MenuInflater=getMenuInflater();
菜单充气器(右菜单主菜单);
返回super.onCreateOptions菜单(菜单);
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
int id=item.getItemId();
if(id==R.id.delete){
新建AlertDialog.Builder(MainActivity.this)
.setTitle(“删除条目”)
.setMessage(“确实要删除所有内容吗?”)
.setCancelable(真)
.setPositiveButton(“是”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
semesterLayout.removeAllViews();
计数器=0;
}
})
.setNegativeButton(“否”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
dialog.cancel();
}
})
.show();
返回true;
}
返回super.onOptionsItemSelected(项目);
}
public void on floatAction按钮单击(查看){
semesterButton=新按钮(MainActivity.this);
如果(计数器<8){
设置ID(计数器+1);
setText(“学期”+(计数器+1));
setBackgroundColor(getColor(R.color.colorPrimary));
semesterButton.setTextColor(Color.WHITE);
lp.设定利润(24,24,24,24);
SeMeterButton.setLayoutParams(lp);
semesterLayout.addView(semesterButton);
计数器++;
semesterButton.setOnLongClickListener(新视图。OnLongClickListener(){
@凌驾
仅长按公共布尔值(视图v){
最终按钮b=(按钮)v;
b、 setTag(b.getText().toString());
b、 setBackgroundColor(颜色:红色);
b、 setText(“删除”);
新建AlertDialog.Builder(MainActivity.this)
.setTitle(“删除条目”)
.setMessage(“确实要删除此条目吗?”)
.setCancelable(真)
.setPositiveButton(“是”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
semesterLayout.removeView(b);
计数器--;
对于(int i=0;i
这是我的XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
tools:context="myapp.onur.journeygpacalculator.MainActivity">
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<LinearLayout
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dp">
</LinearLayout>
</ScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/addActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_margin="16dp"
android:clickable="true"
android:longClickable="true"
android:onClick="onFloatActionButtonClick"
android:src="@drawable/ic_add_black_48dp"
android:tint="@color/colorWhite"
app:backgroundTint="@color/colorPrimary"
app:layout_behavior="com.myapp.onur.journeygpacalculator.ScrollingFABBehavior"
android:elevation="6dp"
app:borderWidth="0dp"/>
</RelativeLayout>
这里有问题吗?对不起。我刚刚编辑了它。