2017-01-19 2 views
0

xmlを使用せずにRecyclerView行の余白を設定する場合は、2行あり、余裕を動的に変更する必要があるためです。しかし、私がプログラムを起動するときに余裕は設定されていません。私が間違ったことを答えた人がいますか?xmlを使用しないRecyclerViewアダプタの余白を設定する

理解するために私のxmlファイル:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:layout_height="match_parent" 
 
    android:layout_width="match_parent" 
 
    android:focusable="true" 
 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
 
    android:paddingRight="@dimen/activity_horizontal_margin" 
 
    android:clickable="true" 
 
    android:background="?android:attr/selectableItemBackground" 
 
    android:orientation="vertical"> 
 

 
    <TextView 
 
     android:id="@+id/settingTitle" 
 
     android:textColor="@color/colorBlack" 
 
     android:layout_width="match_parent" 
 
     android:textSize="16dp" 
 
     android:layout_height="wrap_content" /> 
 

 
    <TextView 
 
     android:id="@+id/settingSubtitle" 
 
     android:layout_below="@id/settingTitle" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" /> 
 

 
</RelativeLayout>

これは私のアダプタからの私の[OnBind]のである:

@Override 
 
    public void onBindViewHolder(MySettingHolder holder, int position) { 
 
     // Setting for one entry 
 
     Settings setting = settingList.get(position); 
 
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
 

 

 
     // If the settingSubtitle is empty it should be not visible and just the settingTitle 
 
     if (setting.getSettingSubtitle().equals("")) { 
 
      params.setMargins(0, 18, 0, 18); 
 
      holder.settingTitle.setLayoutParams(params); 
 
      holder.settingTitle.setText(setting.getSettingTitle()); 
 
      holder.settingSubtitle.setVisibility(View.GONE); 
 
     } else { 
 
      // Set Margins for settingTitle 
 
      params.setMargins(0, 18, 0, 18); 
 
      holder.settingTitle.setLayoutParams(params); 
 
      // Set Margins for settingSubtitle 
 
      params.setMargins(0, 0, 0, 18); 
 
      holder.settingSubtitle.setLayoutParams(params); 
 
      holder.settingTitle.setText(setting.getSettingTitle()); 
 
      holder.settingSubtitle.setText(setting.getSettingSubtitle()); 
 
     } 
 
    }

+1

paramsをrecyclerviewに設定する必要があります –

+0

@santoshkumarそして、私はここで何をしていると思いますか? – ItsOdi1

+1

recyclerview.setParams(params); –

答えて

0

SOLUTION

私は私の問題を修正取得日の多くを試してみました。他の答えはそれほど役に立たなかった。私の質問は(時々、私が代わりに2のただ一つのTextViewを持っているので)私はRecyclerView行で私のTextViewにXMLずにマージンを設定することができる方法だった。

これはで新しいマージンを定義する方法であるenter image description here

私の状況でアンドロイドでアダプタ:

// Create new LayoutParams object 
 
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
 

 
// Define values in dp and calculate pixels from it because margins are with pixels 
 
int dpValueBottom = 14; 
 
int dpValueLeft = 40; 
 
float d = customContext.getResources().getDisplayMetrics().density; 
 
int marginStandard = (int)(dpValueBottom * d); 
 
int marginLeft = (int)(dpValueLeft * d); 
 

 
holder.settingImage.setImageResource(setting.getSettingImageUrl()); 
 
// If the settingSubtitle is empty it should be not visible and just the settingTitle 
 
if (setting.getSettingSubtitle().equals("")) { 
 
    holder.settingTitle.setText(setting.getSettingTitle()); 
 
    holder.settingSubtitle.setVisibility(View.GONE); 
 

 
    // Get settingTitle TextView 
 
    TextView settingTitle = holder.settingTitle; 
 

 
    // Set margin to params 
 
    params.setMargins(marginLeft, marginStandard, 0, marginStandard); 
 

 
    // Set params to settingTitle 
 
    settingTitle.setLayoutParams(params); 
 

 
} else { 
 
    holder.settingTitle.setText(setting.getSettingTitle()); 
 
    holder.settingSubtitle.setText(setting.getSettingSubtitle()); 
 
}

私はこの答えを持つ人を助けることができると思います。

0

はI understaの場合あなたの問題を正しく見つけてください。それはRecyclerView全体に影響を及ぼし、各行には影響しませんか?以下はその解決策です。

あなたのActivity/FragmentであなたのRecyclerViewを初期化するとき、あなたのアダプタに入る前に、あなたがあなたのRecyclerViewの全体に影響を与えるようにしたい場合は、onBindViewHolderはそれらのウィジェットを使用すると、行ごとに提供してきましたレイアウトを結合し、操作がマージンを設定する必要があります行ごとに、余白があなたのRecyclerViewではなく各行に影響します。

コードの例あなたの活動の内側/フラグメントは、次のようになります。

recyclerView = (RecyclerView) findViewById(R.id.recyclerView); 
recyclerView.setHasFixedSize(true); 
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false); 
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
params.setMargins(0, 18, 0, 18); 
recyclerView.setLayoutParams(params); 
+0

いいえ私は各行に影響を与えたいです。時々私は2つのタイトルと時にはただ1つの行を持っています。だから、各行は、行にあるテキストビューに応じて、自分のマージンが必要です。私のif文を見てください。あなたは私が何を意味するか見ることができます。 – ItsOdi1

+0

問題はありません。私はラップトップに戻ったときに私の答えを編集します。 –

+0

恐ろしいおかげでたくさん:0 – ItsOdi1

関連する問題