为啥我的 ListView 显示在一个片段中而不是另一个片段中?

Posted

技术标签:

【中文标题】为啥我的 ListView 显示在一个片段中而不是另一个片段中?【英文标题】:Why is my ListView showing in one fragment and not the other?为什么我的 ListView 显示在一个片段中而不是另一个片段中? 【发布时间】:2019-10-23 19:30:07 【问题描述】:

总而言之,我正在构建一个社交媒体应用程序,它显示:

    一个新闻源,显示来自给定帐户中所有用户个人资料的帖子 时间线显示来自帐户中特定用户个人资料的帖子

我已经构建了一个自定义 BaseAdapter 来填充每个 ListView 中的自定义单元格。 Newsfeed ListView(填充来自帐户中所有用户的帖子)正在正确填充。

时间线列表视图(填充帐户上一个个人资料的帖子)未显示。我设置了断点以确保在填充时间轴 ListView 时我的 ArrayList 不为空或为空。此外,断点验证我的自定义适配器实际上是从 ArrayList 中提取数据并膨胀单元格。但是,ListView 根本不可见。

这是我的 Newsfeed 片段的布局文件(工作正常):

<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_
android:layout_
android:background="@color/colorSkyPrimary">

<android.support.constraint.ConstraintLayout
    android:id="@+id/constraintLayout"
    android:layout_
    android:layout_
    android:background="@android:color/white"
    android:elevation="16dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <EditText
        android:id="@+id/input_post"
        android:layout_
        android:layout_
        android:layout_marginStart="32dp"
        android:layout_marginEnd="32dp"
        android:backgroundTint="@color/colorSkyPrimary"
        android:ems="10"
        android:gravity="start|top"
        android:hint="@string/what_s_going_on"
        android:importantForAutofill="no"
        android:inputType="textMultiLine"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:targetApi="o" />

    <ImageButton
        android:id="@+id/button_photo"
        android:layout_
        android:layout_
        android:layout_marginBottom="8dp"
        android:backgroundTint="@android:color/white"
        android:contentDescription="@string/camera_button"
        app:layout_constraintBottom_toBottomOf="@+id/input_post"
        app:layout_constraintEnd_toEndOf="@+id/input_post"
        app:srcCompat="@drawable/camera_icon" />

    <Button
        android:id="@+id/button_cancel"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_
        android:layout_
        android:layout_marginStart="48dp"
        android:layout_marginTop="8dp"
        android:text="@android:string/cancel"
        android:textColor="@color/colorGrassPrimary"
        android:visibility="gone"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/post_image" />

    <Button
        android:id="@+id/button_update"
        android:layout_
        android:layout_
        android:layout_marginTop="8dp"
        android:layout_marginEnd="48dp"
        android:backgroundTint="@color/colorGrassPrimary"
        android:text="@string/update"
        android:textColor="@color/colorButtonText"
        android:visibility="gone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/post_image" />

    <ImageView
        android:id="@+id/post_image"
        android:layout_
        android:layout_
        android:contentDescription="@string/post_image"
        android:scaleType="fitCenter"
        android:visibility="gone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/input_post"
        tools:srcCompat="@tools:sample/avatars" />
</android.support.constraint.ConstraintLayout>

<ListView
    android:id="@+id/list_newsfeed"
    android:layout_
    android:layout_
    app:layout_constraintBottom_toTopOf="@+id/picker_image"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/constraintLayout"
    app:layout_constraintVertical_bias="0.0" />

<android.support.constraint.ConstraintLayout
    android:id="@+id/picker_image"
    android:layout_
    android:layout_
    android:background="@android:color/white"
    android:elevation="16dp"
    android:visibility="gone"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <android.support.constraint.Guideline
        android:id="@+id/guideline14"
        android:layout_
        android:layout_
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" />

    <ImageButton
        android:id="@+id/button_camera"
        android:layout_
        android:layout_
        android:layout_marginTop="8dp"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="8dp"
        android:background="@android:color/transparent"
        android:contentDescription="@string/camera_button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guideline14"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/camera_icon_large" />

    <ImageButton
        android:id="@+id/button_gallery"
        android:layout_
        android:layout_
        android:layout_marginStart="32dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:background="@android:color/transparent"
        android:contentDescription="@string/gallery_button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline14"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.375"
        app:srcCompat="@drawable/gallery_icon" />
</android.support.constraint.ConstraintLayout>

这是我的 Profile 片段的布局文件(其中 ListView 在运行时未显示)

<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_
android:layout_
android:background="@color/colorSkyPrimary">

<android.support.constraint.ConstraintLayout
    android:id="@+id/constraintLayout4"
    android:layout_
    android:layout_
    android:background="@android:color/white"
    android:elevation="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/profile_photo"
        android:layout_
        android:layout_
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        android:src="@drawable/male_icon_large"
        app:civ_border_color="@android:color/transparent"
        app:civ_border_
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/display_name"
        android:layout_
        android:layout_
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:text="@string/brelynn_mack"
        android:textAlignment="viewStart"
        android:textColor="@color/colorTextDark"
        android:textSize="18sp"
        app:layout_constraintEnd_toStartOf="@+id/button_delete_profile"
        app:layout_constraintStart_toEndOf="@+id/profile_photo"
        app:layout_constraintTop_toTopOf="@+id/profile_photo" />

    <TextView
        android:id="@+id/display_timestamp"
        android:layout_
        android:layout_
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:text="@string/may_14_2019_9_59_pm"
        android:textAlignment="viewStart"
        android:textColor="@color/colorTextLight"
        android:textSize="14sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/profile_photo"
        app:layout_constraintTop_toBottomOf="@+id/display_name" />

    <TextView
        android:id="@+id/display_last_location"
        android:layout_
        android:layout_
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:text="@string/_8741_grouse_run_lane_28314"
        android:textAlignment="viewStart"
        android:textColor="@color/colorTextPrimary"
        android:textSize="12sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/profile_photo"
        app:layout_constraintTop_toBottomOf="@+id/display_timestamp" />

    <ImageButton
        android:id="@+id/button_delete_profile"
        android:layout_
        android:layout_
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:background="@android:color/white"
        android:contentDescription="@string/trash_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/trash_icon" />

    <ImageButton
        android:id="@+id/button_photo"
        android:layout_
        android:layout_
        android:layout_marginStart="8dp"
        android:layout_marginBottom="16dp"
        android:background="@android:color/white"
        android:contentDescription="@string/camera_button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@+id/profile_photo"
        app:layout_constraintTop_toBottomOf="@+id/profile_photo"
        app:srcCompat="@drawable/camera_icon" />

    <ImageButton
        android:id="@+id/button_family"
        android:layout_
        android:layout_
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="8dp"
        android:background="@android:color/white"
        android:contentDescription="@string/family_button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/family_icon_small" />

    <ImageButton
        android:id="@+id/button_gallery"
        android:layout_
        android:layout_
        android:layout_marginEnd="8dp"
        android:background="@android:color/transparent"
        android:contentDescription="@string/gallery_button"
        android:scaleType="fitCenter"
        app:layout_constraintEnd_toEndOf="@+id/profile_photo"
        app:layout_constraintTop_toBottomOf="@+id/profile_photo"
        app:srcCompat="@drawable/gallery_icon" />
</android.support.constraint.ConstraintLayout>

<ListView
    android:id="@+id/list_posts"
    android:layout_
    android:layout_
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/constraintLayout4" />

这是我的自定义适配器:

public class NewsfeedAdapter extends BaseAdapter 



// Class properties
private static final String TAG = "NewsfeedAdapter";

public static final String EXTRA_POSTS = "extra_posts";
public static final String EXTRA_POSITION = "extra_position";

private final Context context;
ArrayList<Post> posts;
Account account;



// Constructor
public NewsfeedAdapter(Context context, ArrayList<Post> posts, Account account) 
    this.context = context;
    this.posts = posts;
    this.account = account;




// System generated methods

@Override
public int getCount() 
    return posts.size();


@Override
public long getItemId(int position) 
    return position;


@Override
public Object getItem(int position) 
    return posts.get(position);


@Override
public View getView(final int position, View convertView, ViewGroup parent) 
    final Post post = posts.get(position);

    if(convertView == null) 
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        convertView = layoutInflater.inflate(R.layout.cell_newsfeed, null);
    

    TextView profileNameDisplay = convertView.findViewById(R.id.display_profile_name);
    String name = post.getPosterName() + " " + account.getFamilyName();
    profileNameDisplay.setText(name);

    TextView timestampDisplay = convertView.findViewById(R.id.display_timestamp);
    SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM dd, yyyy @ hh:mm a", Locale.getDefault());
    String timestamp = dateFormat.format(post.getTimeStamp());
    timestampDisplay.setText(timestamp);

    TextView postMessageDisplay = convertView.findViewById(R.id.display_post_message);
    postMessageDisplay.setText(post.getPostMessage());

    if (post.getHasImage()) 
        AccountUtils.loadProfilePhoto(context, convertView, post.getPosterId());
    

    else 
        ImageView postImage = convertView.findViewById(R.id.display_post_image);
        postImage.setVisibility(View.GONE);
    

    PostUtils.loadPostImage(convertView, post.getPostId());

    ImageButton deleteButton = convertView.findViewById(R.id.button_delete_post);
    ImageButton editButton = convertView.findViewById(R.id.button_edit_post);

    toggleButtons(post, editButton, deleteButton);

    deleteButton.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
            alertBuilder.setTitle(context.getString(R.string.delete_post));
            alertBuilder.setMessage(context.getString(R.string.delete_post_message));

            alertBuilder.setPositiveButton(context.getString(R.string.delete), new DialogInterface.OnClickListener() 
                @Override
                public void onClick(DialogInterface dialog, int which) 
                    posts.remove(position);
                    PostUtils.deletePost(context, post.getPostId(), post.getPosterId());
                    notifyDataSetChanged();
                    PostUtils.listenForNews(context);
                
            );

            alertBuilder.setNegativeButton(context.getString(R.string.cancel), null);

            AlertDialog alert = alertBuilder.create();
            alert.show();
        
    );

    editButton.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            Intent editIntent = new Intent(context, EditPostActivity.class);
            editIntent.putExtra(EXTRA_POSTS, posts);
            editIntent.putExtra(EXTRA_POSITION, position);

            context.startActivity(editIntent);
        
    );

    return convertView;




// Custom methods
private void toggleButtons(Post post, ImageButton editButton, ImageButton deleteButton) 
    long twoMinutes = System.currentTimeMillis() - (2 * 60 * 1000);
    long fiveMinutes = System.currentTimeMillis() - (5 * 60 * 1000);

    if (post.getTimeStamp().getTime() < fiveMinutes) 
        editButton.setVisibility(View.GONE);
    

    else 
        editButton.setVisibility(View.VISIBLE);
    

    if (post.getTimeStamp().getTime() < twoMinutes) 
        deleteButton.setVisibility(View.GONE);
    

    else 
        deleteButton.setVisibility(View.VISIBLE);
    

以下是来自 Newsfeed 片段的生命周期方法,它们加载我的数据并设置适配器:

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
    View view = inflater.inflate(R.layout.fragment_newsfeed, container, false);

    PostUtils.listenForNews(getActivity());

    mPosts = PostUtils.loadNewsfeed(getActivity());

    mNewsfeed = view.findViewById(R.id.list_newsfeed);

    mImagePicker = view.findViewById(R.id.picker_image);
    mPhotoView = view.findViewById(R.id.post_image);

    AccountUtils.listenForUpdates(getActivity());

    setClickListener(view);
    setFocusListener(view);

    return view;


@Override
public void onResume() 
    super.onResume();

    mPosts = PostUtils.loadNewsfeed(getActivity());
    Account account = AccountUtils.loadAccount(getActivity());

    mNewsfeedAdapter = new NewsfeedAdapter(getActivity(), mPosts, account);
    mNewsfeed.setAdapter(mNewsfeedAdapter);

以下是我的个人资料片段中的生命周期方法,其中相同的功能应该适用于稍微不同的数据集:

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
    View view = inflater.inflate(R.layout.fragment_profile, container, false);

    try 
        if(getActivity().getIntent() != null && getActivity().getIntent().getAction().equals(FamilyProfileFragment.ACTION_EDIT_PROFILE)) 
            mEditingSelf = false;
            mIsParent = true;
            mProfile = (Profile) getActivity().getIntent().getSerializableExtra(FamilyProfileFragment.EXTRA_PROFILE);

            String selectedName = mProfile.getFirstName();
            String loadedName = AccountUtils.loadProfile(getActivity()).getFirstName();

            if(selectedName.equals(loadedName)) 
                mEditingSelf = true;
            
        

        else 
            mEditingSelf = true;
            mProfile = AccountUtils.loadProfile(getActivity());

            if(mProfile instanceof Parent) 
                mIsParent = true;
            

            else 
                mIsParent = false;
            
        
    

    catch (Exception e) 
        e.printStackTrace();

        mEditingSelf = true;
        mProfile = AccountUtils.loadProfile(getActivity());

        if(mProfile instanceof Parent) 
            mIsParent = true;
        

        else 
            mIsParent = false;
        
    

    mAccount = AccountUtils.loadAccount(getActivity());
    AccountUtils.loadProfilePhoto(getActivity(), view, mProfile.getProfileId());

    mPhotoView = view.findViewById(R.id.profile_photo);

    PostUtils.listenForTimeline(getActivity(), mProfile);
    mPosts = PostUtils.loadTimeline(getActivity());

    mTimeline = view.findViewById(R.id.list_posts);

    setClickListener(view);
    setTextDisplay(view);
    populateProfile(view);

    return view;


@Override
public void onResume() 
    super.onResume();

    mPosts = PostUtils.loadTimeline(getActivity());

    mNewsfeedAdapter = new NewsfeedAdapter(getActivity(), mPosts, mAccount);
    mTimeline.setAdapter(mNewsfeedAdapter);

【问题讨论】:

【参考方案1】:

我的约束在 Profile 片段中设置不正确。将 XML 更新为以下内容:

<ListView
    android:id="@+id/list_newsfeed"
    android:layout_
    android:layout_
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/constraintLayout4" />

【讨论】:

以上是关于为啥我的 ListView 显示在一个片段中而不是另一个片段中?的主要内容,如果未能解决你的问题,请参考以下文章

为啥数组值出现在 impala 中而不是 hive 中?

使用滑动片段显示数据而不是 ListView

为啥只保存在源中而不是卡片中的条带卡?

片段中的ListView不显示

为啥将 Collections.emptySet() 与泛型一起使用在赋值中而不是作为方法参数?

在 Arraylist 的 listview 的 listitem 上显示值