带有okHtttp的多重多片段

时间:2016-01-09 10:01:36

标签: android android-fragments tablet okhttp

我是Android dev的新手,我正在尝试为大屏幕创建多个窗格。它有3个片段。如果我保留2个片段,一切都很好。第三个评论片段有Http Call.Issue 问题我正面临我无法在详细信息片段下方显示评论片段。

1.由于我直接调用updateView方法,所有成员变量虽然在onCreateView方法中分配,但未初始化

2.所以我使用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>

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

}

2 个答案:

答案 0 :(得分:0)

我认为问题出在Framelayout上。以下是developer.android.com的声明。

  

“FrameLayout旨在阻挡屏幕上的某个区域以显示单个项目。通常,FrameLayout应该用于保存单个子视图,因为以可扩展到不同的方式组织子视图可能很困难但是,没有子项相互重叠的屏幕大小。但是,您可以使用android:layout_gravity属性,通过为每个子项分配重力,将多个子项添加到FrameLayout并控制它们在FrameLayout中的位置。   “

将其更改为相对布局并查看是否显示。

答案 1 :(得分:0)

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

<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>