【问题标题】:ArrayAdapter getView method trigger only first elementArrayAdapter getView 方法仅触发第一个元素
【发布时间】:2014-09-10 14:46:14
【问题描述】:

我尝试使用 ListFragment 创建一个列表。我为 listfragment 创建了自定义数组适配器。当我启动我的项目时,我只能在我的活动中看到第一个项目。但是我注意到当我拖放第一个项目时,我可以在一行上看到第二个项目。您可以在下面看到我的片段布局和列表项布局。另外我附上我的自定义适配器类。提前致谢。

片段布局

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

<fragment
    android:id="@+id/menuFragment"
    android:name="com.example.fragment.MenuListFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />

列表项布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp" >

<ImageView
    android:id="@+id/imgMenu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/makel_icon"
    android:contentDescription="@string/desc_mainMenuImages"
     />

<TextView
    android:id="@+id/txtMenu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/imgMenu"
    android:textSize="14sp"
    android:textStyle="bold" />

我的自定义数组适配器

public class MenuListAdapter extends ArrayAdapter<MainMenuInfo> {

private List<MainMenuInfo> menuList;
private Context context;
private int layoutResourceID;

public MenuListAdapter(Context context, int layoutResourceID, List<MainMenuInfo> menuList) {
    super(context, layoutResourceID, menuList);

    this.context = context;
    this.menuList = menuList;
    this.layoutResourceID = layoutResourceID;
}

@Override
public int getCount() {

    return menuList.size();
}

@Override
public MainMenuInfo getItem(int position) {

    return menuList.get(position);
}

@Override
public long getItemId(int position) {

    return menuList.get(position).get_ID();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view = convertView;

    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(layoutResourceID, null);
    }

    MainMenuInfo menu = menuList.get(position);

    ImageView imgMenu = (ImageView) view.findViewById(R.id.imgMenu);

    if (menu.getImageName() != null) {

        int r = view.getResources().getIdentifier(menu.getImageName(), "drawable", context.getPackageName());

        if (r > 0) {
            imgMenu.setImageResource(r);
        }
    }

    TextView txtMenu = (TextView) view.findViewById(R.id.txtMenu);

    txtMenu.setText(menu.getMenuName());

    return view;
}

}

【问题讨论】:

  • 可以上下滚动列表吗?
  • 当我滚动第一个项目时,我可以看到第二个项目。我只能看到一个活动项目。
  • 将片段LinearLayout 的方向更改为android:orientation="vertical"
  • 请说明您的问题是什么?你是说你的列表视图只显示一个项目?在这种情况下,您在定义 setAdapter 时使用的 ArrayList 可能存在问题。
  • 好吧,我的列表视图只显示一个项目。我注意到当我在第一个项目上滚动时,我可以看到第二个项目。但是我只能看到一个活动项目

标签: android android-fragments android-arrayadapter android-listfragment


【解决方案1】:

我的问题解决了。上面的代码没有错误。我的错误是在 setDividerHeight 属性中输入了错误的值。因此我的列表视图无法显示其他项目。谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多