2016-07-29 19 views
5

tôi đang cố gắng để tạo ra hiệu ứng nàyMake chiều rộng của mặt hàng xem tái chế ngang 70% màn hình rộng

[![Design image][1]][1] 


    [1]: http://i.stack.imgur.com/SCMA5.jpg 

Tôi đang sử dụng một cái nhìn tái chế nhưng vấn đề của tôi, là mỗi thẻ là 100% độ rộng của màn hình tương ứng với 70%.

Đây là mã xml cho từng hạng mục

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:layout_margin="@dimen/scale_20dp" 
    android:id="@+id/rowLayout" 
    > 



     <LinearLayout 
      android:id="@+id/button_parent" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

      <android.support.v7.widget.CardView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/card_view" 
       > 



      <LinearLayout 
       android:id="@+id/currentYear" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="@drawable/paymentscreengrey" 
       android:orientation="vertical" 
       android:layout_gravity="center" 
       android:gravity="center" 
       android:paddingTop="@dimen/scale_50dp" 
       android:paddingLeft="35dp"> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="**** **** **** 5432" 
        android:textSize="@dimen/scale_20dp" 
        android:textColor="@color/white" 
        android:layout_marginTop="@dimen/scale_20dp" 
        /> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="2345" 
        android:textSize="@dimen/scale_16dp" 
        android:textColor="@color/white" /> 



       <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="match_parent" 
        android:orientation="horizontal" 
        android:paddingRight="@dimen/scale_20dp" 
        android:paddingTop="@dimen/scale_15dp" 
        > 




        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="PERSONAL" 
         android:fontFamily="bold" 
         android:textColor="@color/white" 
         android:textSize="18dp" 
         android:textStyle="bold" /> 



        <View 
         android:layout_width="0dp" 
         android:layout_height="0dp" 
         android:layout_weight="1" 
         /> 



        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:text="@string/title_valid_up_to" 
         android:textColor="@color/white" 
         android:textSize="10dp" /> 
        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="12/15" 
         android:layout_marginLeft="10dp" 
         android:textColor="@color/white" 
         android:textSize="@dimen/text_18" 
         /> 



       </LinearLayout> 


      </LinearLayout> 




    </android.support.v7.widget.CardView> 

     </LinearLayout> 



</LinearLayout> 

Dưới đây là file xml cho quan điểm cho rằng giữ quan điểm tái chế

<?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.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/rv" 
     /> 

</LinearLayout> 



and finally the recycler view 

    package com.heyjude.heyjudeapp.adapter; 

    import android.app.Activity; 
    import android.graphics.Point; 
    import android.support.v7.widget.CardView; 
    import android.support.v7.widget.RecyclerView; 
    import android.view.Display; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.LinearLayout; 

    import com.heyjude.heyjudeapp.R; 
    import com.heyjude.heyjudeapp.model.Card; 

    import java.util.ArrayList; 
    import java.util.List; 


    public class CheckOutCardRecycler extends RecyclerView.Adapter<CheckOutCardRecycler.CardViewHolder>{ 

     private List<Card> checkOutCard; 
     private int screenWidth; 


     public CheckOutCardRecycler(Activity activity, List<Card> checkOutCard){ 

      Display display = activity.getWindowManager().getDefaultDisplay(); 
      Point size = new Point(); 
      display.getSize(size); 
      this.screenWidth = size.x; 
      this.checkOutCard = checkOutCard; 
     } 

     public static class CardViewHolder extends RecyclerView.ViewHolder { 
      CardView card; 
      LinearLayout row; 



      CardViewHolder(View itemView) { 
       super(itemView); 
       row = (LinearLayout)itemView.findViewById(R.id.rowLayout); 
       card = (CardView)itemView.findViewById(R.id.card_view); 


      } 
     } 

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

     @Override 
     public CardViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
      View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_checkout_cards, viewGroup, false); 
      CardViewHolder pvh = new CardViewHolder(v); 
      return pvh; 
     } 

     @Override 
     public void onBindViewHolder(CardViewHolder cardViewHolder, int i) { 




      } 

      @Override 
      public void onAttachedToRecyclerView(Recycler View recyclerView) { 
       super.onAttachedToRecyclerView(recyclerView); 
      } 

    } 
+0

thay vì tái chế xem bạn có thể sử dụng xem pager trong đó có tính năng này bạn cần theo mặc định [tham khảo này] (http://stackoverflow.com/questions/9907748/viewpager-get-a-view-of-the-next-page) – SaravInfern

+0

Bạn có thể thêm các mục động vào một người xem không? –

+0

Có, bạn có thể thêm/xóa chế độ xem pro view pager động – SaravInfern

Trả lời

5

Hai cách để làm điều này thực sự.

1) Sử dụng chế độ xem tùy chỉnh cho chế độ xem được tái chế của bạn. Ghi đè onMeasure để trả về chiều rộng của nó là 70 phần trăm của màn hình.

2) Trong bộ điều hợp Recycler View, khi bạn tạo chế độ xem, thiết lập chiều rộng của nó là 70% chiều rộng của màn hình.

Trong cả hai trường hợp, bạn nhận được kích thước màn hình từ Hiển thị và chỉ nhân chiều rộng bằng .7. Trong trường hợp đầu tiên bạn thiết lập nó như chiều rộng đo EXACT, trong lần thứ hai bạn đặt nó trong param layout. Thứ hai có lẽ dễ dàng hơn một chút.

+0

Tôi đã thử 2 và nó không bao giờ làm việc, mặc dù nó thay đổi kích thước của xem tất cả các mục ở phía dưới và bên trái của mục dissapeared tắt màn hình. Tôi sẽ không gặp phải vấn đề tương tự nếu tôi đã thử câu trả lời đầu tiên của bạn? –

-2

Hãy thử thay đổi LinearLayout bằng PercentRelativeLayout để "bọc" RecyclerView và sau đó đặt chiều rộng của nó là 70%.

Thay đổi này:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 

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

</LinearLayout> 

Với điều này:

<android.support.percent.PercentRelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

     <android.support.v7.widget.RecyclerView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      app:layout_widthPercent="70%" 
      android:id="@+id/rv" 
      /> 

EDIT

Từ Hỗ trợ Percent Thư viện đi kèm cùng với Thư viện hỗ trợ Android 23 vì vậy hãy chắc chắn rằng bạn cập nhật Android Hỗ trợ Thư viện trong Trình quản lý SDK lên phiên bản mới nhất (thực tế là 24). Và sau đó thêm một sự phụ thuộc như dưới đây trong build.gradle file:

compile 'com.android.support:percent:24.0.0' 
+0

Không may là không bao giờ làm việc, nó chỉ làm cho người tái chế xem 70% chiều rộng thay vì các mặt hàng bên trong của nó Bạn đã nhìn vào hình ảnh tôi đã thêm vào để hiển thị những gì tôi đang tìm kiếm? –

Các vấn đề liên quan