2016-08-02 4 views
0

Я попытался реализовать пользовательский вид и поместить его в расширяемый вид карты, который содержится внутри RecyclerView. Вначале я открываю его, и это выглядит неплохо.
Перед свитка Before scrollСвойство layout_weight игнорируется в пользовательских представлениях

Но тогда, я оставляю его открытия и перехода к нижней части RecyclerView. Когда я вернусь к элементу, закройте его, а затем снова откройте его, макет сломан.
После свитка After scroll

Ниже приведен код пользовательского вида

<?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="60dp" android:background="?android:attr/selectableItemBackground" 
    android:clickable="true"> 
    <View 
     android:layout_width="@dimen/session_padding_left" 
     android:layout_height="@dimen/session_padding_left"></View> 
    <TextView 
     android:id = "@+id/tv_title" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:textColor="@color/lightGray" 
     android:gravity="center_vertical" 
     android:layout_weight="6" 
     android:maxLines = "2" 
     android:lines="2" 
     android:textSize="@dimen/extra_small_text_size" 
     android:ellipsize="end" 
     android:paddingTop = "10dp" 
     android:paddingBottom = "10dp" 
     android:text = "Word-of-Mouth Behaviour in Mobile Social Media "/> 
    <TextView 
     android:id = "@+id/tv_time" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:gravity="right" 
     android:textSize="@dimen/extra_small_text_size" 
     android:textColor="@color/lightGray" 
     android:layout_weight = "4" 
     android:text="11:30 - 12:00"/> 

    <ImageButton 
     android:layout_width="@dimen/dimen_image_button_add_remove" 
     android:layout_height="@dimen/dimen_image_button_add_remove" 
     android:layout_gravity="center|right" 
     android:scaleType="fitCenter" 
     style = "?android:attr/borderlessButtonStyle" 
     android:src = "@drawable/ic_add_agenda" 
     /> 

</LinearLayout> 

А класс вид:

package au.com.leremede.acis2016.views; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import au.com.leremede.acis2016.R; 

public class PresentationView extends LinearLayout { 
    private TextView tvTitle; 
    private TextView tvTime; 
    public PresentationView(Context context) { 
     super(context); 
     init(context); 
    } 

    public PresentationView(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     init(context); 
    } 


    private void init(Context context) { 
     setOrientation(HORIZONTAL); 
     inflate(context, R.layout.view_presentation, this); 
     tvTitle = (TextView)findViewById(R.id.tv_title); 
     tvTime = (TextView)findViewById(R.id.tv_time); 
    } 


    public void setTitle(String title) 
    { 
     tvTitle.setText(title); 
    } 

    public void setTime(String time) 
    { 
     tvTime.setText(time); 
    } 
} 
+0

Где вы используете PresentationView в макете ?? – sJy

ответ

0

Я решил эту проблему сам, используя библиотеку Процент поддержки

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" 
       android:layout_height="60dp" xmlns:app="http://schemas.android.com/apk/res-auto" 
       android:background="?android:attr/selectableItemBackground" 
       android:clickable="true"> 
    <View 
      android:id="@+id/empty_view" 
      android:layout_width="@dimen/session_padding_left" 
      android:layout_height="@dimen/session_padding_left" 
    ></View> 
    <ImageButton 
      android:id="@+id/btn_add_remove_agenda" 
      android:layout_width="@dimen/dimen_image_button_add_remove" 
      android:layout_height="@dimen/dimen_image_button_add_remove" 
      android:layout_centerVertical="true" 
      android:layout_alignParentRight="true" 
      android:scaleType="fitCenter" 
      style="?android:attr/borderlessButtonStyle" 
      android:src="@drawable/ic_add_agenda" 
    /> 
    <android.support.percent.PercentRelativeLayout android:layout_width="wrap_content" 
                android:layout_height="match_parent" 
                android:layout_toRightOf="@id/empty_view" 
                android:layout_toLeftOf="@id/btn_add_remove_agenda"> 
     <TextView 
       android:id="@+id/tv_title" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:textColor="@color/lightGray" 
       android:layout_alignParentLeft="true" 
       app:layout_widthPercent="60%" 
       android:maxLines="2" 
       android:lines="2" 
       android:textSize="@dimen/extra_small_text_size" 
       android:ellipsize="end" 
       android:paddingTop="10dp" 
       android:paddingBottom="10dp" 
       android:text="Word-of-Mouth Behaviour in Mobile Social Media "/> 
     <TextView 
       android:id="@+id/tv_time" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:gravity="right" 
       android:layout_alignParentRight="true" 
       app:layout_widthPercent="40%" 
       android:textSize="@dimen/extra_small_text_size" 
       android:textColor="@color/lightGray" 
       android:text="11:30 - 12:00"/> 
    </android.support.percent.PercentRelativeLayout> 


</RelativeLayout> 
Смежные вопросы