简体   繁体   English

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

[英]Custom GridView as a header of Custom Listview in Viewpager Fragment in Android

I am trying to display a Custom gridview with 2 columns on top of Custom listview as header 我正在尝试在Custom Listview顶部显示2列的Custom Gridview作为标题

I am able to see the Custom Listview but cannot see the Gridview on top of the Listview. 我能够看到“自定义列表视图”,但看不到列表视图顶部的Gridview。

It is just showing blank space for Gridview above Listview. 它只是在Listview上方显示Gridview的空白。

在此处输入图片说明

Below is the code : 下面是代码:

1) Fragmentpage.java 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 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;
}

} }

I found the Culprit, it was in my xml : 我找到了Culprit,它在我的xml中:

android:stretchMode="spacingWidth";

changed it to 改成

android:stretchMode="columnWidth";

and i can see my gridview Thank God _/\\ _ 我可以看到我的gridview感谢上帝_ / \\ _

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM