相关疑难解决方法(0)

Android 5.0 - 将页眉/页脚添加到RecyclerView

我花了一些时间试图找出一种方法来添加一个标题RecyclerView,但没有成功.这是我到目前为止所得到的:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...

    layouManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layouManager);

    LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    headerPlaceHolder = inflater.inflate(R.layout.view_header_holder_medium, null, false);
    layouManager.addView(headerPlaceHolder, 0);

   ...
}
Run Code Online (Sandbox Code Playgroud)

LayoutManager似乎是处理的配置对象RecyclerView的项目.因为我无法找到任何addHeaderView(View view)方法,我决定去与LayoutManageraddView(View view, int position)方法,并增加我的头鉴于第一位置,像一个头.

Aaand这是事情变得更加丑陋的地方:

java.lang.NullPointerException: Attempt to read from field 'android.support.v7.widget.RecyclerView$ViewHolder android.support.v7.widget.RecyclerView$LayoutParams.mViewHolder' on a null object reference
    at android.support.v7.widget.RecyclerView.getChildViewHolderInt(RecyclerView.java:2497)
    at android.support.v7.widget.RecyclerView$LayoutManager.addViewInt(RecyclerView.java:4807)
    at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:4803)
    at com.mathieumaree.showz.fragments.CategoryFragment.setRecyclerView(CategoryFragment.java:231)
    at com.mathieumaree.showz.fragments.CategoryFragment.access$200(CategoryFragment.java:47)
    at com.mathieumaree.showz.fragments.CategoryFragment$2.success(CategoryFragment.java:201)
    at com.mathieumaree.showz.fragments.CategoryFragment$2.success(CategoryFragment.java:196)
    at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:41)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135) …
Run Code Online (Sandbox Code Playgroud)

java android android-5.0-lollipop android-recyclerview

120
推荐指数
7
解决办法
12万
查看次数

ConstraintLayout链和文本省略+右侧图像

2018年7月更新:

如果您正在使用ConstraintLayout 1.1.0,则使用正确的属性app:layout_constrainedWidth="true"代替旧的app:layout_constraintWidth_default="wrap"(和高度对应的).查看更新的答案.

2017年11月更新:

如果您使用的是ConstraintLayout 1.0.0稳定版(或以上版本)(此时为1.0.2版),请参阅更新的答案以获得更简单的解决方案,而无需嵌套布局.

原始问题:

使用ConstraintLayouts-Beta3于2016年11月3日发布.(来源)

我正在尝试执行以下操作:

ConstraintLayout 1.1.0

我用这样的布局实现了这个目标:

|                                        |
|<-[TextView]<->[ImageView] -----------> |
|                                        |
Run Code Online (Sandbox Code Playgroud)

这看起来不错,但是当文本比可用空间长时......

app:layout_constrainedWidth="true" 文本视图具有指定以下内容的样式:

  <TextView
      android:id="@+id/textView"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"

      app:layout_constraintHorizontal_chainStyle="packed"

      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintRight_toLeftOf="@+id/caret"
      app:layout_constraintHorizontal_bias="0.0"

      android:text="Some Text"
      android:textAlignment="viewStart"
      android:gravity="start" />

  <ImageView
      android:id="@+id/caret"
      android:layout_width="wrap_content"
      android:layout_height="8dp"
      app:layout_constraintDimensionRatio="1:1"

      app:layout_constraintLeft_toRightOf="@+id/textView"
      app:layout_constraintRight_toRightOf="parent"

      app:layout_constraintTop_toTopOf="@+id/textView"
      app:layout_constraintBottom_toBottomOf="@+id/textView"


      app:layout_constraintHorizontal_bias="0.0"

      android:contentDescription=""
      app:srcCompat="@drawable/ic_selection"
      android:layout_marginStart="8dp"/>
Run Code Online (Sandbox Code Playgroud)

所以它应该工作,但我不确定我需要什么约束让图像滑到右边然后在那里让文本视图理解没有更多的空间.

我错过了什么?

注意:如果我将textview的宽度设置为0dp,它可以工作,但是图像总是在右边(水平偏差似乎被忽略了)

注2:我也尝试过beta2,事实上,似乎Beta3 在可视化编辑器中有一个错误.

更新:我试图在Xcode/AutoLayout中复制它:

这是短文本的外观

短文

现在是相同的布局,我只需在文本视图中键入一个长文本...

长文

正如您可以看到图像视图的轨迹(右)约束所示:您从右边距开始有8个或更多 …

android android-studio android-constraintlayout constraint-layout-chains

31
推荐指数
1
解决办法
7074
查看次数