我有一个gridview,我正在尝试为不同的类别添加标题。 (我有4个类别,所以我想实现4个标题)。现在,4个类别中的所有项目都加载到同一个网格中。
我尝试了这些说明,但它对我不起作用(Using StickyGridHeaders),对我实施此类别标题的任何帮助都将不胜感激。
PizzaFragment类
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu_grid_main, container, false);
new PizzaMenuAsyncTask(getActivity(), this).execute();
grid = (GridView) view.findViewById(R.id.grid);
return view;
}
@Override
public void onTaskCompleted(JSONArray responseJson) {
try {
List<String> descriptions = new ArrayList<String>();
List<String> imageUrls = new ArrayList<String>();
for (int i = 0; i < responseJson.length(); ++i) {
JSONObject object = responseJson.getJSONObject(i);
if ((object.getString("MainCategoryID")).equals("1") && (object.getString("SubCategoryID")).equals("1") ) {
Log.i("ImageURL ", object.getString("ImageURL"));
imageUrls.add(object.getString("ImageURL"));
Log.i("Description ", object.getString("Description"));
descriptions.add(object.getString("Description"));
}
if ((object.getString("MainCategoryID")).equals("1") && (object.getString("SubCategoryID")).equals("2") ) {
Log.i("ImageURL ", object.getString("ImageURL"));
imageUrls.add(object.getString("ImageURL"));
Log.i("Description ", object.getString("Description"));
descriptions.add(object.getString("Description"));
}
if ((object.getString("MainCategoryID")).equals("1") && (object.getString("SubCategoryID")).equals("8") ) {
Log.i("ImageURL ", object.getString("ImageURL"));
imageUrls.add(object.getString("ImageURL"));
Log.i("Description ", object.getString("Description"));
descriptions.add(object.getString("Description"));
}
if ((object.getString("MainCategoryID")).equals("1") && (object.getString("SubCategoryID")).equals("9") ) {
Log.i("ImageURL ", object.getString("ImageURL"));
imageUrls.add(object.getString("ImageURL"));
Log.i("Description ", object.getString("Description"));
descriptions.add(object.getString("Description"));
}
}
CustomGrid adapter = new CustomGrid(getActivity(), descriptions, imageUrls);
grid.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
CustomGrid类
public class CustomGrid extends BaseAdapter {
private Context context;
public final ArrayAdapter<String> headers;
private final List<String> descriptions;
private final List<String> imageUrls;
public CustomGrid(Context c, List<String> descriptions, List<String> imageUrls) {
this.context = c;
this.descriptions = descriptions;
this.imageUrls = imageUrls;
headers = new ArrayAdapter<String>(context, R.layout.list_header);
}
@Override
public int getCount() {
return descriptions.size();
}
@Override
public Object getItem(int position) {
return descriptions.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(
R.layout.fragment_pizza, parent, false);
holder.ivImage = (ImageView) convertView
.findViewById(R.id.grid_image);
holder.tvHeader = (TextView) convertView
.findViewById(R.id.grid_text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvHeader.setText(descriptions.get(position));
Picasso.with(this.context).load(imageUrls.get(position)).into(holder.ivImage);
return convertView;
}
private class ViewHolder {
private TextView tvHeader;
private ImageView ivImage;
}
}
答案 0 :(得分:0)
我知道这些问题已经过时但我希望这可以帮到某些人。
将yourlayout.xml中的gridview更改为来自stickygridheaders的自定义gridview
<com.tonicartos.widget.stickygridheaders.StickyGridHeadersGridView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/asset_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:columnWidth="90dip"
android:horizontalSpacing="3dip"
android:numColumns="auto_fit"
android:verticalSpacing="3dip" />
为标题创建xml。例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:paddingBottom="8dp"
android:paddingLeft="4dp"
android:paddingTop="8dp"
android:textColor="#ff33b5e5"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
在自定义适配器中实现StickyGridHeadersSimpleAdapter
您可以在here
中查看示例