Android弹出网格视图

时间:2014-02-19 08:26:44

标签: android

可以使用网格视图创建弹出窗口。例如,我要点击一个按钮,弹出一个网格视图,然后点击其中一个项目转移到我的图像视图,我尝试了很多教程,而不是其中一个适合我。我是Android开发的新手请帮忙或任何建议或样本片段会提前谢谢你..

3 个答案:

答案 0 :(得分:4)

有可能。对于我看来,最简单的方法是使用你想要的布局创建DialogFragment子类

对话框将如下所示

public class GridDialogFragment extends DialogFragment {

GridView mGridView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); // it title is not needed

        View view = inflater.inflate(R.layout.grid_dialog, container); // you should define dialog layout in resources

            mGridView = (GridView)view.findViewById(R.id.grid);

            //initialize your gridview

            // ...
            return view;

       }

       //here can be other methods 

}

您的布局可能如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/selectable_background_mainstyle"
    android:orientation="vertical" >

<!-- here can be other elements -->

    <GridView
     android:id="@+id/grid"
     android:layout_width="match_parent"
    android:layout_height="match_parent"/>

   <!-- here can be other elements -->

</LinearLayout>

比你可以用下一个方式显示对话框:

GridDialogFragment fr = new GridDialogFragment();
fr.setTitle(file.getName()); //Set title if needed

fr.show(getFragmentManager(), "tag");

如果您的min sdk级别低于11

,请不要忘记将支持库添加到项目中

答案 1 :(得分:3)

您可以使用对话框主题创建一个活动,并且不会在您的网格中创建标题,然后在主活动中为这个新的“弹出”活动执行startActivityForResult,

如果您不知道如何设置对话框主题,没有标题请求或如何控制结果的活动让我知道,我会扩展答案

宣言上的活动声明:

<activity android:name=".GridActivity" 
        android:theme="@android:style/Theme.Dialog" />

这样活动看起来有点像弹出窗口。使用您想要的XML进行布局,在您认为您将使用gridView

的情况下

当您需要弹出窗口显示时,您应该调用您的活动。例如,您将在调用按钮时调用弹出窗口:

public static final int GRID_REQUEST = 1; 

@Override
public void onClick(View v) {
    Intent intent = new Intent(MainActivity.this, GridActivity.class);
    startActivityForResult(intent,GRID_REQUEST);
}

通过它,您可以使用代码启动活动。在返回活动时,您将需要该代码来识别活动。

在GridActivity中,当你完成所做的事情时,你必须关闭它才能得到结果。我认为你需要给出一个图像的路径,所以我会这样做:

Intent returnIntent = new Intent();
returnIntent.putExtra("imagePath", selectedImagePath);
setResult(RESULT_OK,returnIntent);     
finish();

这将关闭弹出窗口并将选定的图像路径发送回上一个活动。要接收数据,您需要覆盖主要活动中的onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (requestCode == GRID_REQUEST) {
  if(resultCode == RESULT_OK) {      
     String selectedImagePath =data.getStringExtra("imagePath");          
     //TODO: Set the image in your imageview
   }
   if (resultCode == RESULT_CANCELED) {    
     showError("The selection was cancelled");
   }
 }
}

希望有所帮助

答案 2 :(得分:1)

是的,有可能。我会给你一些想法 - 但你需要做的工作。

有两种方法可以解决它 - 自定义对话框布局和看起来像对话框的活动。

要创建自定义对话框,请使用自定义布局创建DialogFragment,填充gridview并设置项目单击侦听器。

要使用类似对话框的活动,请创建一个带有Dialog主题且没有标题栏的活动,在其中创建您喜欢的任何布局并添加onitemclick监听器。然后从主活动中,使用startActivityForResult来调用此活动;在itemclicklistener中,使用setResultfinish - 然后在主要活动中使用onActivityResult方法检查选择了哪个元素。

开始工作,编写一些代码 - 如果您遇到特定问题,请回复您的代码以及有关哪些代码无效以及原因的详细信息。