2016-09-12 13 views
0

こんにちは私はAssymetricGridViewの実装に問題がありますが、まだクラッシュしていますが、私のアダプターで正しく実装する方法が分かりません。最初はAndroid実装AsymmetricGridView

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; 

     } 
    } 

答えて

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]; 
    } 
}; 
} 

が次にあなたが(あなたが意見をリサイクルする必要がありexept)のようになりますアダプター、作成する必要があります。

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); 
} 
} 

そして最後に、あなたはすべてを持っているときそれは、あなたがこのようなあなたの活動/フラグメントのすべてを実装することができます。

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; 
    } 
} 

そして、それはそれだ、私はあなたを助けることができる願っています。