2016-06-15 14 views
1

RecyclerViewに1つまたは2つのアイテムがあるとします。どのように私は中心に をそれらを表示することができRecyclerViewは、幅と高されている必要がありtopからRecyclerViewの中心に単一のアイテムを合わせるandroid

私の知る限りrecyclerView開始match_parentです。 single itemにどうすればいいですか?gravity = center私は、1つのアイテムが画面の中央になければならないということを意味します。 RecylerViewにはgravity attributeがありません。それ以外の方法はありますか?

wrap_contentの高さに設定できません。また、swipeRefreshLayoutを追加する必要があるためです。

layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    style="@style/MatchMatch" 
    android:background="@color/theme_color_gift_finder"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <android.support.v4.widget.SwipeRefreshLayout 
      android:id="@+id/activity_main_swipe_refresh_layout" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"> 

      <android.support.v7.widget.RecyclerView 
       android:id="@+id/rv_gift_menu" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:padding="@dimen/margin_pad_10" /> 

     </android.support.v4.widget.SwipeRefreshLayout> 

     <LinearLayout 
      android:id="@+id/ll_bottom" 
      style="@style/LinearHorizontal" 
      android:layout_marginBottom="@dimen/margin_pad_16" 
      android:background="@android:color/transparent" 
      android:gravity="center"> 

      <com.netsolace.efc.utility.customviews.widgets.CustomButton 
       android:id="@+id/btn_go" 
       android:layout_width="@dimen/circular_go_n_done_btn_size" 
       android:layout_height="@dimen/circular_go_n_done_btn_size" 
       android:background="@drawable/oval_generic" 
       android:text="@string/go" 
       android:textColor="@color/theme_color_gift_finder" 
       android:textSize="@dimen/text_size_medium" /> 

     </LinearLayout> 

    </LinearLayout> 

    <com.netsolace.efc.utility.customviews.widgets.CustomTextView 
     android:id="@+id/tv_gift_no_result" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:text="No Results" 
     android:textColor="@android:color/white" 
     android:textSize="@dimen/text_size_large" 
     android:textStyle="bold" 
     android:visibility="gone" /> 

</FrameLayout> 

答えて

0

あなたが中心に単一のリスト項目を表示するので考えるならば、私はmatch_parentlayout_heightlayout_widthセットを持っている別のレイアウトを取得することをお勧めして、リスト項目を設定しますリストに1つのアイテムしか含まれていない場合お使いのアダプタのごonCreateViewHolder内側だから、

、この

// Declare a constant named SINGLE_VIEW first, inside your Adapter 
private final int SINGLE_VIEW = 1; 

@Override 
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

    View v; 

    if (viewType == SINGLE_VIEW) { 
     v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_single, parent, false); 
     ViewHolder vh = new ViewHolder(v); 
     return vh; 

    } else { 
     v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false); 
     ViewHolder vh = new ViewHolder(v); 
     return vh; 

    } 
} 

ような何かを次にgetItemViewTypeにあなたがreturn適切なビューにする必要があります。

@Override 
public int getItemViewType(int position) { 
    if(position == 0 && yourList.size() == 1) return SINGLE_VIEW; 
    else return super.getItemViewType(position); 
} 

更新

あなたが画面の中央にあなたのRecyclerViewを置く必要があるなら、あなたはあなたのレイアウトを設計する必要があり、この

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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: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" 
    android:gravity="center"> 

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

などからピックアップするための重要な事柄上記のコードでは、親のgravityを中心に設定し、RecyclerViewの高さをwrap_contentに設定しています。

は、ここで私は1,2または5は、デバイスに示す表示項目に依存かもしれrecyclerView .ITに示す項目を中央にしたい私のテストアプリケーションのスクリーンショット

Here's my test application screenshot

+0

です。私はそれらを中心にしたい。 – Nepster

+0

Reaz Murshed私はRecyclerViewの上にSwipeRefreshがあると言います。 height = wrap_contentを使うとうまくいきません。 – Nepster

関連する問題