Search This Blog

Monday, March 28, 2016

LISTVIEW - PUSH REFRESH LISTVIEW



- Tạo layout activity_list_row.xml :

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

xmlns:android="http://schemas.android.com/apk/res/android"    
android:id="@+id/text1"    
android:layout_width="match_parent"    
android:layout_height="wrap_content"    
android:gravity="center_vertical"    
android:minHeight="?android:attr/listPreferredItemHeightSmall"    
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"    
android:paddingStart="?android:attr/listPreferredItemPaddingStart"    
android:textAppearance="?android:attr/textAppearanceListItemSmall"    
android:textColor="@android:color/black"

    />

- Main.xml :

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

    <ScrollView        

      android:layout_width="match_parent"        
      android:layout_height="match_parent" 

>

        <LinearLayout            

          android:layout_width="match_parent"            
          android:layout_height="wrap_content"            
          android:orientation="vertical" 

>

            <TextView                

               android:id="@+id/infoText"                
               android:layout_width="match_parent"                
               android:layout_height="wrap_content"                
               android:layout_marginTop="10dp"                
               android:gravity="center"                
               android:text="Pull Down to Refresh" 

/>

            <ListView                

                android:id="@+id/listView"                
                android:layout_width="match_parent"                
                android:layout_height="424dp"                
                android:layout_marginTop="10dp" 
            >
            </ListView>
        </LinearLayout>

    </ScrollView>

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


- Main.java : 


public class ManHinhChinh extends Activity implements OnRefreshListener {

    private SwipeRefreshLayout swipeView;

    private ListView listView;
    private ArrayAdapter<String> adapter;

    private String[] LIST_ITEM_TEXT_CITIES = { "Los Angeles", "Chicago",
            "Indianapolis", "San Francisco", "Oklahoma City", "Washington" };

    private String[] LIST_ITEM_TEXT_MORE_CITIES = { "Phoenix", "San Antonio",
            "San Jose", "Nashville", "Las Vegas", "Virginia Beach" };

    private List<String> cityList;

    // variable to toggle city values on refresh   

    boolean refreshToggle = true;

    @Override    

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_man_hinh_chinh);

        swipeView = (SwipeRefreshLayout) findViewById(R.id.swipe_view);
        swipeView.setOnRefreshListener(this);
        swipeView.setColorSchemeColors(Color.GRAY, Color.GREEN, Color.BLUE,
                Color.RED, Color.CYAN);
        swipeView.setDistanceToTriggerSync(20);// in dips        
        swipeView.setSize(SwipeRefreshLayout.DEFAULT);// LARGE also can be used
        cityList = Arrays.asList(LIST_ITEM_TEXT_CITIES);
        listView = (ListView) findViewById(R.id.listView);
        adapter = new ArrayAdapter<String>(getApplicationContext(),
                R.layout.activity_list_row, cityList);
        listView.setAdapter(adapter);
        listView.requestLayout();
    }

        Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {

            if (refreshToggle) {
                refreshToggle = false;
                cityList = Arrays.asList(LIST_ITEM_TEXT_MORE_CITIES);
                adapter = new ArrayAdapter<String>(getApplicationContext(),
                        R.layout.activity_list_row, cityList);
            } else {
                refreshToggle = true;
                cityList = Arrays.asList(LIST_ITEM_TEXT_CITIES);
                adapter = new ArrayAdapter<String>(getApplicationContext(),
                        R.layout.activity_list_row, cityList);
            }
            listView.setAdapter(adapter);

            // SỰ KIỆN SAU KHI REFRESH XONG            
            swipeView.postDelayed(new Runnable() {
                @Override                

            public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Đã refesh ", Toast.LENGTH_SHORT).show();
                    swipeView.setRefreshing(false);
                }
            }, 1000);
        };
    };

    @Override    
     public void onRefresh() {

        swipeView.postDelayed(new Runnable() {

            @Override            
            public void run() {
                swipeView.setRefreshing(true);
                handler.sendEmptyMessage(0);
            }
        }, 1000);
    }

}



No comments:

Post a Comment