片段在活动中不可见?

时间:2016-05-15 07:44:44

标签: java android xml android-layout android-fragments

我正在尝试在我的工具栏下显示一个片段。该片段包含一个回收者视图。在fragment类中,我设置了适配器。然后适配器应该膨胀card_feed_view.xml布局并在屏幕上显示它。

.add(R.id.fragment_container, fragment)方法似乎无法正常工作,因为片段确实出现(通过添加黑色背景进行测试),但内容(recycleler-view)却没有。我已将fragment_container id添加到我在根元素中添加片段的活动中。

单击操作栏操作时,我还有其他片段可以替换此片段。对于这些事务,我使用.replace(R.id.current_fragment, newFragment)方法。它们也完美地互相替换,但每个片段的内容似乎永远不会出现。

在下面的代码中,为简洁起见,删除了import语句。

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);
        }
    }
}

那么这笔交易是什么?我如何解决这个烦人的错误。

谢谢你们。

0 个答案:

没有答案
相关问题