我在主片段上有一个TextView(sub_text),另一个是RecyclerView (rv)包含TextView项。
当我尝试在RecyclerView中选择文本时,我获得了不同数量的项目。 它缺少实施ACTION_PROCESS_TEXT的其他应用程序的选择。
我选择的项目只是部分 - 与stackoverflow上的其他问题不同(例如"android:textIsSelectable="true" not working for TextView in RecyclerView),其中选择完全缺失或无效。
如何使rv的textView中的项与片段的textView相同?
在我的TextView上,我得到以下浮动文本选择工具栏
但是当我尝试从Recycler视图中选择文本时,我得到以下浮动文本选择工具栏
请注意,RV中只有2个可选项?
sub_text的xml是
<TextView
android:id="@+id/sub_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textSize="16sp"
android:padding="8dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:textColor="@color/primary"
android:textIsSelectable="true"
android:visibility="gone" />
rv的xml是
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scrollbars="vertical"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
rv中textview的xml是
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="经"
android:textColor="@color/primary_dark"
android:textIsSelectable="true" />
答案 0 :(得分:3)
文本视图需要启用,可聚焦,longClickable和 textIsSelectable
<TextView
android:id="@+id/textViewHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:enabled="true"
android:textIsSelectable="true"
android:focusable="true"
android:longClickable="true" />
使用波纹管回收站视图代码
<android.support.v7.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/recyclerViewRecordList"
app:layout_constraintTop_toBottomOf="@+id/relativeLayoutHeader"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@+id/view_bottom"
android:layout_margin="10dp"
android:focusable="true"
android:focusableInTouchMode="true">
</android.support.v7.widget.RecyclerView>
在代码中使用SpeedyLinearLayoutManager而不是xml
SpeedyLinearLayoutManager linearLayoutManager = new SpeedyLinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
myRecyclerViewList.setLayoutManager(linearLayoutManager);
RecordAdapter myCustomerAdapter = new RecordAdapter(this, myRecordListData);
myRecyclerViewList.setAdapter(myCustomerAdapter);
public class SpeedyLinearLayoutManager extends LinearLayoutManager {
private static final float MILLISECONDS_PER_INCH = 2f; //default is 25f (bigger = slower)
public SpeedyLinearLayoutManager(Context context) {
super(context);
}
public SpeedyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public SpeedyLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
final LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return SpeedyLinearLayoutManager.this.computeScrollVectorForPosition(targetPosition);
}
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
}
};
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
}