我在主 fragment 上有一个 TextView (sub_text),还有另一个 RecyclerView (rv) 包含 TextView 项目。
当我尝试在 RecyclerView 中选择文本时,我得到了不同数量的项目。 它缺少来自其他实现 ACTION_PROCESS_TEXT 的应用程序的选择。
我选择的项目只是部分 - 与 stackoverflow 上的其他问题(例如 "android:textIsSelectable="true" not working for TextView in RecyclerView)不同,在这些问题中选择完全缺失或不起作用。
如何使 rv 的 textView 中的项目与 fragment 的 textView 相同?
在我的 TextView 上,我得到以下 float 文本选择工具栏
但是,当我尝试从 Recycler View 中选择文本时,我得到以下 float 文本选择工具栏
注意到 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" />
最佳答案
Text View need to be enabled, focusable, longClickable and 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" />
use bellow recycler view code
<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>
use SpeedyLinearLayoutManager in code instead of 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);
}
}
关于android - 对于 recyclerView 中的 TextView,textIsSelectable() 在 "floating text selection toolbar"中缺少项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48379079/