GridView未显示在导航抽屉的示例中[英] GridView is not shown in an example with Navigation Drawer

本文是小编为大家收集整理的关于GridView未显示在导航抽屉的示例中的处理方法,想解了GridView未显示在导航抽屉的示例中的问题怎么解决?GridView未显示在导航抽屉的示例中问题的解决办法?GridView未显示在导航抽屉的示例中问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

我从开发人员指南下载了导航抽屉示例,我尝试将GridView实现为抽屉选项的一个活动,但在执行时,GridView未出现,IDE不会向我展示任何错误,并且在一个示例中,除了我可以做到的experview成功,如何纠正这个问题?

我的代码在这里:

在主动力中,我使用与

的示例相同
Fragment fragment;
FragmentManager fragmentManager;
fragment = new inicioFragment(R.layout.inicio);
fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

InicioActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;
public class InicioActivity extends Activity {
    TextView tv1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.inicio);    
        tv1=(TextView)findViewById(R.id.tv1);
        GridView gv = (GridView)findViewById(R.id.gridView1); 
        gv.setAdapter(new ImageAdapter(this.getApplicationContext()));
        gv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id){              
                String g=Integer.toString(position);
                tv1.setText(g);
            }
        });
    }
}

imageapapter.java

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
private Context mContext;
    public ImageAdapter(Context c) {
        mContext = c;
    }
    public int getCount() {
        return mThumbIds.length;
    }
    public Object getItem(int position) {
        return null;
    }
    public long getItemId(int position) {
        return 0;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(13, 13, 13, 13);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }
    private Integer[] mThumbIds = {
            R.drawable.image_2, R.drawable.image_3,
            R.drawable.image_4, R.drawable.image_5,
            R.drawable.image_6, R.drawable.image_7,
            R.drawable.image_0, R.drawable.image_1,
            R.drawable.image_2, R.drawable.image_3,
            R.drawable.image_4, R.drawable.image_5,
            R.drawable.image_6, R.drawable.image_7,
            R.drawable.image_0, R.drawable.image_1,
            R.drawable.image_2, R.drawable.image_3,
            R.drawable.image_4, R.drawable.image_5,
            R.drawable.image_6, R.drawable.image_7
    };
}

inicio.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="230dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linearLayout1"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Aqui van las notificaciones push de facebook" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="230dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="horizontal" >

        <GridView
            android:id="@+id/gridView1"
            android:layout_width="156dp"
            android:layout_height="192dp"
            android:numColumns="2" >
        </GridView>

        <TextView
            android:id="@+id/tv1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center_vertical"
            android:gravity="center_horizontal|center_vertical"
            android:text="Información" />
    </LinearLayout>
</RelativeLayout>

感谢您的协作!!

推荐答案

以下为我工作.在Android Studio:

1)从活动模板列表中创建一个新的导航抽屉活动.

2)请注意,将为您创建几个布局文件,包括 content_main.xml .用<GridView> xml替换 content_main.xml 文件中的XML.

3)为gridview创建相关的XML文件或Java类,例如. ImageAdapter类和GridView项目布局的XML文件.

其他推荐答案

尝试将inicioactive更改为片段.
并检查这个问题.我觉得你面临着同样的问题.

本文地址:https://www.itbaoku.cn/post/157514.html

问题描述

I've downloaded the Navigation Drawer example from the developers guide, I try to implement a GridView into an Activity of one of the options of the Drawer, but in execution time the GridView doesn't appears, the IDE doesn't show me any errors and in an example apart I can do it de GridView successfuly, How do I correct this problem?

My code is here:

In the MainActivity I use the same like the example with

Fragment fragment;
FragmentManager fragmentManager;
fragment = new inicioFragment(R.layout.inicio);
fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

InicioActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;
public class InicioActivity extends Activity {
    TextView tv1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.inicio);    
        tv1=(TextView)findViewById(R.id.tv1);
        GridView gv = (GridView)findViewById(R.id.gridView1); 
        gv.setAdapter(new ImageAdapter(this.getApplicationContext()));
        gv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id){              
                String g=Integer.toString(position);
                tv1.setText(g);
            }
        });
    }
}

ImageAdapter.java

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
private Context mContext;
    public ImageAdapter(Context c) {
        mContext = c;
    }
    public int getCount() {
        return mThumbIds.length;
    }
    public Object getItem(int position) {
        return null;
    }
    public long getItemId(int position) {
        return 0;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(13, 13, 13, 13);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }
    private Integer[] mThumbIds = {
            R.drawable.image_2, R.drawable.image_3,
            R.drawable.image_4, R.drawable.image_5,
            R.drawable.image_6, R.drawable.image_7,
            R.drawable.image_0, R.drawable.image_1,
            R.drawable.image_2, R.drawable.image_3,
            R.drawable.image_4, R.drawable.image_5,
            R.drawable.image_6, R.drawable.image_7,
            R.drawable.image_0, R.drawable.image_1,
            R.drawable.image_2, R.drawable.image_3,
            R.drawable.image_4, R.drawable.image_5,
            R.drawable.image_6, R.drawable.image_7
    };
}

inicio.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="230dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linearLayout1"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Aqui van las notificaciones push de facebook" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="230dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="horizontal" >

        <GridView
            android:id="@+id/gridView1"
            android:layout_width="156dp"
            android:layout_height="192dp"
            android:numColumns="2" >
        </GridView>

        <TextView
            android:id="@+id/tv1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center_vertical"
            android:gravity="center_horizontal|center_vertical"
            android:text="Información" />
    </LinearLayout>
</RelativeLayout>

Thanks for your collaboration!!

推荐答案

The following worked for me. In Android Studio:

1) Create a new Navigation Drawer Activity from the list of activity templates.

2) Note that several layout files will be created for you, including a content_main.xml. Replace the XML in the content_main.xml file with your <GridView> XML.

3) Create the relevant XML files or Java classes for the GridView, eg. the ImageAdapter class and an XML file for the GridView item layout.

其他推荐答案

Try to change InicioActivity into a fragment.
And check this question. I think you are facing the same problem.

查看更多