自定义GridView作为Android中Viewpager Fragment中Custom Listview的标题

时间:2016-01-21 10:33:34

标签: android listview android-gridview

我正在尝试在自定义列表视图顶部显示带有2列的自定义网格视图作为标题

我能够看到自定义列表视图,但无法在列表视图的顶部看到Gridview。

它只是在Listview上方显示Gridview的空白区域。

enter image description here

以下是代码:

1)Fragmentpage.java

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    view=inflater.inflate(R.layout.fragmentpage_layout, null,false);

    listView=(ListView)view.findViewById(R.id.listview);

    View header=inflater.inflate(R.layout.gridview_layout,null,false);

    gridView=(GridView)header.findViewById(R.id.gridview);

    gridViewAdapter=new CustomGridViewAdapter(getActivity(),images,toptexts, bottomtexts);
    listViewAdapter=new CustomListViewAdapter(getActivity(),images,toptexts,bottomtexts);

    gridView.setAdapter(gridViewAdapter);
    listView.addHeaderView(header);
    listView.setAdapter(listViewAdapter);

   return view;
}

2)CustomGridViewAdapter.java

public class CustomGridViewAdapter extends ArrayAdapter<String> {

LayoutInflater inflater;
Context context;
ArrayList<Integer> images;
ArrayList<String> fronts,seconds;


CustomGridViewAdapter(Context context,ArrayList<Integer> images,ArrayList<String> fronts,ArrayList<String> seconds){
    super(context,-1,fronts);
    this.context=context;
    this.images=images;
    this.fronts=fronts;
    this.seconds=seconds;
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}


static class ViewHolder{
    ImageView imageView;
    TextView toptextView,bottomtextView;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view=convertView;
    ViewHolder holder;
    if(convertView==null){
        holder=new ViewHolder();
        view=inflater.inflate(R.layout.featured_gridlayout,null,false);
        holder.imageView=(ImageView)view.findViewById(R.id.gridImage);
        holder.toptextView=(TextView)view.findViewById(R.id.toptext);
        holder.bottomtextView=(TextView)view.findViewById(R.id.bottomtext);
        view.setTag(holder);
    }else{
        holder=(ViewHolder) view.getTag();
    }

       holder.imageView.setImageResource(images.get(position));
       holder.toptextView.setText(fronts.get(position));
       holder.bottomtextView.setText(seconds.get(position));

    return view;
}

}

1 个答案:

答案 0 :(得分:0)

我找到了罪魁祸首,它在我的xml中:

android:stretchMode="spacingWidth";

将其更改为

android:stretchMode="columnWidth";

我可以看到我的gridview 感谢上帝_ / \ _ _