gpt4 book ai didi

java - 每次单击按钮时将 View 添加到 Fragment 的 LinearLayout

转载 作者:行者123 更新时间:2023-11-29 19:37:55 25 4
gpt4 key购买 nike

我有一个按钮,在 onClickListener 我有:

Button button1 = new Button(mContext);

LinearLayout.LayoutParams x = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);

button1.setLayoutParams(x);
ll.addView(button1);

当我第一次点击按钮时,它会起作用,第二次点击(以及更多)按钮不会添加新按钮。有人可以帮助我吗?

这是我的课:

public class ListaDruzynAdapter extends ArrayAdapter {
private ArrayList<Druzyna>listaDruzyn;
private Context mContext;
private SelectedTeamsFragment selectedTeamsFragment=null;
private LinearLayout ll;
int i = 0;
public ListaDruzynAdapter(Context c,ArrayList lista){
super(c, 0, lista);
this.mContext = c;
listaDruzyn=lista;
ll=(LinearLayout)((Activity)mContext).findViewById(R.id.selectedTeamFrag);
}

@Override
public int getCount() {
return listaDruzyn.size();
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;

if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
imageView.setPadding(8, 8, 8, 8);

} else {
imageView = (ImageView) convertView;
}

if (position==0){
imageView.setImageResource(R.drawable.ic_add_24dp);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

DodajNowaDruzyneFragment dodajNowaDruzyneFragment = new DodajNowaDruzyneFragment();
dodajNowaDruzyneFragment.setListeDruzyn(ListaDruzynAdapter.this);

FragmentManager fragmentManager = ((Activity)mContext).getFragmentManager();
dodajNowaDruzyneFragment.show(fragmentManager,"ADD_NEW_TEAM_FRAGMENT");
}
});
}else{
imageView.setImageResource(R.drawable.ic_done_24dp);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (selectedTeamsFragment==null){
selectedTeamsFragment = new SelectedTeamsFragment();
FragmentTransaction ft=((Activity)mContext).getFragmentManager().beginTransaction();
ft.add(R.id.selectedTeamFrag,selectedTeamsFragment);
ft.commit();
}

Button button1 = new Button(mContext);

LinearLayout.LayoutParams x = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);

button1.setLayoutParams(x);
ll.addView(button1);

}
});
}

return imageView;
}

类是一个适配器,其中 gridd 中的第一项是“添加”按钮。添加按钮正在添加新按钮,但在另一个 fragment 中。

最佳答案

您需要为 Linearlayout 设置方向,否则它只会容纳一个 child

通过编程

ll.setOrientation(LinearLayout.VERTICAL); 

ll.setOrientation(LinearLayout.HORIZONTAL); 

并通过 XML

<LinearLayout
android:id="@+id/selectedTeamFrag"
android:orientation="vertical"
....
.... >
...
...
</LinearLayout>

<LinearLayout
android:id="@+id/selectedTeamFrag"
android:orientation="horizontal"
....
.... >
...
...
</LinearLayout>

在下面找到水平方向的示例 Activity 代码

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/horizontalScrollView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >

<LinearLayout
android:id="@+id/selectedTeamFrag"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>

主 Activity .java

public class MainActivity extends AppCompatActivity {

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

LinearLayout ll = (LinearLayout) findViewById(R.id.selectedTeamFrag);

for (int count=0; count < 10 ; count++) {
Button button1 = new Button(MainActivity.this);
button1.setText("B:"+count);
LinearLayout.LayoutParams x = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);

button1.setLayoutParams(x);
ll.addView(button1);
}
}
}

输出:-

enter image description here

关于java - 每次单击按钮时将 View 添加到 Fragment 的 LinearLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39035307/

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