2017-06-29 12 views
0

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> 

ありがとう!

+0

recyclerview 'android:layout_height = "match_parent"' –

+0

アダプタのgetItemcount値を確認してください。それは0ではない? –

+0

私は 'match_parent'を望んでいません。画面の下のボタンを押してしまうからです。私の他のRecyclerViewのlayout_heightは 'wrap_content'で、うまくいきます。そして、はい、私のアダプタは 'recyclerView.setAdapter(アダプタ)'を呼び出す何らかの理由で、サーバからの応答を含んでいます。それは画面に反映されません。 – intA

答えて

1
  1. データを更新しようとするたびにアダプタを作成しないでください。たとえば、リサイクラーの初期化中にonCreateView()メソッドで設定します。あなたのアダプタでこれを使用することができ、データ更新するには :テスト

+0

クール、ありがとう。私は本当にコンセプトの証明として働くことを試みるだけで、ここで完璧なデザインパターンを実際には行っていませんでした。私は実際にそれを理解した。私は 'return rootView;の代わりに' onCreateView() 'の最後に' return inflater.inflate(R.layout.fragment_linear、container、false);を呼び出していました。それがそれを解決しました。私は答えを投稿します。 – intA

1

のレイアウトにリサイクルビューのための静的な高さを設定してみ

public void updateItems(ArrayList<AndroidVersion> newItems) { 
    if(this.items != null) { 
     this.items.clear() 
     this.items.addAll(newItems) 
     notifyDataSetChanged() 
    } 
} 
  • はそれを考え出しました。愚かな間違いをしてonCreateView()メソッドの末尾にreturn rootView;の代わりにreturn inflater.inflate(R.layout.fragment_linear, container, false);というメソッドが呼び出されましたrootViewオブジェクトは、メソッドの最初に既に作成されています。

  • 関連する問題