2016-07-10 4 views
1

RecyclerViewをカスタムビューの本体(RefreshableList)に埋め込むことを試みています。カスタムビュー内にRecyclerViewを埋め込む

これは私のプロジェクトの構造である:それは2つのモジュール、アプリ & RLISTが含まれています。

Project view

RLISTモジュールは、私がRecyclerViewを埋め込むしたいカスタムビュー(RefreshableList)を保持しています。

RefreshableList.java(カスタムビュー)

public class RefreshableList extends RelativeLayout { 

    private RecyclerView mRecyclerView; 

    private Context mContext; 
    private MyAdapter mAdapter; 

    public RefreshableList(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     mContext = context; 
     Log.i("ADAPTER", "RefreshableList is initializing views..."); 

     setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
       ViewGroup.LayoutParams.MATCH_PARENT)); 

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(
       Context.LAYOUT_INFLATER_SERVICE); 
     View view = inflater.inflate(R.layout.refreshable_list, null); 

     findViewsById(view); 
     setupRecyclerView(); 
    } 

    private void findViewsById(View view) { 
     mRecyclerView = (RecyclerView) view.findViewById(R.id.dataRecyclerView); 
     Log.i("ADAPTER", "RecyclerView has been initialized."); 
    } 

    private void setupRecyclerView() { 
     mRecyclerView.setHasFixedSize(true); 
     mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext)); 
     Log.i("ADAPTER", "RecyclerView has been setup."); 
    } 

    public void setAdapter(MyAdapter adapter) { 
     mAdapter = adapter; 
     mRecyclerView.setAdapter(mAdapter); 
     Log.i("ADAPTER", "Adapter has been set." + mAdapter.getItemCount()); 
    } 
} 

MyAdapter.javaRecyclerViewのアダプタ)

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { 

    protected Context mContext; 
    protected List<String> mItems; 

    public MyAdapter(Context context, List<String> items) { 
     mContext = context; 
     mItems = new ArrayList<>(items); 
    } 

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View v = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.item_row, parent, false); 
     Log.i("ADAPTER", "Creating row..."); 

     return new ViewHolder(v); 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     holder.label.setText(mItems.get(position)); 
    } 

    @Override 
    public int getItemCount() { 
     return mItems.size(); 
    } 

    class ViewHolder extends RecyclerView.ViewHolder { 

     TextView label; 

     public ViewHolder(View itemView) { 
      super(itemView); 
      label = (TextView) itemView.findViewById(R.id.label); 
     } 
    } 
} 

item_row.xml:(XMLレイアウトRecyclerViewの要素のうち)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="60dp"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="New Text" 
     android:id="@+id/label" 
     android:gravity="left|center_vertical" 
     android:padding="8dp" 
     android:background="#ff0000"/> 
</LinearLayout> 

refreshable_list.xml:RefreshableListカスタムビューのXMLレイアウト)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/dataRecyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</LinearLayout> 

すべてこのコードはRLISTモジュールに属します。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    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:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.zouag.refreshablelist.MainActivity" 
    android:background="#00ff00"> 

    <com.zouag.rlist.RefreshableList 
     android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#ffff00"/> 
</RelativeLayout> 
activity_main.xml

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    @Bind(R.id.list) 
    RefreshableList mRefreshableList; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ButterKnife.bind(this); 

     mRefreshableList.setAdapter(new MyAdapter(
       this, Arrays.asList("Java", "Android", "Python", "Kivy"))); 
    } 
} 

:それをテストするために、私はRefreshableListを埋め込むアプリモジュールにMainActivityを添加しました

アプリケーションを実行すると、RecyclerViewの要素は表示されません。 RecyclerViewは、私がここで何をしないのです

Run

)黄色でマークされ、はっきりと見えるのですか?

答えて

2

あなたのrecyclerviewは表示されていません。あなたのrelativelayoutはです。 ビューグループに膨らんだレイアウトを添付しません。

コール:

View view = inflater.inflate(R.layout.refreshable_list, null); 

だけでレイアウトを膨張させるが、ビューに添付しdosen't。 uがしたいあなたがで膨張させた後、ビューを添付する必要があり、この場合に固執:

this.addView(view) 

それとも呼び出す:

View view = inflater.inflate(R.layout.refreshable_list, this,true); 

ルートビューに膨張し、レイアウトを添付しています。

+0

@MohammedAoufZOUAG:なぜあなたのケースでは 'LayoutInflater.from'が動作しないのですか? 'parent.getContext()'の代わりに 'mContext'を渡してみましたか? –

+0

LayoutInflater.fromによってビューホルダービューを膨張させることに問題はありません。 問題はrecyclerview自身が添付されていないことです... –

関連する問題