具有自定义基础适配器的Android Grid View [英] Android Grid View with Custom base Adapter
问题描述
我已经开始学习android开发.我正在制作一个要在网格视图布局中组合显示电影缩略图(图像视图和文本视图)的应用程序,我制作了自定义适配器,但问题是我无法弄清楚如何一起显示图像视图和文本视图.我运行的应用程序显示空白屏幕.我已经尝试了许多解决方案,并且用了很多谷歌搜索,但是我不知道该怎么做,请帮帮我.这是我的代码.
i have started to learn android development. i am making an app in which i want to show movie thumbnails(image view and text view) combined in grid view layout i have made custom adapter but problem is that i cannot figure it out how to show image view and text view together.when i run app it shows blank white screen. i have tried many solution and also googled a lot but i cannot figure it out how to do this please help me. Here is my code.
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater inflater;
public ImageAdapter(Context c) {
mContext = c;
//
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
NewHolder holder = null;
ImageView imageView;
inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
if(convertView==null){
holder = new NewHolder();
convertView =inflater.inflate(R.layout.activity_main,viewGroup,false);
holder.imageView = new ImageView(mContext);
holder.textView = new TextView(mContext);
holder.imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
// holder.textView.setLayoutParams(new GridView.LayoutParams(85, 85));
convertView.setTag(holder);
//textView.
// textView.setText("Hello");
holder.imageView.setPadding(8, 8, 8, 8);
}
else {
holder = (NewHolder) convertView.getTag();
//imageView= (ImageView) convertView. ;
//textView = (TextView) convertView;
}
// convertView.
holder.imageView.setImageResource(mThumbIds[position]);
// holder.textView.setText("hello");
return convertView;
}
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
}
主要活动
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.grid_view);
gridview.setAdapter(new ImageAdapter(this));
// setContentView(gridview);
}
持有人阶层
public class NewHolder {
public ImageView imageView;
public TextView textView;
}
主要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="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
推荐答案
这是您的问题的解决方案. 您使用的适配器不正确.您正在使用此行中的适配器来扩展"GridView".
Here's the solution to your issue. You're using the adapter incorrectly. You're inflating the 'GridView' with the adapter in this line.
convertView =inflater.inflate(R.layout.activity_main,viewGroup,false);
这不是您想要做的.您要制作另一个XML Layout
来代表TextView
中的每部电影的标题和Imageview
That's no what you want to do. You want to make an other XML Layout
that represents each movie's title in a TextView
and image in an Imageview
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/movieimage"
android:src="@drawable/moviethumb"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/movie_title"
android:id="@+id/todo_list_item_bottom_text"
android:layout_marginTop="10dp"
android:textSize="12sp"/>
</LinearLayout>
本教程将为您提供帮助.它用于列表视图,但是android中的所有适配器几乎都以相同的方式工作. 适配器教程
This tutorial should help you. It's for a list view but all adapters in android pretty much work the same way. Adapter Tutorial
我注意到您可以改进的地方.将inflator
放在convertview == null
中.不使用该对象时没有必要创建它.
I noticed a something you can improve upon. Put your inflator
inside you convertview == null
. There's no point to creating this object when it's not used.
这篇关于具有自定义基础适配器的Android Grid View的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!