سؤال

خلفية

نشرت جوجل مؤخرا تحديث لمكتبة الدعم الخاصة به, ، والتي لديها الآن جديد "SwipeRefreshLayout" منظر.

يسمح العرض بالتفاف عرض آخر، مع دعم التمرير لأسفل لإجراء عملية التحديث.

لقطة شاشة:

enter image description here

المشكلة

لم تقدم Google عينة (على الأقل لم أتمكن من العثور عليها حتى الآن)، لذلك حاولت استخدامها بنفسي.

في البداية تعرضت لعطل (NPE) كلما قمت بالتمرير، ولكن بعد ذلك اكتشفت أن السبب هو أنني لم أقدم "OnRefreshListener" لذلك.

لكن ما زلت لا أعرف كيفية استخدامه، ناهيك عن تخصيصه

إليك ملف XML لملف التخطيط:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.swiperefreshlayouttest.MainActivity"
    tools:ignore="MergeRootFrame" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TTT"
                android:textSize="40sp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TTT"
                android:textSize="40sp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TTT"
                android:textSize="40sp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TTT"
                android:textSize="40sp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TTT"
                android:textSize="40sp" />
        </LinearLayout>
    </ScrollView>

</android.support.v4.widget.SwipeRefreshLayout>

الكود، على الرغم من أنه لا يفعل أي شيء خاص على الإطلاق:

public class MainActivity extends ActionBarActivity
  {
  @Override
  protected void onCreate(final Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final SwipeRefreshLayout swipeRefreshLayout=(SwipeRefreshLayout)findViewById(R.id.container);
    swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener()
      {
        @Override
        public void onRefresh()
          {
          // do nothing
          }
      });
    }
  }

السؤال

ما هي الطريقة الصحيحة لاستخدام هذا الرأي؟

كيف يمكنني تخصيصه؟حاليا هو مجرد خط أسود ...

هل كانت مفيدة؟

المحلول

لا أعرف ما ذلك ActionBarActivity الفئة التي تقوم بتوسيعها هي، لكنني جعلتها تعمل بشكل جيد باستخدام FragmentActivity

public class ActivityMain extends FragmentActivity implements OnRefreshListener {

    private SwipeRefreshLayout mSwipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.container);
        mSwipeRefreshLayout.setOnRefreshListener(this);

        super.onCreate(savedInstanceState);
    }

    @Override
    public void onRefresh() {
        Toast.makeText(this, "Refresh", Toast.LENGTH_SHORT).show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.setRefreshing(false);
            }
        }, 2000);
    }
}

تجدر الإشارة إلى أنني قمت بنسخ تخطيط XML الخاص بك ولصقه تمامًا كما هو

فيما يتعلق بالتخصيص، ليس هناك الكثير الذي يمكنك فعله بخلاف تغيير لون الشريط الملون عن طريق استدعاء setColorScheme(int colorResId, int colorResId, int colorResId, int colorResId);

على سبيل المثال

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="blue">#0099CC</color>
    <color name="purple">#9933CC</color>
    <color name="green">#669900</color>
    <color name="orange">#FF8800</color>

</resources>

mSwipeRefreshLayout.setColorScheme(R.color.blue, R.color.purple, R.color.green, R.color.orange);

إنها إضافة مخيبة للآمال حقًا.حساسية التحديث عالية إلى حد ما ولا يوجد إعداد لتغييرها

يحرر

لقد كتبت هذا عندما كان هذا الفصل (و ActionBarActivity class) تمت إضافته للتو إلى sdk.على هذا النحو، تغيرت بعض الأشياء منذ أن كتبت هذه الإجابة.علاوة على ذلك، يجب ألا يؤثر نوع النشاط الذي تستخدمه على هذا الحل.

setColorScheme تم الآن إهمالها، setColorSchemeResources(int... colorResIds) يجب أن تستخدم بدلا من ذلك.(يمكنك وضع أي عدد تريده من معرفات الألوان).

setDistanceToTriggerSync(int distance) يمكن استخدامه أيضًا لتعيين المدى الذي يحتاجه المستخدم للتمرير لأسفل لبدء التحديث.

أوصي بمراجعة الوثائق الرسمية لمعرفة ما يقدمه الفصل أيضًا.

نصائح أخرى

mearactive.java

giveacodicetagpre.

activers_main.xml

giveacodicetagpre.

colors.xml

giveacodicetagpre.

You can by simply following below method (I am using swipelayout on Expandable list ) Test.xml

  <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/RelativeTop"
        android:background="@drawable/listbg" >

        <ExpandableListView
            android:id="@+id/lvExp"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:cacheColorHint="@android:color/transparent"
            android:groupIndicator="@null"
            android:listSelector="@android:color/white" />
    </android.support.v4.widget.SwipeRefreshLayout>

ListHeader.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:background="@drawable/sectionbg"
         android:orientation="horizontal"
         android:padding="8dp" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:contentDescription="@string/contentdescription"
    android:src="@drawable/expandbtn" />

<TextView
    android:id="@+id/lblListHeader"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="3dp"
    android:text="this is test"
    android:textColor="@android:color/white"
    android:textSize="17sp" />

ChildItem.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:background="@color/GrayProductBackground" >

<TextView
    android:id="@+id/lblListItem"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginLeft="35dp"
    android:layout_marginRight="20dp"
    android:gravity="center_vertical"
    android:paddingBottom="5dp"
    android:textColor="@color/GrayTextProduct"
    android:textSize="17sp" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignRight="@+id/lblListItem"
    android:layout_centerVertical="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:contentDescription="@string/arrowdescription"
    android:gravity="center"
    android:src="@drawable/navigationarrow" />

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_alignParentBottom="true"
    android:background="@android:color/white" >
</RelativeLayout>

colors used

<color name="pDarkGreen">#8ec549</color>
<color name="pDarskSlowGreen">#c1f57f</color>
<color name="pLightGreen">#f7ffed</color>
<color name="pFullLightGreen">#d6d4d4</color>

Mainactivity.java

        swipeView = (SwipeRefreshLayout) findViewById(R.id.swipe2);
        swipeView.setColorSchemeResources(R.color.pDarkGreen, R.color.Gray, R.color.Yellow, R.color.pFullLightGreen);

swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {

                        swipeView.setRefreshing(false);

                    }
                    (new Handler()).postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            Log.d("Swipe", "Refreshing Number");

                        }
                    }, 0);
                }
            });

You can set swipeView.setRefreshing(false); to false or true according to your requirment this swipeing mechanisam will work in android's all API level

SwipeRefreshLayout with Kotlin

Xml file code

 <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:layout_width="0dp"
        android:layout_height="0dp" app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="90dp" app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="93dp" app:layout_constraintEnd_toEndOf="parent"
        android:id="@+id/swipe_refresh_layout"
>
    <androidx.recyclerview.widget.RecyclerView
            android:layout_width="0dp"
            android:layout_height="0dp" app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="90dp" app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="93dp" app:layout_constraintEnd_toEndOf="parent"
            android:id="@+id/recyclerView"
    />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

Activity Code

Declare the following code above the oncreate method

    var mSwipeRefreshLayout: SwipeRefreshLayout? = null

Declare the following code inside the oncreate method after setContentView line

mSwipeRefreshLayout= findViewById<SwipeRefreshLayout>(R.id.swipe_refresh_layout)
mSwipeRefreshLayout!!.setOnRefreshListener {
   //API Calls
}

On success API Call or failure API calls

                mSwipeRefreshLayout!!.isRefreshing = false

activity_main.xml

 <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:visibility="gone"/>

MainActivity.java

public class MainActivity extends AppCompatActivity
    implements SwipeRefreshLayout.OnRefreshListener {

      static ViewPager viewPager;

     @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    swipeRefreshLayout.setOnRefreshListener(this);
    swipeRefreshLayout.post(new Runnable() {
                                @Override
                                public void run() {
                                    swipeRefreshLayout.setRefreshing(true);
                                    setupViewPager(viewPager);
                                }
                            }
    );
}



 private void setupViewPager(ViewPager viewPager) {


swipeRefreshLayout.setRefreshing(false);


 }


} 
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top