【问题标题】:For loop in android create gaps between the itemsandroid中的for循环在项目之间创建间隙
【发布时间】:2026-01-13 09:35:01
【问题描述】:

我在导航滑块中传递了 json,它在项目之间获得了 2 个空格间隙。我不明白为什么会这样。我尝试了几种方法,但都没有成功。

Navigation.Java

private void navmenu() {
        String menuurl = "http://www.souqalkhaleejia.com/webapis/categories.php";
        Log.i("menuurl", menuurl);
        JsonObjectRequest menuobj = new JsonObjectRequest(Request.Method.POST, menuurl, (String) null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray menu1=response.getJSONArray("menu1");
                    for (int i=0;i<menu1.length();i++){
                        JSONObject first=menu1.getJSONObject(i);
                        Data navgadata = new Data();
                        navgadata.setCatergory(first.getString("category"));
                        datalts.add(navgadata);
                        JSONArray item=first.getJSONArray("items");
                        for (int j=0;j<item.length();j++){
                            Data secdata=new Data();
                            JSONObject second=item.getJSONObject(j);
                            secdata.setId(second.getString("id"));
                            secdata.setSubcatergory(second.getString("title"));
                            datalts.add(secdata);
                        }


                    }

                   } catch (JSONException e) {
                    e.printStackTrace();
                }
                listnadapter.notifyDataSetChanged();
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("res", "Error: " + error.getMessage());
            }
        });
        AppController.getInstance().addToRequestQueue(menuobj);
    }

这是我的 XML 文件 navigation.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:textColor="#fff"
    android:id="@+id/categories" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:textColor="#fff"
    android:id="@+id/subcategories" />

Adapter.java

public class Navgation_adapter extends BaseAdapter {
private Context context;
private List<Data> catdata;
private LayoutInflater inflater;
public Navgation_adapter(Context context, List<Data> catdata){
    this.context=context;
    this.catdata=catdata;
}
@Override
public int getCount() {
    return catdata.size();
}

@Override
public Object getItem(int i) {
    return catdata.get(i);
}

@Override
public long getItemId(int i) {
    return i;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

    ViewHolder holder;
    if (inflater == null)
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(view==null){
        holder=new ViewHolder();
        view=inflater.inflate(R.layout.navigation_list,viewGroup,false);
        holder.categories= (TextView) view.findViewById(R.id.categories);
        holder.subcategories= (TextView) view.findViewById(R.id.subcategories);
        view.setTag(holder);

    }else {
        holder= (ViewHolder) view.getTag();
    }
    Data ndata=catdata.get(i);
    holder.categories.setText(ndata.getCatergory());
    holder.subcategories.setText(ndata.getSubcatergory());

    return view;
}
static class ViewHolder{
    TextView categories,subcategories;
}

这是我如何获得视图的图像

【问题讨论】:

  • 您使用的是什么适配器?如果是定制的,请发布。实际上,看起来您将列表项布局中的 TextView 之一设置为 INVISIBLE,具体取决于它是类别还是子类别。如果是这种情况,您应该改用GONE。但如果这不是问题,请发布适配器。
  • 我已经更新了帖子,请检查一下
  • 我没有使用任何可见的或不可见的
  • 您在哪里使用 Navgation_adapter ?请上传它的xml。
  • navigation.xml 更新了它

标签: java android arrays json for-loop


【解决方案1】:

您对类别和子类别使用相同的 Data 类。当 JSON 数据是一个类别时,您在 Data 对象上设置类别 String,而不是子类别 String。当数据是子类别时,反之亦然。您未设置的 Strings 保持为空,而在 TextView 上设置空文本会将其留空。因此,当列表项表示类别时,顶部的TextView 显示类别名称,但底部的TextView 为空白。下一项将是一个子类别,出于同样的原因,顶部的TextView 将是空白的。两个相邻的空白TextViews 在类别与其第一个子类别之间留下了很大的差距。以下子类别项目中的空白类别TextViews 在这些项目之间留下了较小的差距。

如果您想尽可能多地坚持当前设置,一种解决方案是根据数据类型切换TextViews 的可见性属性,我们可以通过检查空Data 来确定对象成员。

public View getView(int i, View view, ViewGroup viewGroup) {
    ...     
    Data ndata = catdata.get(i);

    holder.subcategories.setText(ndata.getSubcatergory());
    holder.categories.setText(ndata.getCatergory());

    if(ndata.getCatergory() == null) {
        holder.categories.setVisibility(View.GONE);
        holder.subcategories.setVisibility(View.VISIBLE);       
    }
    else {
        holder.categories.setVisibility(View.VISIBLE);
        holder.subcategories.setVisibility(View.GONE);      
    }

    return view;
}

【讨论】: