ViewPagerを使ってとても簡単なテンプレートアプリをセットアップして、断片とViewPager自体に慣れ親しむようにしました。私は、次のフラグメントのクラスが含まれているTabbedActivityクラスを持っている:なぜ私のRecyclerViewはこのフラグメントに何も表示されていませんか?
public static class LinearFragment extends Fragment {
RecyclerView recyclerView;
public ArrayList<AndroidVersion> data;
public DataAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_linear, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.card_recyler_view_pager);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
loadJSON();
return inflater.inflate(R.layout.fragment_linear, container, false);
}
private void loadJSON() {
Retrofit retrofit = MainActivity.getRestAdapter();
RequestInterface request = retrofit.create(RequestInterface.class);
Call<JSONResponse> call = request.getJSON();
call.enqueue(new Callback<JSONResponse>() {
@Override
public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
JSONResponse jsonResponse = response.body();
data = new ArrayList<>(Arrays.asList(jsonResponse.getAndroid()));
adapter = new DataAdapter(data);
recyclerView.setAdapter(adapter);
}
@Override
public void onFailure(Call<JSONResponse> call, Throwable t) {
Log.d("Error", t.getMessage());
}
});
}
}
マイMainActivityは、いくつかのJSONデータを別のRecyclerViewにレトロフィットから戻って表示するための別の非常に基本的なファイルであり、そうなるように、私はMainActivityにパブリック静的メソッドを構築し、私は1つのRetrofitインスタンスを一貫して共有できます。私のMainActivityではJSONが読み込まれ、RecylerViewに表示されますが、何らかの理由でViewPagerとLinearFragmentの部分に移動しても何も起こりません。私のMainActivityで
getRestAdapter()は次のようになります。次のように
public static Retrofit getRestAdapter(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.learn2crack.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
親アクティビティ(TabbedActivity)およびフラグメントのレイアウトファイルは、次のとおりです。
activity_tabbed.xmlを
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.inta.anthony.recylerjsonparsing.TabbedActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
fragment_linear.xml
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context="com.inta.anthony.recylerjsonparsing.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/card_recyler_view_pager"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/view_pager_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/mainActivityButton"/>
</LinearLayout>
ありがとう!
recyclerview 'android:layout_height = "match_parent"' –
アダプタのgetItemcount値を確認してください。それは0ではない? –
私は 'match_parent'を望んでいません。画面の下のボタンを押してしまうからです。私の他のRecyclerViewのlayout_heightは 'wrap_content'で、うまくいきます。そして、はい、私のアダプタは 'recyclerView.setAdapter(アダプタ)'を呼び出す何らかの理由で、サーバからの応答を含んでいます。それは画面に反映されません。 – intA