0

I am trying to get a fragment to display under my toolbar. The fragment contains a recycler-view. In the fragment class, I setup the adapter. The adapter should then inflate the card_feed_view.xml layout and display it on screen.

The .add(R.id.fragment_container, fragment) method seems to not be working completely, as the fragment does appear (tested this by adding a black background) but the contents (recycler-view) does not. I have added the fragment_container id to the activity that I am adding the fragment to, in the root element.

I also have other fragments that should replace this fragment when the action bar actions are clicked. For those transactions I use the .replace(R.id.current_fragment, newFragment) method. They also replace each other perfectly, however the contents of each fragment never seems to show up.

In the code below the import statements have been removed for brevity.

MainActivity.java:

public class MainActivity extends AppCompatActivity {

private static final String TAG = MainActivity.class.getSimpleName();

private FeedFragment feedFragment = new FeedFragment();
private EventsFragment eventsFragment = new EventsFragment();
private SearchFragment searchFragment = new SearchFragment();
private MoreFragment moreFragment = new MoreFragment();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
    setSupportActionBar(toolbar);

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.add(R.id.fragment_container, feedFragment);
    transaction.commit();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.main_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_feed:
            // Add the feed fragment
            replaceFragment(feedFragment, R.id.fragment_container);
            return true;

        case R.id.action_events:
            // Add the events fragment
            replaceFragment(eventsFragment, R.id.fragment_container);
            return true;

        case R.id.action_search:
            // Add the search fragment
            replaceFragment(searchFragment, R.id.fragment_container);
            return true;

        case R.id.action_more:
            // Add the more fragment
            replaceFragment(moreFragment, R.id.fragment_container);
            return true;

        default:
            // If we got here, the user's action was not recognized.
            // Invoke the superclass to handle it.
            return super.onOptionsItemSelected(item);
    }
}

/**
 * Replaces the current fragment with a new fragment
 *
 * @param newFragment
 * @param currentFragmentId
 */
private void replaceFragment(Fragment newFragment, int currentFragmentId) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(currentFragmentId, newFragment);
    transaction.commit();
}

}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".view.activity.MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/main_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

</RelativeLayout>

FeedFragment.java:

public class FeedFragment extends Fragment {

    public View mView;

    public FeedFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mView = inflater.inflate(R.layout.fragment_feed, container, false);
        FeedAdapter adapter = new FeedAdapter(getContext());
        RecyclerView cardList = (RecyclerView) mView.findViewById(R.id.card_feed);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        cardList.setHasFixedSize(true);
        cardList.setLayoutManager(manager);
        cardList.setAdapter(adapter);
        return mView;
    }
}

fragment_feed.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:orientation="vertical"
    tools:context=".view.fragment.FeedFragment">

    <!-- actual background = #f2f2f2 -->

    <android.support.v7.widget.RecyclerView
        android:id="@+id/card_feed"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

FeedAdapter.java:

public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.ViewHolder> {

    public Context context;

    public FeedAdapter(Context context) {
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.
                from(parent.getContext()).
                inflate(R.layout.card_feed_view, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.infoText.setText("I am here!");
    }

    @Override
    public int getItemCount() {
        return 0;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView profileImage;
        ImageView mainImage;

        TextView infoText;
        TextView postDateText;

        ImageButton likeCountImageButton;
        ImageButton goingImageButton;

        public ViewHolder(View itemView) {
            super(itemView);
            profileImage = (ImageView) itemView.findViewById(R.id.card_profile_image_view);
            mainImage = (ImageView) itemView.findViewById(R.id.card_main_image_view);

            infoText = (TextView) itemView.findViewById(R.id.card_info_text);
            postDateText = (TextView) itemView.findViewById(R.id.date_text_view);

            likeCountImageButton = (ImageButton) itemView.findViewById(R.id.like_count_button);
            goingImageButton = (ImageButton) itemView.findViewById(R.id.going_image_button);
        }
    }
}

So whats the deal? How do I fix this annoying bug.

Thank you guys.

Tom Finet
  • 1,758
  • 6
  • 22
  • 45

0 Answers0