[英]Nested Vertical RecycleView of fixed height (Scroll issue)
我設計了這個布局布局,其中我有一個包含 3 個NestdScrollView
的RecycleViews
。 在每個RecycleView
項目上單擊會出現另一個NestedRecycleView
出現在高度為200dp
的ScrollView
內。 NestedRecycleView
滾動不起作用。 在圖像中有超過 4 個“列表項”,但只顯示了 4 個,它不可滾動。 當我滾動父NestdScrollView
滾動時。
有人評論說這是一個糟糕的設計,如果有人知道如何處理這個問題,也請分享你的想法。
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
private List<String> mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
private Context context;
// data is passed into the constructor
MyRecyclerViewAdapter(Context context, List<String> data) {
this.mInflater = LayoutInflater.from(context);
this.mData = data;
this.context=context;
}
// inflates the row layout from xml when needed
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.list_item_text, parent, false);
return new ViewHolder(view);
}
// binds the data to the TextView in each row
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String animal = mData.get(position);
holder.myTextView.setText(animal);
}
// total number of rows
@Override
public int getItemCount() {
return mData.size();
}
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView myTextView;
RecyclerView recyclerViewNested;
ScrollView nestedScrollViewRecycleView;
ViewHolder(View itemView) {
super(itemView);
myTextView = itemView.findViewById(R.id.text_view);
recyclerViewNested = itemView.findViewById(R.id.recycle_view_nested);
nestedScrollViewRecycleView= itemView.findViewById(R.id.nested_scrollview_in_recycleview);
myTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ArrayList<String> list12 = new ArrayList<String>(
Arrays.asList("list item", "list item", "list item","list item","list item", "list item", "list item","list item","list item", "list item", "list item","list item","list item", "list item", "list item","list item","list item", "list item", "list item","list item"));
nestedScrollViewRecycleView.setVisibility(View.VISIBLE);
MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(context, list12);
LinearLayoutManager layoutManager = new LinearLayoutManager(context, recyclerViewNested.VERTICAL, false);
recyclerViewNested.setLayoutManager(layoutManager);
recyclerViewNested.setAdapter(adapter);
}
});
//itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
// convenience method for getting data at click position
String getItem(int id) {
return mData.get(id);
}
// allows clicks events to be caught
void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}
主要活動
public class MainActivity extends AppCompatActivity {
public static final int SWIPE_THRESHOLD = 100;
public static final int SWIPE_VELOCITY_THRESHOLD = 100;
private static final String TAG = "MainActivity";
ArrayList<String> list2;
NestedScrollView NestedScroll1;
ConstraintLayout constraintLayout;
RecyclerView recyclerView1;
//vars
private boolean mSwiping = false;
private float mDownX = 0;
private float xCoOrdinate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView1 = findViewById(R.id.firstColumn);
RecyclerView recyclerView2 = findViewById(R.id.secondColumn);
RecyclerView recyclerView3 = findViewById(R.id.thirdColumn);
NestedScroll1 = findViewById(R.id.scrollview_nested);
ArrayList<String> list12 = new ArrayList<String>(
Arrays.asList("A", "column1", "test1", "1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3"));
list2 = new ArrayList<String>(
Arrays.asList("B", "column3", "test3", "111", "211", "311", "111", "211", "311", "111", "211", "311", "111", "211", "311", "111", "211", "311", "311", "111", "211", "311", "111", "211", "311", "111", "211", "311"));
// set up the RecyclerView
MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(this, list12);
configRecyclerView(recyclerView1, adapter);
configRecyclerView(recyclerView2, adapter);
configRecyclerView(recyclerView3, adapter);
recyclerView2.addOnItemTouchListener(getOnItemTouchListener());
recyclerView3.addOnItemTouchListener(getOnItemTouchListener());
}
private void configRecyclerView(RecyclerView recyclerView, MyRecyclerViewAdapter adapter) {
LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(), recyclerView.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
private RecyclerView.OnItemTouchListener getOnItemTouchListener() {
return new RecyclerView.OnItemTouchListener() {
@SuppressLint("LongLogTag")
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent ev) {
Log.d("TableView OnswipeTouchListener: ", " recycleViewOdds item onInterceptTouchEvent");
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mDownX = ev.getX();
mSwiping = false;
//coordinate for moving
xCoOrdinate = constraintLayout.getX() - ev.getRawX();
break;
case MotionEvent.ACTION_CANCEL:
//return true;
case MotionEvent.ACTION_UP:
if (mSwiping) {
/// this return true block touch event
Log.d("TableView OnswipeTouchListener:", "Recycle view odds OnItemTouchListener: you swiped!");
return true;
}
break;
case MotionEvent.ACTION_MOVE:
float xMove = ev.getX();
float xDelta = Math.abs(xMove - mDownX);
if (xDelta > 5) {
mSwiping = true;
// this one also
return true;
}
break;
}
return false;
}
private Rect rect;
@SuppressLint("LongLogTag")
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent event) {
int limitLeft = constraintLayout.getLeft();
int limitRight = constraintLayout.getRight();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
// Log.d("OnSwipeTouchListener Class", "recycleViewOdds action move");
//Restrict move to constraintLayout width
// Log.d("Recycleview TouchListener windowwidth: ", limitLeft + ", " + limitRight);
float range = event.getRawX() + xCoOrdinate;
if (Math.abs(range) > limitLeft - 350 && Math.abs(range) < limitRight - 250) {
//constraintLayout.scrollTo((int) (event.getRawX() + xCoOrdinate),0);
constraintLayout.animate().x(event.getRawX() + xCoOrdinate).setDuration(0).start();
constraintLayout.setAlpha(0.7f);
}
break;
case MotionEvent.ACTION_UP:
Log.d("TableView OnswipeTouchListener:", "recycleViewOdds action up");
constraintLayout.animate().alpha(1).translationX(0).translationY(0);
break;
case MotionEvent.ACTION_CANCEL:
constraintLayout.animate().alpha(1).translationX(0).translationY(0);
Log.d("TableView OnswipeTouchListener:", "recycleViewOdds action cancel");
break;
}
}
@SuppressLint("LongLogTag")
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
Log.e("TableView OnswipeTouchListener:", "recycleViewOdds item touch intercept disallow request");
}
};
}
}
activity_main.xml
<androidx.core.widget.NestedScrollView
android:id="@+id/scrollview_nested"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/firstColumn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:nestedScrollingEnabled="false"
app:layout_constraintEnd_toStartOf="@id/secondColumn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/secondColumn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
app:layout_constraintEnd_toStartOf="@id/thirdColumn"
app:layout_constraintLeft_toRightOf="@id/firstColumn"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/thirdColumn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toRightOf="@id/secondColumn"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
list_item_text.xml
<?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="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginBottom="8dp"/>
<ScrollView
android:id="@+id/nested_scrollview_in_recycleview"
android:layout_width="match_parent"
android:visibility="gone"
android:background="@color/colorAccent"
android:layout_height="200dp">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycle_view_nested"
android:layout_width="match_parent"
android:nestedScrollingEnabled="false"
android:layout_height="wrap_content"/>
</ScrollView>
</LinearLayout>
我的建議是在“list_item_text”中,在這里,你有一個封裝的滾動視圖和回收器視圖,我假設這些項目的數量在 5-10 左右會非常少,並且大部分會在屏幕上可見,所以不會不是任何性能問題如果我們通過迭代循環直接在父布局中動態添加 LinearLayout,這種方法的唯一缺點是屏幕外的視圖將被加載到 memory 中(不在回收器視圖中) , 但是考慮到你的物品數量,這是可以接受的,你的大部分物品都會在屏幕上可見,而且顯然它會節省 Recyclerview 填充、渲染和所有的額外開銷。 希望它能解決您的觸摸問題。
添加recyclerView.setNestedScrollingEnabled(false);
到所有 RecyclerViews 並檢查是否解決了滾動問題。
activity_main.xml
請在所有 RecyclerView 標簽中添加android:nestedScrollingEnabled="true"
...
android:fillViewport="true"
android:layout_height="wrap_content"
更改為android:layout_height="0dp"
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.