使用警报对话框或自定义对话框显示自定义gridview

时间:2015-02-05 16:10:33

标签: android android-custom-view android-gridview

我只想在GridView框中显示此自定义AlertDialog。 但我不知道如何做到这一点。 我的GridView自定义是:

MyActivity:

public class MyActivity extends Activity{
    private GridviewAdapter mAdapter;
    private ArrayList<String> taxtVlue;
    private ArrayList<Integer> imageValue;
    private DisplayMetrics metrics;

    private GridView gridView;
    public TextView txtViewTitle;
    public Typeface tp;



    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        //prepareing list
        taxtVlue=GridViewValues.taxtVlue();
        imageValue=GridViewValues.imageValue();

        metrics = new DisplayMetrics();

        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        // prepared arraylist and passed it to the Adapter class

        mAdapter = new GridviewAdapter(this,taxtVlue, imageValue, metrics );

        // Set custom adapter to gridview



    }

GridviewAdapter.java:

public class GridviewAdapter extends BaseAdapter {
    private ArrayList<String> listCountry;
    private ArrayList<Integer> listFlag;
    private Activity activity;
    private DisplayMetrics metrics_;

    public GridviewAdapter(Activity activity, ArrayList<String> listCountry, ArrayList<Integer> listFlag, DisplayMetrics metrics) {
        super();
        this.listCountry = listCountry;
        this.listFlag = listFlag;
        this.activity = activity;
        this.metrics_ = metrics;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listCountry.size();
    }

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return listCountry.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public static class ViewHolder {
        public ImageView imgViewFlag;
        public TextView txtViewTitle;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();

        if (convertView == null) {
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.gridview_row, null);

            view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
            view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);

            convertView.setTag(view);
        } else {
            view = (ViewHolder) convertView.getTag();
        }
        Animation animation = null;
        //animation = new TranslateAnimation(metrics_.widthPixels / 2, 0, 0, 0);
        animation = AnimationUtils.loadAnimation(activity.getBaseContext(), R.anim.push_up_in);

        animation.setDuration(500);
        convertView.startAnimation(animation);

        view.txtViewTitle.setText(listCountry.get(position));
        view.imgViewFlag.setImageResource(listFlag.get(position));


        return convertView;
    }
}

}

我只是想当用户点击一些东西时会弹出一个AlertDialog或任何类型的Dialog,我的GridView会在对话框中显示。换句话说,我只想创建一些这样的东西:

private void showAlertDialog() {
    // Prepare grid view
    GridView gridView = new GridView(this);

    List<Integer>  mList = new ArrayList<Integer>();
    for (int i = 1; i < 36; i++) {
        mList.add(i);
    }

    gridView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, mList));
    gridView.setNumColumns(5);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // do something here
        }
    });

    // Set grid view to alertDialog
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(gridView);
    builder.setTitle("Goto");
    builder.show();
}

但是因为我使用了自定义GridView,所以我不知道如何在AlertDialog中使用它。

1 个答案:

答案 0 :(得分:0)

为警报/弹出窗口创建自定义布局

popup.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<!-- Any other view as per your need -->

    <GridView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="@string/grid" />    

<!-- Any other view as per your need -->

</LinearLayout>

然后OnClick打开此布局

方法1

使用对话框....

button.setOnClickListener(new OnClickListener(){

  @Override
  public void onClick(View arg0) {

    // custom dialog
    final Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.custom);
    dialog.setTitle("Title...");

    GridView gridView= (GridView)dialog.findViewById(R.id.grid);

gridView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, mList));
    gridView.setNumColumns(5);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // do something here
        }
    });

    dialog.show();
  }
});

更多细节可以在这里找到

http://www.mkyong.com/android/android-custom-dialog-example/

方法2

使用弹出窗口...

    btnOpenPopup.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    LayoutInflater layoutInflater 
     = (LayoutInflater)getBaseContext()
      .getSystemService(LAYOUT_INFLATER_SERVICE);  
    View popupView = layoutInflater.inflate(R.layout.popup, null);  

/*
 final PopupWindow popupWindow = new PopupWindow(
               popupView, 
               LayoutParams.WRAP_CONTENT,  
                     LayoutParams.WRAP_CONTENT);  
*/

     GridView gridView= (GridView)popupView.findViewById(R.id.grid);

gridView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, mList));
    gridView.setNumColumns(5);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // do something here
        }
    });


             popupWindow.showAsDropDown(btnOpenPopup, 50, -30);

   }});
    }

此处可以找到更多详情

http://android-er.blogspot.in/2012/03/example-of-using-popupwindow.html