每次单击按钮时,都会将视图添加到Fragment的LinearLayout

时间:2016-08-19 09:14:17

标签: java android view add

我有一个按钮,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;
}

}

Class是一个适配器,其中gridd中的第一项是&#34; add&#34;纽扣。添加的按钮正在添加新的按钮,但在另一个片段中。

1 个答案:

答案 0 :(得分:1)

您需要为Linearlayout设置方向,否则它只会托管一个孩子

以编程方式

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_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);
        }
    }
}

输出: -

enter image description here