带有 okHttp 的多窗格多片段

Multipane-Multi Fragments with okHtttp

我是 android 开发人员的新手,我正在尝试为大型 screen.It 创建多窗格,有 3 个 fragments.If 我保留 2 个片段,一切都是 fine.Third 查看片段有 Http Call.Issue 我面临的问题是我无法在详细信息片段下方显示评论片段。

1.Since我是直接调用updateView方法,所有成员变量都没有初始化,虽然在onCreateView方法中赋值了

2.so 我正在使用 getview() 重新分配,但我没有获得屏幕,尽管适配器数大于 0。 代码在移动设备中按预期工作 activity_main.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:baselineAligned="false"
        android:divider="?android:attr/dividerHorizontal"
        android:orientation="horizontal"
        >

        <!--
        This layout is a two-pane layout for the Items master/detail flow.
        -->

        <fragment
            android:id="@+id/fragment_forecast"
            android:name="com.example.android.app.popularmovies.fragmetns.MovieGridFragment"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="2"
            tools:layout="@layout/movie_grid_fragment" />

       <LinearLayout
            android:layout_width="wrap_content"
           android:layout_weight="1"
            android:layout_height="match_parent"
           android:orientation="vertical"
           >
        <FrameLayout
            android:id="@+id/movie_detail_activity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="invisible"
           />
           <FrameLayout
               android:id="@+id/movie_review"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               />
        </LinearLayout>
    </LinearLayout>

评论片段Class

public class MovieReviewFragment extends Fragment {

private ProgressBar progressBar;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private List<MovieReview> movieReviewList;
private static final String LOG_TAG = MovieReviewFragment.class.getSimpleName();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null && savedInstanceState.containsKey("REVIEW_DATA")) {
        movieReviewList = savedInstanceState.getParcelableArrayList("REVIEW_DATA");
    } else {
        movieReviewList = new ArrayList<>();
    }
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.movie_review, container, false);
    Intent intent = getActivity().getIntent();
    if (intent != null && intent.hasExtra(PopularMovieConstants.MOVIE_DATA)) {
        MovieDetails movieDetails = intent.getParcelableExtra(PopularMovieConstants.MOVIE_DATA);
       // initUI(rootView);
        progressBar = ((ProgressBar) rootView.findViewById(R.id.reviewProgessBar));
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.MovieReviewView);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new MovieReviewListAdapter(movieReviewList);
        mRecyclerView.setAdapter(mAdapter);
        if (mAdapter.getItemCount() == 0) {
            getReview(movieDetails);
        } else {
            progressBar.setVisibility(View.GONE);
        }
    }
    return rootView;
}

public void initUI(View view) {
    Log.d(LOG_TAG,"start initView");
    progressBar = ((ProgressBar) view.findViewById(R.id.reviewProgessBar));
    mRecyclerView = (RecyclerView) view.findViewById(R.id.MovieReviewView);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(getContext());
    mRecyclerView.setLayoutManager(mLayoutManager);
    mAdapter = new MovieReviewListAdapter(movieReviewList);
    mRecyclerView.setAdapter(mAdapter);
}


public void updateView(MovieDetails MovieDetails) {
    //initUI(getView());
    progressBar=(ProgressBar)getView().findViewById(R.id.reviewProgessBar);
    mRecyclerView=(RecyclerView) getView().findViewById(R.id.MovieReviewView);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(getContext());
    mRecyclerView.setLayoutManager(mLayoutManager);
    mAdapter = new MovieReviewListAdapter(new ArrayList<MovieReview>());
    mRecyclerView.setAdapter(mAdapter);

    getReview(MovieDetails);
    Log.d(LOG_TAG,"update View Completed");
}
public void getReview(MovieDetails MovieDetails) {
    Log.d(LOG_TAG, "updateView MovieReviewFragment");
    Uri builtUri = Uri.parse(PopularMovieConstants.BASE_URL)
            .buildUpon()
            .appendPath(PopularMovieConstants.MOVIE_APPENDER)
            .appendPath(PopularMovieConstants.MOVIE_PATH)
            .appendPath(Integer.toString(MovieDetails.getId()))
            .appendPath(PopularMovieConstants.REVIEWS)
            .appendQueryParameter(PopularMovieConstants.API_KEY_PARAM, BuildConfig.THE_MOVIE_DB_API_KEY)
            .build();

    Log.d(LOG_TAG, builtUri.toString());
    Request request = new Request.Builder()
            .url(builtUri.toString())
            .build();

    OkHttpClient client = new OkHttpClient();
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException throwable) {
            throwable.printStackTrace();

        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (!response.isSuccessful())
                throw new IOException("Unexpected code " + response);
            Gson gson = new Gson();
            final MovieReviewsDTO reviews = gson.fromJson(response.body().charStream(), MovieReviewsDTO.class);
            Log.d(LOG_TAG, "success");
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ((MovieReviewListAdapter) mAdapter).setGridData(reviews.getResults());
                    Log.d(LOG_TAG, "ui" + ((MovieReviewListAdapter) mAdapter).getItemCount());
                    progressBar.setVisibility(View.GONE);

                }

            });

        }
    });

}

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putParcelableArrayList("REVIEW_DATA"
            , (ArrayList<? extends Parcelable>) ((MovieReviewListAdapter) mAdapter).getGridData());
    super.onSaveInstanceState(outState);
}

}

我认为问题出在框架布局上。这是来自 developer.android.com 的声明。

"FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute. "

改成相对布局,看看能不能显示。

您可以使用您的布局,只需将您的框架布局替换为线性布局或相对布局,具体取决于您的要求。从您的代码中复制并粘贴。

<LinearLayout>
     <fragment
        android:id="@+id/fragment_forecast"
        android:name="com.example.android.app.popularmovies.fragmetns.MovieGridFragment"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="2"
        tools:layout="@layout/movie_grid_fragment" />
      <fragment/>
       <fragment/>
   </LinearLayout>