我已经从开发者指南中下载了Navigation Drawer示例,我尝试将GridView实现为Drawer的一个选项的Activity,但是在执行时GridView没有出现, IDE没有向我显示任何错误,在一个例子中,我可以成功地完成GridView,如何解决这个问题?
我的代码在这里:
在MainActivity中,我使用与
中的示例相同的内容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>
感谢您的合作!!
答案 0 :(得分:1)
尝试将InicioActivity更改为片段 并查看此question。我认为你面临同样的问题。
答案 1 :(得分:0)
以下对我有用。在Android Studio中:
1)从活动模板列表中创建一个新的导航抽屉活动。
2)请注意,将为您创建多个布局文件,包括 content_main.xml 。使用<GridView>
XML替换 content_main.xml 文件中的XML。
3)为GridView创建相关的XML文件或Java类,例如。 ImageAdapter类和GridView项目布局的XML文件。