2017-08-02 55 views
1

我需要設計一個佈局,其中包含一個或多個卡片。問題在於卡的數量在運行時是已知的,我需要每張卡獲得與其他卡完全相同的高度。所以如果我有兩張卡片,每張卡片將會得到一個weight,0,0Android:在運行時爲孩子分配相同的高度

這個佈局應該在點擊一個卡片時壓下所有其他的卡片。

如何實現這一目標?我應該開發一個自定義視圖嗎?

UPDATE

我已經試過RecyclerView,我說在我的適配器

@Override 
    public void onBindViewHolder(CardViewHolder holder, int position) { 
     Card card = cards.get(position); 
     DisplayMetrics displayMetrics = new DisplayMetrics(); 
     ((AppCompatActivity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 
     holder.tenantId.setText(card.getTenantName()); 
     holder.tenantUsername.setText(card.getUsername()); 
     holder.containerTenant.setBackgroundColor(Color.RED); 
     holder.tenantLogo.setImageResource(card.getImgResource()); 

     holder.itemView.getLayoutParams().height = (int) (displayMetrics.heightPixels/(cards.size() * 1.0f)); 
    } 

以下,但它仍然是推動第二卡並將用戶需要滾動以查看第二張牌。

+1

沒有ü檢查recyclerview的一部分嗎? –

+0

你真的需要一個RecyclerView – lelloman

+0

@RissmonSuresh是的,我已經檢查了recyclerview。我會更新我的問題 –

回答

1

你應該把你的卡放在一個LinearLayout中並以編程方式設置重量。

float cardWeight = 1/numOfCards; 
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
    LayoutParams.MATCH_PARENT, 
    LayoutParams.MATCH_PARENT, 
    cardWeight 
); 
CARD1.setLayoutParams(param); 

LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
    LayoutParams.MATCH_PARENT, 
    LayoutParams.MATCH_PARENT, 
    cardWeight 
); 
CARD2.setLayoutParams(param); 

LINEARPARENT.setWeightSum(1f); 
LINEARPARENT.addView(CARD1); 
LINEARPARENT.addView(CARD2); 

關於下推,你可以設置點擊卡1F(或線性weightsum)的重量和刪除他人和膨脹他們的另一個佈局。

+0

沒有工作,只有第一張牌顯示 –

0

與@yasin答案玩弄後,我找到了解決辦法

MainActivity.java

private LinearLayout mLinearLayout; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main_2); 

     mLinearLayout = (LinearLayout) findViewById(R.id.ll); 

     List<Card> cards = new ArrayList<>(); 
     Card c1 = new Card(R.drawable.ic_1, "Username 1", "Tenant 1"); 
     Card c2 = new Card(R.drawable.ic_2, "Username 2", "Tenant 2"); 
     Card c3 = new Card(R.drawable.ic_3, "Username 3", "Tenant 3"); 
     Card c4 = new Card(R.drawable.ic_4, "Username 4", "Tenant 4"); 
     Card c5 = new Card(R.drawable.ic_5, "Username 5", "Tenant 5"); 

     cards.add(c1); 
     cards.add(c2); 
     cards.add(c3); 
     cards.add(c4); 
     cards.add(c5); 

     mLinearLayout.setWeightSum(1f); 


     for (Card c : cards) { 
      View cardView = getLayoutInflater().inflate(R.layout.tenant_card, null); 
      TextView tvTenant = (TextView) cardView.findViewById(R.id.tv_tenant); 
      TextView tvUsername = (TextView) cardView.findViewById(R.id.tv_username); 
      ImageView imgLogo = (ImageView) cardView.findViewById(R.id.img_logo); 
      tvTenant.setText(c.getTenantName()); 
      tvUsername.setText(c.getUsername()); 
      imgLogo.setImageResource(c.getImgResource()); 
      float cardWeight = 1/(cards.size() * 1.0f); 
      LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT, 
        0, 
        cardWeight 
      ); 


      cardView.setLayoutParams(param); 
      mLinearLayout.addView(cardView); 
     } 
    }