我正在尝试GridView。我制作了一个适配器,一个片段和网格行XML。使用GridView,我试图使网格视图的每个项目都被边框包围,但是出现如下所示的错误。
适配器:
public class GridViewAdapter extends BaseAdapter {
//Imageloader to load images
private ImageLoader imageLoader;
//Context
private Context context;
public NetworkImageView networkImageView;
public TextView textView;
//Array List that would contain the urls and the titles for the images
private ArrayList < String > names;
private ArrayList < String > images;
public SquareLayout l1;
public GridViewAdapter(Context context, ArrayList < String > names, ArrayList < String > images) {
//Getting all the values
this.context = context;
this.names = names;
this.images = images;
}
@Override
public int getCount() {
return images.size();
}
@Override
public Object getItem(int position) {
return images.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
public static class ViewHolder {
public NetworkImageView networkImageView;
public TextView textView;
}@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Creating a linear layout
// SquareLayout li=new SquareLayout(context);
// LinearLayout linearLayout = new LinearLayout(context);
// l1.setOrientation(SquareLayout.Vert)
// linearLayout.setOrientation(LinearLayout.VERTICAL);
ViewHolder view;
LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// LayoutInflater inflator = context.getLayoutInflater();
if (convertView == null) {
view = new ViewHolder();
convertView = inflator.inflate(R.layout.gridview_row, null);
view.textView = (TextView) convertView.findViewById(R.id.textView1);
view.networkImageView = (NetworkImageView) convertView.findViewById(R.id.imageView1);
convertView.setTag(view);
}
else {
view = (ViewHolder) convertView.getTag();
}
view.textView.setText(names.get(position));
//textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
//NetworkImageView
NetworkImageView networkImageView = new NetworkImageView(context);
//networkImageView.setImageResource(mThumbIds[position]);
view.networkImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
view.networkImageView.setLayoutParams(new GridView.LayoutParams(50, 50));
//Initializing ImageLoader
imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
imageLoader.get(images.get(position), ImageLoader.getImageListener(networkImageView, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));
//Setting the image url to load
view.networkImageView.setImageUrl(images.get(position), imageLoader);
view.networkImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Intent intent= new Intent(context, CateFragment.class);
//intent.putExtra("your_extra","your_class_value");
//context.startActivity(intent);
// v.getContext().startActivity(RegisterActivity.class);
Toast.makeText(context, "You clicked on OK", Toast.LENGTH_SHORT).show();
//Toast here
}
});
//Scaling the imageview
view.networkImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//networkImageView.setLayoutParams(new GridView.LayoutParams(200,200));
//Adding views to the layout
//linearLayout.addView(networkImageView);
//linearLayout.addView(textView);
//Creating a textview to show the title
//TextView textView = new TextView(context);
//Returnint the layout
return convertView;
}
}
错误:
Process: novumlogic.payment, PID: 31611
java.lang.ClassCastException: android.widget.AbsListView$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1803)
at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1748)
at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:381)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:389)
at android.view.View.measure(View.java:19756)
at android.widget.GridView.onMeasure(GridView.java:1081)
at android.view.View.measure(View.java:19756)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1117)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:642)
at android.view.View.measure(View.java:19756)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
at android.view.View.measure(View.java:19756)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19756)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
at android.view.View.measure(View.java:19756)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
at android.view.View.measure(View.java:19756)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19756)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19756)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19756)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:687)
at android.view.View.measure(View.java:19756)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2283)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1370)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1258)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6348)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
I/Process: Sending signal. PID: 31611 SIG: 9
Application terminated.