片段中的自定义ArrayAdapter不调用getView()

时间:2019-03-04 05:08:41

标签: android android-fragments android-listview android-arrayadapter

我正在尝试在ListView内加载对象列表,该列表包含在Fragment内。但是无论如何我都无法正常工作。

我尝试直接在主要活动中加载相同的数据和相同的列表,并且可以正常工作。但不在片段中。

这是我的代码:

MainActivity -片段正确加载,从服务器检索数据,一切正常。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        setContentView(R.layout.side_menu);

        if (SharedPreferencesManager.isAuthenticated(getApplicationContext())) {
            ServerComm.getInstance(getApplicationContext()).requestMenuItems(new BaseVolleyCallback() {
                @Override
                public void onMenuRequest(Menu result) {
                    leftSideMenu = (SideMenuFragment) getSupportFragmentManager().findFragmentById(R.id.left_side_menu);
                    leftSideMenu.menuItemList = result.top;
                    leftSideMenu.test();
                }
            });

        }
    }

片段:同样,一切都很好

public class SideMenuFragment extends Fragment {

    ArrayList<MenuItem> menuItemList;
    View mView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.side_menu, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mView = view;
    }

    public void test(){
        MenuItemAdapter adapter = new MenuItemAdapter(getContext(), menuItemList);
        ListView listView = (ListView) mView.findViewById(R.id.listView);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final MenuItem item = (MenuItem) parent.getItemAtPosition(position);
            MainActivity activity = (MainActivity)getActivity();
            activity.loadContent(item);
            }
        });
    }
}

适配器:-使用项目列表调用构造函数,使用正确数量的项目调用getCount()。 getView-未调用。

class MenuItemAdapter extends ArrayAdapter<MenuItem> {

    private Context mContext;
    private List<MenuItem> menuList = new ArrayList<>();

    public MenuItemAdapter(@NonNull Context context, ArrayList<MenuItem> list) {
        super(context, 0 , list);
        mContext = context;
        menuList = list;
    }

    @Override
    public int getCount () {
        return menuList.size();
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.menu_cell, parent, false);
        }
        MenuItem item = menuList.get(position);

        TextView tvName = (TextView) convertView.findViewById(R.id.itemName);
        tvName.setText(item.name);

        return convertView;
    }
}

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment android:name="com.example.dbl.SideMenuFragment"
        android:id="@+id/left_side_menu"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:elevation="100dp"
        app:layout_constraintWidth_default="percent"
        app:layout_constraintWidth_percent=".7"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

side_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent">


    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/colorAccent"
        android:cacheColorHint="@android:color/transparent"
        android:divider="#CCCCCC"
        android:dividerHeight="1dp"
        android:paddingLeft="2dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

menu_cell.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/itemName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="Test"
        android:textAlignment="center"
        android:textColor="#666"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

老实说,我不知道下一步该怎么做。

0 个答案:

没有答案