I have a button, in onClickListener
I have:
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);
It works when I click first time the button, second click (and more) the button desn't add new button. Can somebody help me?
This is my class:
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;
}
}
Class is an adapter where first item in gridd is "add" buttons. Added buttons are adding new one but in another fragment.
you need set orientation for Linearlayout otherwise it will host only one child
through programatically
ll.setOrientation(LinearLayout.VERTICAL);
or
ll.setOrientation(LinearLayout.HORIZONTAL);
And through XML
<LinearLayout
android:id="@+id/selectedTeamFrag"
android:orientation="vertical"
....
.... >
...
...
</LinearLayout>
or
<LinearLayout
android:id="@+id/selectedTeamFrag"
android:orientation="horizontal"
....
.... >
...
...
</LinearLayout>
find below sample Activity code for Horizontal orientation
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>
MainActivity.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);
}
}
}
Output : -
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.