你好我有一些问题,实施AssymetricGridView它仍然崩溃,但我不知道有什么问题如何正确实现它与我的适配器有错误链接到AssymetricGridView库。谢谢
Adapter must be an instance of AsymmetricGridViewAdapter
at com.felipecsl.asymmetricgridview.library.widget.AsymmetricGridView.setAdapter(AsymmetricGridView.java:71)
at com.gabriel.movies.fragment.MainFragment.render(MainFragment.java:140)
at com.gabriel.movies.fragment.MainFragment$MyTask.onPostExecute(MainFragment.java:228)
at com.gabriel.movies.fragment.MainFragment$MyTask.onPostExecute(MainFragment.java:161)
我的代码
<com.felipecsl.asymmetricgridview.library.widget.AsymmetricGridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
AsymmetricGridView gridview;
List<ItemCategory> list;
AdapterCategory adapter;
gridview = (AsymmetricGridView) mRootView.findViewById(R.id.gridView1);
gridview.setRequestedColumnWidth(Utils.dpToPx(getActivity(), 120));
list = new ArrayList<ItemCategory>();
adapter = new AdapterCategory(getActivity(), R.layout.fragment_worcipe_main_item, list);
gridview.setAdapter(adapter);
和适配器代码
public class AdapterCategory extends ArrayAdapter<ItemCategory> {
ItemCategory object;
private Activity activity;
private List<ItemCategory> item;
private int row;
public AdapterCategory(Activity act, int resource, List<ItemCategory> arrayList) {
super(act, resource, arrayList);
this.activity = act;
this.row = resource;
this.item = arrayList;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(row, null);
holder = new ViewHolder();
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
if ((item == null) || ((position + 1) > item.size()))
return view;
object = item.get(position);
holder.title = (TextView) view.findViewById(R.id.category_title);
holder.image = (ImageView) view.findViewById(R.id.category_image);
holder.title.setText(object.getCategoryName());
Picasso.with(getContext()).load(RecipeConfig.SERVER_URL + "/upload/" +
object.getCategoryImageurl()).placeholder(R.drawable.ic_thumbnail).into(holder.image);
return view;
}
public class ViewHolder {
public TextView title;
public ImageView image;
}
}
答案 0 :(得分:1)
首先,您需要一个从AsymmetricItem扩展的类。这应该是这样的:
public class DemoItem implements AsymmetricItem {
private int columnSpan;
private int rowSpan;
private int position;
private String text;
public DemoItem() {
this(1, 1, 0,"");
}
public DemoItem(int columnSpan, int rowSpan, int position,String text) {
this.columnSpan = columnSpan;
this.rowSpan = rowSpan;
this.position = position;
this.text=text;
}
public DemoItem(Parcel in) {
readFromParcel(in);
}
@Override public int getColumnSpan() {
return columnSpan;
}
@Override public int getRowSpan() {
return rowSpan;
}
public String getText(){
return text;
}
public int getPosition() {
return position;
}
@Override public String toString() {
return String.format("%s: %sx%s", position, rowSpan, columnSpan);
}
@Override public int describeContents() {
return 0;
}
private void readFromParcel(Parcel in) {
columnSpan = in.readInt();
rowSpan = in.readInt();
position = in.readInt();
}
@Override public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeInt(columnSpan);
dest.writeInt(rowSpan);
dest.writeInt(position);
}
/* Parcelable interface implementation */
public static final Parcelable.Creator<DemoItem> CREATOR = new Parcelable.Creator<DemoItem>() {
@Override public DemoItem createFromParcel(@NonNull Parcel in) {
return new DemoItem(in);
}
@Override @NonNull
public DemoItem[] newArray(int size) {
return new DemoItem[size];
}
};
}
然后你需要创建你的适配器,它应该是这样的(如果你应该回收视图):
public class GridAdapter extends ArrayAdapter<DemoItem> {
private final LayoutInflater layoutInflater;
public GridAdapter(Context context, List<DemoItem> items) {
super(context, 0, items);
layoutInflater = LayoutInflater.from(context);
}
public GridAdapter(Context context) {
super(context, 0);
layoutInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
DemoItem item = getItem(position);
View v = layoutInflater.inflate( R.layout.adapter_item, parent, false);
TextView textView = (TextView) v.findViewById(R.id.textView);
textView.setText(item.getText());
return v;
}
@Override public int getViewTypeCount() {
return 2;
}
@Override public int getItemViewType(int position) {
return position % 2 == 0 ? 1 : 0;
}
public void appendItems(List<DemoItem> newItems) {
addAll(newItems);
notifyDataSetChanged();
}
public void setItems(List<DemoItem> moreItems) {
clear();
appendItems(moreItems);
}
}
最后,当你拥有所有这些时,你可以像这样在Activity / Fragment中实现所有内容:
private ListAdapter adapter;
private AsymmetricGridView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dashboard_gridview, container, false);
listView = (AsymmetricGridView) rootView.findViewById(R.id.listView);
listView.setAllowReordering(true);
final List<DemoItem> items = new ArrayList<>();
items.add(new DemoItem(2,2,0,"Example Text 1"));
items.add(new DemoItem(1,1,0,"Example Text 2"));
items.add(new DemoItem(1,1,0,"Example Text 3"));
items.add(new DemoItem(2,2,0,"Example Text 4"));
items.add(new DemoItem(1,1,0,"Example Text 5"));
items.add(new DemoItem(1,1,0,"Example Text 6"));
items.add(new DemoItem(1,1,0,"Example Text 7"));
items.add(new DemoItem(2,2,0,"Example Text 8"));
items.add(new DemoItem(1,1,0,"Example Text 9"));
items.add(new DemoItem(1,1,0,"Example Text 10"));
adapter = new GridAdapter(getContext(), items);
listView.setAdapter(new AsymmetricGridViewAdapter(getContext(), listView, adapter));
return rootView;
}
}
就是这样,我希望我能帮助你。