点击按钮打开图片库页面

时间:2018-03-07 03:48:06

标签: android android-activity gridview

我正在创建食品店应用程序,点击主页上的菜单按钮,它应该重定向到网格视图,

enter image description here

我已创建图像网格布局xml和图像网格类,但无法使用主页上的按钮进行映射

这是我的主要活动

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

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:columnWidth="90dp"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"
    android:gravity="center"
    android:stretchMode="columnWidth" >

</GridView>

这是我的gridlayout

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    // Keep all Images in array
    public Integer[] mThumbIds = {
            R.drawable.img15, R.drawable.img15,
            R.drawable.img15, R.drawable.img15
    };

    // Constructor
    public ImageAdapter(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;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
        return imageView;
    }

}

这是我网格的适配器类

public class MenuScreen extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.grid_layout);

        GridView gridView = (GridView) findViewById(R.id.grid_view);

        // Instance of ImageAdapter Class
        gridView.setAdapter(new ImageAdapter(this));

}

并在menuscreen.java上尝试调用我的gridadapater

cmake_minimum_required(VERSION 3.7)

project(mylib VERSION 0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 14)
#add_compile_options(-Wa -aslh)


# Define the library target
add_library(mylib INTERFACE)
target_include_directories(mylib INTERFACE 
    $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include>
)

include(CMakePackageConfigHelpers)
write_basic_package_version_file(
    "${PROJECT_BINARY_DIR}/mylibConfigVersion.cmake"
    VERSION 0.1
    COMPATIBILITY AnyNewerVersion
)

install(TARGETS mylib
    EXPORT mylibTargets
    LIBRARY DESTINATION lib COMPONENT Runtime
    ARCHIVE DESTINATION lib COMPONENT Development
    RUNTIME DESTINATION bin COMPONENT Runtime
    PUBLIC_HEADER DESTINATION include COMPONENT Development
    BUNDLE DESTINATION bin COMPONENT Runtime
)

include(CMakePackageConfigHelpers)
configure_package_config_file(
    "${PROJECT_SOURCE_DIR}/cmake/mylibConfig.cmake.in"
    "${PROJECT_BINARY_DIR}/mylibConfig.cmake"
    INSTALL_DESTINATION lib/cmake/mylib
)

install(EXPORT mylibTargets DESTINATION lib/cmake/mylib)
install(FILES "${PROJECT_BINARY_DIR}/mylibConfigVersion.cmake"
              "${PROJECT_BINARY_DIR}/mylibConfig.cmake"
        DESTINATION lib/cmake/mylib)
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ DESTINATION include)


add_executable(mytest test/basic_checks.cpp)
target_link_libraries(mytest mylib)

点击按钮,出现此错误

java.lang.RuntimeException:无法启动活动ComponentInfo {com.restaurant/com.restaurant.MenuScreen}:                                                                 java.lang.RuntimeException:您的内容必须有一个ExpandableListView,其id属性为'android.R.id.list'

如何正确地从主页按钮重定向到网格视图页面,请建议

2 个答案:

答案 0 :(得分:0)

在主要职位(R.layout.main)中添加占位符

<FrameLayout
    android:layout_height="match_parent"
    android:id="@+id/place_men"
    android:layout_width="match_parent">
</FrameLayout>

和chnage代码

go_to_menu.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                //starting new fragment on button click
               MenuScreen menuScreen =new MenuScreen();

 getSupportFragmentManager().beginTransaction().replace(R.id.place_men, menuScreen).commit();
            }
        });

此外,MenuScreen扩展了android.support.v4.app.Fragment

答案 1 :(得分:0)

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f0f0">

<GridView
    android:id="@+id/gridView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:columnWidth="100dp"
    android:drawSelectorOnTop="true"
    android:gravity="center"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="5dp"
    android:focusable="true"
    android:clickable="true"/>

    </RelativeLayout>

grid_item_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@drawable/grid_color_selector"
android:orientation="vertical"
android:padding="5dp">

<ImageView
    android:id="@+id/image"
    android:layout_width="100dp"
    android:layout_height="100dp" />

    <TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:gravity="center"
    android:textSize="12sp" />

   </LinearLayout>

GridViewAdapter.class

public class GridViewAdapter extends ArrayAdapter {
private Context context;
private int layoutResourceId;
private ArrayList data = new ArrayList();

public GridViewAdapter(Context context, int layoutResourceId, ArrayList data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ViewHolder holder = null;

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new ViewHolder();
        holder.imageTitle = (TextView) row.findViewById(R.id.text);
        holder.image = (ImageView) row.findViewById(R.id.image);
        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }

    ImageItem item = data.get(position);
    holder.imageTitle.setText(item.getTitle());
    holder.image.setImageBitmap(item.getImage());
    return row;
}

static class ViewHolder {
    TextView imageTitle;
    ImageView image;
}
}

ImageItem.class pojo class

public class ImageItem {
private Bitmap image;
private String title;

public ImageItem(Bitmap image, String title) {
    super();
    this.image = image;
    this.title = title;
}

public Bitmap getImage() {
    return image;
}

public void setImage(Bitmap image) {
    this.image = image;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}
}

MainActivity.class

public class MainActivity extends ActionBarActivity {
private GridView gridView;
private GridViewAdapter gridAdapter;

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

    gridView = (GridView) findViewById(R.id.gridView);
    gridAdapter = new GridViewAdapter(this, R.layout.grid_item_layout, getData());
    gridView.setAdapter(gridAdapter);
}

// Prepare some dummy data for gridview
private ArrayList<ImageItem> getData() {
    final ArrayList<ImageItem> imageItems = new ArrayList<>();
    TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids);
    for (int i = 0; i < imgs.length(); i++) {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imgs.getResourceId(i, -1));
        imageItems.add(new ImageItem(bitmap, "Image#" + i));
    }
    return imageItems;
}
}

请遵循Gridview的此代码实现

在按钮点击事件中

button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             Intent intent = new Intent(YourActivity.this, MainActivity.class);
    startActivity(intent);

         }
     });

点击此链接查看工作示例

Image GridView