【问题标题】:android: how to use an scrolling layout into our recyclerView layout?android:如何在我们的 recyclerView 布局中使用滚动布局?
【发布时间】:2019-02-14 18:49:45
【问题描述】:

我有一个 recyclerview,它的每一行都有一个滚动布局。 滚动布局充满了线性布局的文本视图,它们是从服务器接收的

但是回收器布局中的滚动视图没有滚动(回收器是 滚动良好)。

谁知道是什么问题?!

我的回收站布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="15dp">

    <TextView
        android:id="@+id/txt_payer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:text="@string/payer"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/payer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="15dp"
        android:layout_toLeftOf="@id/txt_payer"
        android:text="@string/payer"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/cost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_marginStart="15dp"
        android:text="@string/cost_t"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/txt_cost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_toRightOf="@id/cost"
        android:text="@string/cost_t"
        android:textColor="#000000" />


    <LinearLayout
        android:id="@+id/linear2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/txt_payer"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:gravity="end"
                android:layout_marginTop="10dp">

                <TextView
                    android:id="@+id/cost_for_me"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="15dp"
                    android:layout_marginTop="15dp"
                    android:text="@string/cost_for_me"
                    android:textColor="#000000" />

                <TextView
                    android:id="@+id/txt_cost_for_me"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="15dp"
                    android:layout_marginTop="15dp"
                    android:text="@string/cost_for_me"
                    android:textColor="#000000" />

            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:gravity="start"
                android:layout_marginTop="15dp">
                <TextView
                    android:id="@+id/calender"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="end"
            android:orientation="horizontal">

            <ScrollView
                android:id="@+id/scroll_list"
                android:layout_width="50dp"
                android:layout_height="100dp"
                android:layout_marginEnd="15dp"
                android:layout_marginTop="15dp">

                <LinearLayout
                    android:id="@+id/partner_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" />
            </ScrollView>

            <TextView
                android:id="@+id/txt_partners"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:text="@string/partners"
                android:textColor="#000000" />


        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#ff0000"
        android:gravity="bottom"
        android:layout_margin="10dp"
        android:orientation="horizontal"
        android:layout_below="@id/linear2"/>

</RelativeLayout>

问题应该在这个标签中:

   <LinearLayout
            android:id="@+id/partner_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />

从服务器接收的文本视图被动态添加到此布局中。

java代码:

recyclerView = (RecyclerView) view.findViewById(R.id.recycler);
final ArrayList<RecyclerViewDataReport> items = new ArrayList<>();
ArrayList<String> name = new ArrayList<>();
name.add("sajad");
name.add("sajad");
name.add("sajad");
name.add("sajad");
name.add("sajad");
name.add("sajad");
name.add("sajad");
name.add("sajad");
name.add("sajad");
name.add("sajad");
name.add("sajad");

for (int i=0 ; i<10 ; i++){
    RecyclerViewDataReport temp = new RecyclerViewDataReport("sajad" , "111111", "99999" , "4444" , name);
    items.add(temp);
}

// 2. set layoutManger
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
// 3. create an adapter
mAdapter = new RecyclerViewAdapterReport(items, getActivity());
// 4. set adapter
recyclerView.setAdapter(mAdapter);
recyclerView.setNestedScrollingEnabled(true);
// 5. set item animator to DefaultAnimator
recyclerView.setItemAnimator(new DefaultItemAnimator());

【问题讨论】:

    标签: android android-recyclerview


    【解决方案1】:

    你需要使用 setNestedScrollingEnabled 方法

     recyclerView.setNestedScrollingEnabled(false);
    

    【讨论】:

      【解决方案2】:

      您需要有效地处理触摸事件:

      //do this after seting adapter in recycler
      
          parent.setOnTouchListener(new View.OnTouchListener() {
      
              public boolean onTouch(View v, MotionEvent event) {
                  findViewById(R.id.inner_scroll).getParent()
                          .requestDisallowInterceptTouchEvent(false);
                  return false;
              }
          });
      
      //do this in bind view holder
      
          innerScrollView.setOnTouchListener(new View.OnTouchListener() {
      
              public boolean onTouch(View v, MotionEvent event) {
                  v.getParent().requestDisallowInterceptTouchEvent(true);
                  return false;
              }
          });
      

      【讨论】:

        猜你喜欢
        • 2015-09-03
        • 1970-01-01
        • 1970-01-01
        • 2015-09-03
        • 2021-05-17
        • 1970-01-01
        • 2021-07-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多