也许我累了,但我错过了代码中发生的事情。我有一个碎片的活动。
我的活动HomeActivity调用一个webservice来填充一个列表(我的业务对象),它执行这个onResume()。
我的片段WalletFragment有一个mehtod refreshCardListView(),它使用适配器来显示列表。它执行这个onResume()。
我所做的直到现在都在起作用:
现在,我发现调用refreshList两次是愚蠢的,所以我尝试将displayWalletFragment从HomeActivity.onCreate()移到was服务的成功回调,并从HomeActivity中删除对refreshCardList的调用,只将其保留在WalletFragment.onResume()中,这样就会这样:
但是此时我的适配器崩溃了,因为parent.getWidth() == 0(而且我需要父级宽度来显示卡片图像)。
我不明白为什么,通过移动这段代码,父视图现在不会被初始化,您有什么想法吗?
这就是我使用的代码,我唯一改变的是从onCreate中删除onCreate(False),并在refreshCardList的成功返回中将它移动,并从那里删除walletFragment.refreshCardListView()。
HomeActivity.java
public class HomeActivity extends Activity {
    CardService cardService = new CardService(this);
    UserService userService = new UserService(this);
    User user = null;
    Set<WegaCard> userCards = null;
    ProfileFragment profileFragment;
    WalletFragment walletFragment;
    /*
     * Saving card images for performance concerns.
     */
    Map<String, Bitmap> cardsImageBitmaps = new HashMap<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initializeComponents();
        styleCompnents();
        displayWalletFragment(false);
    }
    @Override
    protected void onResume() {
        Log.d(this, "fired HomeActivity.onResume()");
        super.onResume();
        user = null;
        userCards = null;
        refreshUser(
                new SingleResultCallback<User>() {
                    @Override
                    public void onResponse(User cards) {
                        Log.d(this, "user: " + user.toString());
                        HomeActivity.this.user = user;
                        profileFragment.refreshUser();
                    }
                },
                new ErrorCallback() {
                    @Override
                    public void onResponse(Throwable error) {
                        Log.e(this, "error: " + error.toString());
                    }
                });
        refreshCardList(
                new SingleResultCallback<Set<WegaCard>>() {
                    @Override
                    public void onResponse(Set<WegaCard> cards) {
                        Log.d(this, "cards: " + cards.toString());
                        userCards = cards;
                        walletFragment.refreshCardListView();
                    }
                },
                new ErrorCallback() {
                    @Override
                    public void onResponse(Throwable error) {
                        Log.e(this, "error: " + error.toString());
                    }
                });
    }
    private void refreshCardList(SingleResultCallback<Set<WegaCard>> successCallback, ErrorCallback errorCallback) {
        Log.d(this, "fired HomeActivity.refreshCardList()");
        // First empty list...
        userCards = new LinkedHashSet<>();
        // ...then fill it back
        cardService.getCards(false, true, successCallback, errorCallback);
    }
    private void refreshUser(SingleResultCallback<User> successCallback, ErrorCallback errorCallback) {
        Log.d(this, "fired HomeActivity.refreshUser()");
        // First empty user...
        userCards = new LinkedHashSet<>();
        // ...then recreate it
        userService.getUser(successCallback, errorCallback);
    }
    public void displayWalletFragment(boolean addToBackStack) {
        displayFragment(WalletFragment.newInstance(), addToBackStack);
    }
    public void displayCardFragment(String cardNumber, boolean addToBackStack) {
        displayFragment(CardFragment.newInstance(cardNumber), addToBackStack);
    }
    public void displayProfileFragment(boolean addToBackStack) {
        displayFragment(ProfileFragment.newInstance(), addToBackStack);
    }
    private void displayFragment(HomeFragment fragment, boolean addToBackStack) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.home_fragment, fragment);
        if (addToBackStack) {
            fragmentTransaction.addToBackStack(null);
        }
        fragmentTransaction.commit();
    }
    public void profileEdit(View view) {
        ActivityLauncher.getInstance().startProfileActivityForResult(this, ProfileActivity.ProfileEditMode.EDIT_PROFILE, user);
    }
    public Map<String, Bitmap> getCardsImageBitmaps() {
        return cardsImageBitmaps;
    }
}WalletFragment.java
public class WalletFragment extends HomeFragment {
    List<WegaCard> cardList;
    ListView cardListView;
    WegaCardAdapter adapter;
    public static WalletFragment newInstance() {
        WalletFragment fragment = new WalletFragment();
        // Bundle args = new Bundle();
        // fragment.setArguments(args);
        return fragment;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        activity.walletFragment = this;
        cardListView = (ListView) getView().findViewById(R.id.fragment_home_wallet_list);
        cardListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                activity.displayCardFragment(cardList.get(position).getCardNumber(), true);
            }
        });
    }
    @Override
    public void onResume() {
        Log.d(this, "fired WalletFragment.onResume()");
        super.onResume();
        refreshCardListView();
    }
    void refreshCardListView() {
        Log.d(this, "fired WalletFragment.refreshCardListView()");
        // First empty list...
        cardList = new ArrayList<>();
        cardList.addAll(activity.userCards);
        adapter = new WegaCardAdapter(activity, R.layout.adapter_card_item, cardList);
        cardListView.setAdapter(adapter);
        getView().findViewById(R.id.fragment_home_wallet_empty).setVisibility(cardList.isEmpty() ? View.VISIBLE : View.GONE);
    }
}WegaCardAdapter.java
public class WegaCardAdapter extends ArrayAdapter<WegaCard> {
    public WegaCardAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<WegaCard> objects) {
        super(context, resource, objects);
    }
    /**
     * Data that should appear in the view should be added here
     */
    private class ViewHolder {
        ImageView imageView;
    }
    /*
     * Note: using layout_height="match_parent" on the ListView helps Android calculate faster
     * the elements that are displayed on screen, and prevent the array adapter for calling
     * getView() too many times, thus improving the display speed
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.d(this, "fired WegaCardAdapter.getView(" + position + ")");
        ViewHolder holder = null;
        WegaCard card = getItem(position);
        LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(android.app.Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_card_item, null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.adapter_card_image);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }
        Bitmap cardImage = null;
        if (getContext() instanceof HomeActivity) {
            Log.d(this, "In home activity \\o/");
            Map<String, Bitmap> savedBitmaps = ((HomeActivity) getContext()).getCardsImageBitmaps();
            if (savedBitmaps.containsKey(card.getCardNumber())) {
                Log.d(this, "Found saved image, using it ^^");
                cardImage = savedBitmaps.get(card.getCardNumber());
            }
            else {
                Log.d(this, "Didn't found saved image éè building and saving it for later!");
                cardImage = card.getRoundedScaledBitmap(getContext(), parent.getWidth());
                savedBitmaps.put(card.getCardNumber(), cardImage);
            }
        }
        else {
            Log.d(this, "Not in home activity?");
            cardImage = card.getRoundedScaledBitmap(getContext(), parent.getWidth());
        }
        holder.imageView.setImageBitmap(cardImage);
        return convertView;
    }
}activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:auto="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
        <!-- Fragment will be displayed here -->
        <LinearLayout
            android:id="@+id/home_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
        </LinearLayout>
    </ScrollView>
</RelativeLayout>fragment_home_wallet.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="match_parent">
    <LinearLayout
        android:id="@+id/fragment_home_wallet_root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center">
        <LinearLayout
            android:id="@+id/fragment_home_wallet_empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center"
            android:visibility="gone">
            <ImageView
                android:src="@drawable/icon_card_white"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <com.gaetanl.aspa.ui.component.StaticTextView
                android:id="@+id/fragment_home_wallet_empty_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/defaultSpacing"
                android:text="@string/dashboard_text_nocard" />
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/defaultSpacing"
                android:gravity="center_vertical"
                android:orientation="horizontal">
                <com.gaetanl.aspa.ui.component.StaticTextView
                    android:id="@+id/fragment_home_wallet_empty_text1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/dashboard_text_tap1" />
                <ImageView
                    android:src="@drawable/icon_plus_white"
                    android:layout_width="@dimen/inlineIcon"
                    android:layout_height="@dimen/inlineIcon"
                    android:layout_marginLeft="@dimen/wordSpacing"
                    android:layout_marginRight="@dimen/wordSpacing" />
                <com.gaetanl.aspa.ui.component.StaticTextView
                    android:id="@+id/fragment_home_wallet_empty_text2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/dashboard_text_tap2" />
            </LinearLayout>
        </LinearLayout>
        <!-- Note: using layout_height="match_parent" on the ListView helps Android calculate faster
         the elements that are displayed on screen, and prevent the array adapter for calling
         getView() too many times, thus improving the display speed -->
        <ListView
            android:id="@+id/fragment_home_wallet_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </ListView>
    </LinearLayout>
</LinearLayout>发布于 2017-05-05 21:01:44
我在这里找到了解决方案:When should I get Width View in Fragment。
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    view.post(new Runnable() {
        @Override
        public void run() {
            // do operations or methods involved
            // View.getWidth(); or View.getHeight();
            // here
        }
    });
}https://stackoverflow.com/questions/43800148
复制相似问题