Android:将活动转换为片段

时间:2016-02-16 06:20:08

标签: android

我有以下代码段,即活动。而且我不知道如何转换为片段。我正在使用图像和文本制作网格视图。

public class CustomGridViewMainActivity extends Activity 
{


            GridView gridView;
            GridViewCustomAdapter grisViewCustomeAdapter;


            @Override
            protected void onCreate(Bundle savedInstanceState)
            {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.main);


                    gridView=(GridView)findViewById(R.id.gridViewCustom);
                    // Create the Custom Adapter Object
                    grisViewCustomeAdapter = new GridViewCustomAdapter(this);
                    // Set the Adapter to GridView
                    gridView.setAdapter(grisViewCustomeAdapter);

                    // Handling touch/click Event on GridView Item
                      gridView.setOnItemClickListener(new OnItemClickListener() {

                       @Override
                       public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
                           String selectedItem;
                           if(position%2==0)
                               selectedItem="Facebook";
                           else
                               selectedItem="Twitter";
                        Toast.makeText(getApplicationContext(),"Selected Item: "+selectedItem, Toast.LENGTH_SHORT).show();

                       }
                      });

请你指导我......

1 个答案:

答案 0 :(得分:0)

希望它对你有所帮助!

 public class CustomGridViewMainFragment extends Fragment
        {
                    View view_root;
                    GridView gridView;
                    GridViewCustomAdapter grisViewCustomeAdapter;

                    @Override
                    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                            view_root = inflater.inflate(R.layout.main, container, false);

                            gridView=(GridView)view_root.findViewById(R.id.gridViewCustom);
                            // Create the Custom Adapter Object
                            grisViewCustomeAdapter = new GridViewCustomAdapter(this);
                            // Set the Adapter to GridView
                            gridView.setAdapter(grisViewCustomeAdapter);

                            // Handling touch/click Event on GridView Item
                              gridView.setOnItemClickListener(new OnItemClickListener() {

                               @Override
                               public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
                                   String selectedItem;
                                   if(position%2==0)
                                       selectedItem="Facebook";
                                   else
                                       selectedItem="Twitter";
                                Toast.makeText(getApplicationContext(),"Selected Item: "+selectedItem, Toast.LENGTH_SHORT).show();

                               }
                             });

                    return view_root;
                  }

    }