Twoway视图spannable网格布局参数不起作用

时间:2015-02-26 17:27:44

标签: android android-recyclerview

我正在使用twoway视图作为第一个,我正在努力指定视图的布局属性。我有两种观点,标题和插曲。

这是两者的xml: 显示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="@dimen/margin_16dp"
android:layout_width="248dp"
android:layout_height="160dp"
android:background="@color/black">

 //More views...

</RelativeLayout>

部首:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="54dp"
android:background="@color/red_800">
//More views

我在标题中将高度设置为54dp,为节目设置为160dp,但我得到的是这个,它总是相同(错误)高度,红色条是标题。

http://postimg.org/image/xx1dk45n9/

代码:

public static class ViewHolder extends RecyclerView.ViewHolder{

    public TextView title,date,rate,episode;
    public ImageView image;

    public ViewHolder(View view) {
        super(view);
        image = (ImageView)view.findViewById(R.id.image);
        title = (TextView)view.findViewById(R.id.title);
        date = (TextView)view.findViewById(R.id.date);
        rate = (TextView)view.findViewById(R.id.rate);
        episode = (TextView)view.findViewById(R.id.episode);
    }

}

    public static class ViewHolderHeader extends RecyclerView.ViewHolder{

    public TextView date,children;

    public ViewHolderHeader(View view) {
        super(view);
        date = (TextView)view.findViewById(R.id.date);
        children = (TextView)view.findViewById(R.id.children);
    }

}

public CalendarAdapter(Context context) {
    mContext = context;
    readDateFormat(context);
    simpleDateFormat = new SimpleDateFormat(datePattern, Locale.US);
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    final View view;
    if (viewType == CalendarGridItem.TYPE.SHOW.ordinal()){
        view = LayoutInflater.from(mContext).inflate(R.layout.calendar_grid_tile, parent, false);
        return new ViewHolder(view);
    }
    else {
        view = LayoutInflater.from(mContext).inflate(R.layout.calendar_grid_header, parent, false);
        return new ViewHolderHeader(view);
    }
}


  @Override
 public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    final View view = holder.itemView;
    final CalendarGridItem item = mShows.get(position);
    final SpannableGridLayoutManager.LayoutParams lp =
            (SpannableGridLayoutManager.LayoutParams) view.getLayoutParams();

    if (getItemViewType(position) == CalendarGridItem.TYPE.HEADER.ordinal()){ //Header

        final ViewHolderHeader holder1 = (ViewHolderHeader)holder;

        CalendarHeader header = (CalendarHeader)item;

        Log.d(TAG,"Entrou header: "+header.getDate());

        //Mete a colSpan a 2
        if (lp.colSpan != 2)
            lp.colSpan = 2;

        lp.height = 100; //Even forcing the height doesn't work
        view.setLayoutParams(lp);

    }else{ //Show

        final ViewHolder holder1 = (ViewHolder)holder;
        UpcomingShow show = (UpcomingShow)item;

        if (lp.colSpan != 1)
            lp.colSpan = 1;

        view.setLayoutParams(lp);

    }

}

片段布局:

<org.lucasr.twowayview.widget.TwoWayView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/spanGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
style="@style/TwoWayView"
app:twowayview_layoutManager="SpannableGridLayoutManager"
app:twowayview_numColumns="2"/>

我做错了什么?我不习惯回收者的观点做事,我必须遗漏一些东西。使用Android 4.1.1和4.4的Teste

2 个答案:

答案 0 :(得分:1)

目前有相同的问题(我的是元素都是“正方形”) 似乎无法为项目布局中的元素指定特定的高度/宽度。

当你查看源代码时,在SpannableGridLayoutManager.java文件中,你有方法:LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp)。 在那里,那些是incrimated线:

final LayoutParams spannableLp = new LayoutParams((MarginLayoutParams) lp);
spannableLp.width = LayoutParams.MATCH_PARENT;
spannableLp.height = LayoutParams.MATCH_PARENT;

但是如果你删除它,你就会“破坏”可跨越网格视图的所有逻辑......

答案 1 :(得分:0)

我还认为TwoWayView可以将项目显示为正方形。所以我在源头挖掘并发现,就像Wicha说的那样,两个地方的MATCH_PARENT导致了不良行为。

所以我在SpannableGridLayoutManager文件中进行了以下调整(在checkLayoutParams方法中注明了几行):

 @Override
    public boolean checkLayoutParams(RecyclerView.LayoutParams lp) {
//        if (lp.width != LayoutParams.MATCH_PARENT ||
//            lp.height != LayoutParams.MATCH_PARENT) {
//            return false;
//        }

        if (lp instanceof LayoutParams) {
        (.....)
    }

和这里(使用generateLayoutParams方法中布局参数的宽度和高度):

 @Override
    public LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
        final LayoutParams spannableLp = new LayoutParams((MarginLayoutParams) lp);
        spannableLp.width = lp.width; //LayoutParams.MATCH_PARENT;
        spannableLp.height = lp.height; //LayoutParams.MATCH_PARENT;

        if (lp instanceof LayoutParams) {
        (....)
    }

此外,在您的Adapter类中,执行以下操作:

@Override
    public void onBindViewHolder(ViewHolder viewHolder, final int i) {
        viewHolder.image.setScaleType(ImageView.ScaleType.FIT_XY);
        viewHolder.image.setPadding(5,5,5,5);
        SpannableGridLayoutManager.LayoutParams layoutParams = ((SpannableGridLayoutManager.LayoutParams) viewHolder.itemView.getLayoutParams());
        layoutParams.width = cell_width * item.size; //multiply size
        layoutParams.height = cell_height * item.size;//multiply size
        layoutParams.colSpan = item.size;
        layoutParams.rowSpan = item.size;
        viewHolder.image.setLayoutParams(layoutParams);
}

一些观察结果:

  • cell_width的计算方式类似于纵向的屏幕宽度/ 3和横向的屏幕宽度/ 4。
  • cell_height的计算方式与image_width / image_ratio相同。
  • item.size:我决定对项目使用1:1或2:2或3:3的宽高比,因此item_size是乘法值。