Android UI - 动态添加按钮到Gridview [英] Android UI - add buttons dynamically to Gridview

查看:204
本文介绍了Android UI - 动态添加按钮到Gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我现在卡住的地方。我被卡在动态添加按钮到gridview。

- 我的gridview星星与一个按钮。

- 当用户点击该按钮的上下文菜单弹出,要求用户输入信息,一旦完成。网格视图中的块是使用该信息创建的

- 如图所示



我已粘贴代码。我对如何做到这一点并不清楚。我假设它会膨胀一个新的视图,并将其添加到父(布局或网格?)视图。我没有太多的想法如何编写它。我从谷歌尝试过很多东西。我想我可以从简单的图像网格视图开始,并将其修改为我的需要,但是它不起作用。



请提供一些方向。





create_team_new.xml(gridview所在的布局)

 <?xml version =1.0encoding =utf-8 ?> 
< RelativeLayout
android:id =@ + id / parentcreateteam
android:layout_width =fill_parent
android:layout_height =fill_parent
xmlns: android =http://schemas.android.com/apk/res/android
android:background =@ drawable / background>

< TextView
android:id =@ + id / citytextview
android:layout_width =wrap_content
android:layout_height =wrap_content
android:text =@ string / city
android:layout_alignTop =@ + id / teamcity
android:layout_alignParentLeft =true/>

< AutoCompleteTextView
android:id =@ + id / teamname
android:layout_width =244dp
android:layout_height =wrap_content
android:layout_alignLeft =@ + id / teamcity
android:layout_alignRight =@ + id / gridviewplayer
android:text =@ string / teamname
android:textSize = 18sp>
< requestFocus />
< / AutoCompleteTextView>
< Button
android:id =@ + id / createteam
android:layout_width =140dp
android:layout_height =wrap_content
android: text =创建团队
android:layout_alignParentBottom =true
android:layout_centerHorizo​​ntal =true
android:background =@ drawable / red_button
style =@ + style / button_text/>

< TextView
android:id =@ + id / teamnametextview
android:layout_width =wrap_content
android:layout_height =22dp
android:layout_alignBaseline =@ + id / teamname
android:layout_alignBottom =@ + id / teamname
android:layout_alignParentLeft =true
android:text =@ string / teamname/>

< AutoCompleteTextView
android:id =@ + id / teamcity
android:layout_width =244dp
android:layout_height =wrap_content
android:layout_alignLeft =@ + id / cricket
android:layout_alignRight =@ + id / gridviewplayer
android:layout_below =@ + id / teamname
android:layout_marginTop =22dp
android:ems =10
android:text =@ string / city
android:textSize =18sp>


< / AutoCompleteTextView>

< TextView
android:id =@ + id / radiotextview
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_alignParentLeft =true
android:layout_below =@ + id / teamcity
android:layout_marginTop =15dp
android:text =选择运动/> ;


< GridView
android:id =@ + id / gridviewplayer
android:layout_width =match_parent
android:layout_height = 170dp
android:layout_above =@ + id / createteam
android:layout_below =@ + id / pick_sport
android:layout_centerHorizo​​ntal =true
android:background =@ drawable / gv_bkg
android:padding =5dp
android:numColumns =4>

< / GridView>



< RadioGroup
android:id =@ + id / pick_sport
android:layout_width =wrap_content
android :layout_height =wrap_content

android:layout_alignRight =@ + id / createteam
android:layout_below =@ + id / radiotextview
android:orientation =horizo​​ntal >

< RadioButton
android:id =@ + id / cricket
android:layout_width =wrap_content
android:layout_height =wrap_content
android:text =Cricket/>

< RadioButton
android:id =@ + id / soccer
android:layout_width =wrap_content
android:layout_height =wrap_content
android:text =Soccer/>
< / RadioGroup>

< / RelativeLayout>

gv_createplayer.xml(视图要在gridview中被放大)

 <?xml version =1.0encoding =utf-8?> 

< LinearLayout xmlns:android =http://schemas.android.com/apk/res/android
android:orientation =vertical
android:layout_width =fill_parent
android:layout_height =fill_parent
>

< Button android:id =@ + id / btn_player
android:layout_width =match_parent

android:layout_height =wrap_content

android:text =+

android:background =@ drawable / red_button>

< / Button>
< / LinearLayout>

PlayerAdapter.java(gridview适配器)

  import android.content.Context; 
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;

public class PlayerAdapter extends BaseAdapter {

private Context mContext;
LayoutInflater inflater;

//保持数组中的所有图像
public Integer [] mThumbIds = {};
/ * R.drawable.gridview_createteam,R.drawable.pic2,
R.drawable.pic3,R.drawable.pic4,
R.drawable.pic5,R.drawable.pic6,
R.drawable.pic7,R.drawable.pic8,
R.drawable.pic9,R.drawable.pic10,
R.drawable.pic11,R.drawable.pic12,
R.drawable.pic13,R.drawable.pic14,
R.drawable.pic15
};
* /
//构造函数
public PlayerAdapter(Context c){
mContext = c;
}

@Override
public int getCount(){
return mThumbIds.length;
}

@Override
public Object getItem(int position){
return mThumbIds [position];
}

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

@SuppressWarnings(static-access)
@Override
public View getView(int position,View convertView,ViewGroup parent){
View view = new View(mContext);
view.inflate(mContext,R.layout.gv_createplayer,null);

返回视图;


}


}


解决方案


1)如何在主视图中创建列表视图,只使用一半的屏幕。


这是由weight属性完成的。草图的粗略示例:顶部包装(edittexts和按钮)的权重为.45,列表视图的权重为.45,页脚的权重为.1。您可以在周围视图中指定权重 - 但是默认值应该是1。


2)如何以网格方式填充列表视图(一列两列将重复本身) p>

嗯,使用 GridView (适配器API是一样的)(:


This is where I am stuck now. I am stuck at dynamically adding buttons to gridview.
- My gridview stars with a one button.
- When user clicks that button a context menu , pops up, asking user to enter info, once that is done. A block in the grid view is created with that info
- This is shown in the picture

I have pasted the code. I do not have clear idea on how to do it. I am assuming it will be inflating a new view and adding it to parent(layout or grid ?) view. I do not have much idea on how to code it. I have tried many things from google. I thought I could start from simple image gridview and modify it to my needs but it did not work.

Please provide some directions.

create_team_new.xml(layout where gridview resides)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/parentcreateteam"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
     android:background="@drawable/background">

<TextView
    android:id="@+id/citytextview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/city"
    android:layout_alignTop="@+id/teamcity"
    android:layout_alignParentLeft="true" />

<AutoCompleteTextView
    android:id="@+id/teamname"
    android:layout_width="244dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/teamcity"
    android:layout_alignRight="@+id/gridviewplayer"
    android:text="@string/teamname"
    android:textSize="18sp" >
<requestFocus />
</AutoCompleteTextView>
<Button
    android:id="@+id/createteam"
    android:layout_width="140dp"
    android:layout_height="wrap_content"
    android:text="Create Team"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
     android:background="@drawable/red_button"
          style="@+style/button_text" />

<TextView
    android:id="@+id/teamnametextview"
    android:layout_width="wrap_content"
    android:layout_height="22dp"
    android:layout_alignBaseline="@+id/teamname"
    android:layout_alignBottom="@+id/teamname"
    android:layout_alignParentLeft="true"
    android:text="@string/teamname" />

<AutoCompleteTextView
    android:id="@+id/teamcity"
    android:layout_width="244dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/cricket"
    android:layout_alignRight="@+id/gridviewplayer"
    android:layout_below="@+id/teamname"
    android:layout_marginTop="22dp"
    android:ems="10"
    android:text="@string/city"
    android:textSize="18sp" >


</AutoCompleteTextView>

<TextView
    android:id="@+id/radiotextview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/teamcity"
    android:layout_marginTop="15dp"
    android:text="Pick a Sport" />


<GridView
    android:id="@+id/gridviewplayer"
    android:layout_width="match_parent"
    android:layout_height="170dp"
    android:layout_above="@+id/createteam"
    android:layout_below="@+id/pick_sport"
    android:layout_centerHorizontal="true"
    android:background="@drawable/gv_bkg"
 android:padding="5dp"
    android:numColumns="4" >

</GridView>



<RadioGroup
    android:id="@+id/pick_sport"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:layout_alignRight="@+id/createteam"
    android:layout_below="@+id/radiotextview"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/cricket"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cricket" />

    <RadioButton
        android:id="@+id/soccer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Soccer" />
</RadioGroup>

</RelativeLayout>

gv_createplayer.xml(view to be inflated inside gridview)

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<Button  android:id="@+id/btn_player"
    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:text= "+"

    android:background="@drawable/red_button">

</Button>
</LinearLayout>

PlayerAdapter.java(gridview Adapter)

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;

public class PlayerAdapter extends BaseAdapter {

    private Context mContext;
    LayoutInflater inflater;

    // Keep all Images in array
    public Integer[] mThumbIds = {};
        /*  R.drawable.gridview_createteam, R.drawable.pic2,
            R.drawable.pic3, R.drawable.pic4,
            R.drawable.pic5, R.drawable.pic6,
            R.drawable.pic7, R.drawable.pic8,
            R.drawable.pic9, R.drawable.pic10,
            R.drawable.pic11, R.drawable.pic12,
            R.drawable.pic13, R.drawable.pic14,
            R.drawable.pic15
    };
*/
    // Constructor
    public PlayerAdapter(Context c){
        mContext = c;
    }

    @Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

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

    @SuppressWarnings("static-access")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = new View(mContext);
        view.inflate(mContext, R.layout.gv_createplayer,null);

        return view;


    }


}

解决方案

1) How to create a list view inside a main view, only using a half of the screen.

This is done by the weight attribute. A rough example for your sketch: The top wrapper (edittexts and buttons) has a weight of .45, the listview has a weight of .45 and the footer has a weight of .1. You can specify the weight-sum in the surrounding view; but default should be 1.

2) How to populate list view in grid manner (a row of two columns will repeat itself)

Well, with the GridView (the adapter API is the same) (:

这篇关于Android UI - 动态添加按钮到Gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆