网格视图具有重复的列值

时间:2016-06-18 07:33:37

标签: view grid

我正在尝试在可扩展列表视图中构建网格视图。我面临的问题是,无论我在xml中指定的列数,gridview中的列都在重复。请在下面找到代码和屏幕截图。那就是有两个子视图A和B.而不是每个都在一行中。 A完全在第1行,无论列数是多少,B都在完整的第二行。

ExpandableListAdapter

    public class ExpandListAdapter extends BaseExpandableListAdapter {

private Activity activity;
private Context context;
private ArrayList<GroupModel> mMainDatabase;
private LayoutInflater inflater;
private ArrayList<String> parentItems, child, child1, child2;
private CustomGrid adapter;
private GridView grid;
ViewHolder viewHolder;

private static final String LOG_TAG = ExpandListAdapter.class.getSimpleName();


public ExpandListAdapter(ArrayList<GroupModel> mMainDatabase) {

    this.mMainDatabase = mMainDatabase;

}
public void setInflater(LayoutInflater inflater, Activity activity) {
    this.inflater = inflater;
    this.activity = activity;
}

@Override
public int getGroupCount() {
    return mMainDatabase.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    return mMainDatabase.get(groupPosition).getItemModels().size();
}

@Override
public Object getGroup(int groupPosition) {
    return mMainDatabase.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return child.get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    View v = convertView;

    // if (convertView == null) {

    // inflate the adapter_side
    LayoutInflater vi = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = vi.inflate(R.layout.fragment_list_item, null);

    // well set up the ViewHolder
    viewHolder = new ViewHolder();
    viewHolder.title = (TextView) v.findViewById(R.id.item_title);

    viewHolder.image = (ImageView) v.findViewById(R.id.item_image);
    viewHolder.image.setId(groupPosition);



    if (mMainDatabase.get(groupPosition).getGroupImage() != null &&
            !mMainDatabase.get(groupPosition).getGroupImage().equals(""))



        Utilities.displayImage(activity.getApplicationContext(), mMainDatabase.get(groupPosition).getGroupImage(), viewHolder.image);
    else
        Picasso.with(activity).load("http://www.google.com")
                .into(viewHolder.image);

    viewHolder.title
            .setText(mMainDatabase.get(groupPosition).getGroupName());

    viewHolder.arrowExpand = (ImageView) v.findViewById(R.id.arrowExpand);

    if (isExpanded)
        viewHolder.arrowExpand.setImageResource(R.drawable.dropdown2);
    else
        viewHolder.arrowExpand.setImageResource(R.drawable.dropdown);



    return v;
}

@Override
public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {

        //    LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.grid_view, null);
        }



    grid = (GridView) convertView.findViewById(R.id.grid);
    adapter = new CustomGrid(activity,  groupPosition, childPosition,activity, mMainDatabase);
    grid.setAdapter(adapter);
    grid.setTag(childPosition);
    grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


            Intent intent = new Intent(activity, FoodDetailActivity.class);
            intent.putExtra("item", mMainDatabase.get(groupPosition).getItemModels().
                    get(Integer.parseInt(view.getTag().toString())));
            intent.putExtra("grpId", mMainDatabase.get(groupPosition).get_id());
            intent.putExtra("atBottom", true);
            activity.startActivity(intent);
        }
    });

    return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onGroupExpanded(int groupPosition) {
    super.onGroupExpanded(groupPosition);

}

@SuppressLint("NewApi")
@Override
public void onGroupCollapsed(int groupPosition) {
    super.onGroupCollapsed(groupPosition);
    // ImageView img_selection = (ImageView) groupPosition
    // .findViewById(R.id.arrowExpand);

}

static class ViewHolder {
    TextView title;

    TextView number;
    TextView numbertxt;
    ImageView image, arrowExpand;
    Bitmap b;
    int position;
}

}

自定义网格适配器

    public class CustomGrid extends BaseAdapter {
private Context mContext;
private ArrayList<String> mItemName = null;
private ArrayList<String> mItemImage = null;
private int groupPosition, childPosition;
Activity activity;
ArrayList<GroupModel> mMainDatabase;
LayoutInflater inflater;

public CustomGrid(Context c,
                  int groupPosition, int childPosition, Activity activity, ArrayList<GroupModel> mMainDatabase) {

    mContext = c;
    this.groupPosition = groupPosition;
    this.childPosition = childPosition;
    this.activity = activity;
    this.mMainDatabase = mMainDatabase;
}


@Override
public int getCount() {
    // TODO Auto-generated method stub
    return mMainDatabase.get(groupPosition).getItemModels().size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View grid;

   LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {

        grid = new View(mContext);
        grid = inflater.inflate(R.layout.grid_single, null);
        TextView textView = (TextView) grid.findViewById(R.id.grid_text);
        ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);


        textView.setText(mMainDatabase.get(groupPosition).getItemModels()
                .get(childPosition).getItemName());

        Log.d("CUSTOMGRID", "child name is " + mMainDatabase.get(groupPosition).getItemModels()
                .get(childPosition).getItemName() + " child position is " + childPosition);

        if (mMainDatabase.get(groupPosition).getItemModels().get(childPosition).getItemImage() != null &&
                !mMainDatabase.get(groupPosition).getItemModels().get(childPosition).getItemImage()
                        .equals(""))

            Picasso.with(activity)
                    .load(mMainDatabase.get(groupPosition).getItemModels()
                            .get(childPosition).getItemImage())
                    .placeholder(R.drawable.place).into(imageView);

        else
            Picasso.with(activity).load("http://www.google.com")
                    .placeholder(R.drawable.place).into(imageView);

    } else {
        grid = (View) convertView;
    }


    return grid;
}
}

0 个答案:

没有答案