如何使用GridLayout创建对话框?

时间:2019-10-25 21:27:49

标签: android dialog android-gridlayout

如何创建使用GridLayout的对话框,以及当用户单击此布局的项目然后单击“选择”按钮时。 GridLayout项包含一个ImageView。如何实现此对话框?

2 个答案:

答案 0 :(得分:0)

1-制作所需的自定义布局

2-创建myDialog类,该类扩展了DialogFragment并使它膨胀到您创建的布局中

3-在片段中添加所需的功能

但是我正在使用gridview,gridlayout比较简单,但是我建议使用gridview

这里是Dialog类

public class myDialog extends DialogFragment implements dis_listener {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.my_dialog_layout,container,false);
    GridView gv = view.findViewById(R.id.dialog_grid);

    grid_adapt adapt = new grid_adapt(getActivity(),this);
    gv.setAdapter(adapt);
    return view;



}
@Override
public void listen() {
    this.dismiss();
}
}

grid_adapt:-

public class grid_adapt extends BaseAdapter {
private Context cxt;
//pass the data you want
//private ArrayList<Integers> imgs
private dis_listener img_dismiss;
grid_adapt(Context context,dis_listener dismisser){
    this.cxt = context;
    this.img_dismiss = dismisser;
}
@Override
public int getCount() {
    //return the data length
    //return imgs.length
    return 3;

}

@Override
public Object getItem(int position) {
    //return imgs.get(position);
    return position;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    viewHolder vh;
    if(convertView == null) {
        convertView= LayoutInflater.from(cxt).inflate(R.layout.grid_item, parent, false);
        vh = new viewHolder(convertView);
        convertView.setTag(vh);

    }else{
        vh = (viewHolder) convertView.getTag();
    }
    vh.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(cxt,"Open",Toast.LENGTH_LONG).show();
        }
    });
     vh.img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            img_dismiss.listen();
        }
    });
    return convertView;
}
static class viewHolder{
    private Button button;
    private ImageView img;
    viewHolder(View view){
        button = view.findViewById(R.id.dialog_item_button);
        img = view.findViewById(R.id.img);
    }

}

}

该接口只有一种名为listen

的方法

结果: enter image description here

答案 1 :(得分:0)

GridView gridView = new GridView(this);
gridView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, your_list));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // do something here
            }
 });
 AlertDialog.Builder builder = new AlertDialog.Builder(this);
 builder.setView(gridView);
 builder.setTitle("ALERT BOX WITH GRID");
 builder.show();

尝试一下,这基于单列数据网格..

如果您想要图片,请按照以下步骤操作: https://android-developers.googleblog.com/2012/05/using-dialogfragments.html