我正在尝试在片段中插入GridView。我开发了自定义适配器来填充gridview。现在一切正常,但gridview没有显示在片段中。我检查了 getCount()方法它返回的总长度。这意味着数据存在,但没有显示。我对gridview很新,所以不知道为什么会这样。我尝试了以下答案,但他们也没有帮助我。 Android: getView in adapter never called
Custom Adapter getView() method is not called
Gridview not displaying in fragment
这是我的代码:
Tile Fragment Class:
public class TileFragment extends Fragment
{
public TileFragment(){
}
GridView gv;
Context context;
public static String [] prgmNameList={"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
public static int [] prgmImages={R.mipmap.dicover_disable,R.mipmap.discover_active, R.mipmap.home_disable,
R.mipmap.home_active,R.mipmap.lists_disable,R.mipmap.lists_active,
R.mipmap.profile_disable,R.mipmap.profile_active,R.mipmap.ic_search };
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
@SuppressLint("InflateParams")
// View v = LayoutInflater.from(getActivity()).inflate(R.layout.tile_fragment, null);
View v = inflater.inflate(R.layout.tile_fragment, container, false);
gv = (GridView) v.findViewById(R.id.gridView1);
gv.setAdapter(new CustomAdapter(getActivity(), prgmNameList,prgmImages));
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
}
}
自定义适配器:
public class CustomAdapter extends BaseAdapter {
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(FragmentActivity tileFragment, String[] prgmNameList, int[] prgmImages) {
result=prgmNameList;
imageId=prgmImages;
context = tileFragment;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return imageId.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
@SuppressLint("ViewHolder")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.programlist, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "You Clicked " + result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
tile_fragment:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:numColumns="3"
android:visibility="visible">
</GridView>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"
android:text=" Computer Languages..." />
</RelativeLayout>
prgramlist
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_gravity="center"
android:layout_width="88dp"
android:layout_height="88dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="TextView" />
</LinearLayout>
这就是我添加片段的方式:
HomeFragment:
public class HomeFragment extends Fragment
{
public HomeFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
FragmentTabHost mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager());
Bundle b = new Bundle();
b.putString("key", "Tile");
mTabHost.addTab(mTabHost.newTabSpec("tile").setIndicator("Tile"), TileFragment.class, b);
//
b = new Bundle();
b.putString("key", "Map");
mTabHost.addTab(mTabHost.newTabSpec("map").setIndicator("Map"), ProfileFragment.class, b);
return mTabHost;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
//
}
任何帮助将不胜感激......
答案 0 :(得分:2)
试试这个:
mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_parent_fragment);
其中R.layout.my_parent_fragment是您拥有tabhost的布局片段。
一旦您确认某个活动中GridView
已正确显示,我们就可以推断Gridview
Adapter
不是问题FragmentTabHost
,问题来自您的问题<div class="panel panel-primary" ng-repeat="device in home.devices">
<div class="panel-heading">
<h3 class="panel-title pull-right">
<a href="#settings">
<em class="fa fa-cog"></em>
</a>
</h3>
<h3 class="panel-title" ng-bind="::device.name"></h3>
</div>
<div class="panel-body">
<div class="row">
<div class="col-lg-4 text-center" ng-repeat="sensor in device.sensors">
<div class="panel panel-default">
<div class="panel-body">
<h2 ng-if="device.type == 'bm'" ng-bind="t{{$parent.$index}}{{$index}}"></h2>
t{{$parent.$index}}{{$index}}
</div>
</div>
</div>
</div>
</div>
</div>
定义或设置。您正在从onCreateView()返回tabHost,因为您必须在tabHost中设置布局或容器,如果不这样做,片段如何知道找到布局的位置?
有关详细信息,请参阅api here
答案 1 :(得分:0)
尝试使用此布局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" >
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
>
</GridView>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"
android:text=" Computer Languages..." />
</LinearLayout>
答案 2 :(得分:0)
无法找出问题的根源,但请尝试以下方法:
View rowView = convertView;
if(rowView != null) {
rowView = inflater.inflate(R.layout.programlist, null);
}