Ran*_*ku' 414 android listview android-listview pull-to-refresh
在Twitter(官方应用程序)等Android应用程序中,当您遇到ListView时,可以将其拉下(并在发布时它会反弹)以刷新内容.
我想知道在您看来,实施该方法的最佳方式是什么?
我能想到的一些可能性:
有什么建议?
PS我想知道官方Twitter应用程序源代码何时发布.有人提到它会被释放,但已经过去了6个月,从那时起我们就没有听说过.
Ran*_*ku' 278
最后,Google发布了一个正式版的pull-to-refresh库!
它SwipeRefreshLayout在支持库中被调用,文档在这里:
1)添加SwipeRefreshLayout为视图的父级,将被视为拉动刷新布局.(我ListView举了一个例子,它可以是任何View像LinearLayout,ScrollView等等)
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/pullToRefresh"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
Run Code Online (Sandbox Code Playgroud)
2)为你的班级添加一个听众
protected void onCreate(Bundle savedInstanceState) {
final SwipeRefreshLayout pullToRefresh = findViewById(R.id.pullToRefresh);
pullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshData(); // your code
pullToRefresh.setRefreshing(false);
}
});
}
Run Code Online (Sandbox Code Playgroud)
您也可以pullToRefresh.setRefreshing(true/false);根据您的要求致电.
Joh*_*son 79
我已尝试实现一个pull to refresh组件,它远未完成,但演示了一个可能的实现,https://github.com/johannilsson/android-pulltorefresh.
主要逻辑在PullToRefreshListView该扩展中实现ListView.在内部,它使用现在更新了小部件,支持1.5及更高版本,请阅读README以获得1.5支持.smoothScrollBy(API级别8)控制标题视图的滚动.
在您的布局中,您只需像这样添加它.
<com.markupartist.android.widget.PullToRefreshListView
android:id="@+id/android:list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
Run Code Online (Sandbox Code Playgroud)
Eri*_*rik 54
我还为Android实现了一个强大的,开源的,易于使用且高度可定制的PullToRefresh库.您可以使用PullToRefreshListView替换ListView,如项目页面上的文档中所述.
https://github.com/erikwt/PullToRefresh-ListView
lar*_*ech 38
我认为最简单的方法是由android支持库提供:
android.support.v4.widget.SwipeRefreshLayout;
一旦导入,您可以按如下方式定义您的布局:
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/refresh"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.v7.widget.RecyclerView
xmlns:recycler_view="http://schemas.android.com/apk/res-auto"
android:id="@android:id/list"
android:theme="@style/Theme.AppCompat.Light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/button_material_light"
>
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
Run Code Online (Sandbox Code Playgroud)
我假设您使用recycleler视图而不是listview.但是,listview仍然有效,所以你只需要用listview替换recyclerview并更新java代码(Fragment)中的引用.
在您的活动片段中,首先实现接口SwipeRefreshLayout.OnRefreshListener:i,e
public class MySwipeFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener{
private SwipeRefreshLayout swipeRefreshLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item, container, false);
swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh);
swipeRefreshLayout.setOnRefreshListener(this);
}
@Override
public void onRefresh(){
swipeRefreshLayout.setRefreshing(true);
refreshList();
}
refreshList(){
//do processing to get new data and set your listview's adapter, maybe reinitialise the loaders you may be using or so
//when your data has finished loading, cset the refresh state of the view to false
swipeRefreshLayout.setRefreshing(false);
}
}
Run Code Online (Sandbox Code Playgroud)
希望这有助于群众
Ara*_*cem 21
在这个链接中,你可以找到一个着名PullToRefresh视图的分支,它有一些新的有趣的实现,PullTorRefreshWebView或者PullToRefreshGridView可以在PullToRefresh列表的下边缘添加一个.
https://github.com/chrisbanes/Android-PullToRefresh
最好的是Android 4.1中完美的工作(正常PullToRefresh不起作用)
小智 18
我有非常简单的方法来做到这一点,但现在确定它的万无一失的方式有我的代码PullDownListView.java
package com.myproject.widgets;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
/**
* @author Pushpan
* @date Nov 27, 2012
**/
public class PullDownListView extends ListView implements OnScrollListener {
private ListViewTouchEventListener mTouchListener;
private boolean pulledDown;
public PullDownListView(Context context) {
super(context);
init();
}
public PullDownListView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public PullDownListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
setOnScrollListener(this);
}
private float lastY;
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
lastY = ev.getRawY();
} else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
float newY = ev.getRawY();
setPulledDown((newY - lastY) > 0);
postDelayed(new Runnable() {
@Override
public void run() {
if (isPulledDown()) {
if (mTouchListener != null) {
mTouchListener.onListViewPulledDown();
setPulledDown(false);
}
}
}
}, 400);
lastY = newY;
} else if (ev.getAction() == MotionEvent.ACTION_UP) {
lastY = 0;
}
return super.dispatchTouchEvent(ev);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
setPulledDown(false);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
public interface ListViewTouchEventListener {
public void onListViewPulledDown();
}
public void setListViewTouchListener(
ListViewTouchEventListener touchListener) {
this.mTouchListener = touchListener;
}
public ListViewTouchEventListener getListViewTouchListener() {
return mTouchListener;
}
public boolean isPulledDown() {
return pulledDown;
}
public void setPulledDown(boolean pulledDown) {
this.pulledDown = pulledDown;
}
}
Run Code Online (Sandbox Code Playgroud)
您只需要在您要使用此ListView的活动上实现ListViewTouchEventListener并设置监听器
我在PullDownListViewActivity中实现了它
package com.myproject.activities;
import android.app.Activity;
import android.os.Bundle;
/**
* @author Pushpan
*
*/
public class PullDownListViewActivity extends Activity implements ListViewTouchEventListener {
private PullDownListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView = new PullDownListView(this);
setContentView(listView);
listView.setListViewTouchListener(this);
//setItems in listview
}
public void onListViewPulledDown(){
Log.("PullDownListViewActivity", "ListView pulled down");
}
}
Run Code Online (Sandbox Code Playgroud)
这个对我有用 :)
man*_*ani 18
要实现android Pull-to-Refresh尝试这段代码,
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/pullToRefresh"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
Run Code Online (Sandbox Code Playgroud)
活动类:
ListView lv = (ListView) findViewById(R.id.lv);
SwipeRefreshLayout pullToRefresh = (SwipeRefreshLayout) findViewById(R.id.pullToRefresh);
lv.setAdapter(mAdapter);
pullToRefresh.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
// TODO Auto-generated method stub
refreshContent();
}
});
private void refreshContent(){
new Handler().postDelayed(new Runnable() {
@Override public void run() {
pullToRefresh.setRefreshing(false);
}
}, 5000);
}
Run Code Online (Sandbox Code Playgroud)
小智 7
请注意,在Android和WP上实现时,需要解决UX问题.
"对于为什么设计师/开发人员不应该在iOS应用程序风格中实现拉动刷新的一个很好的指标是Google和他们的团队在iOS上使用它时从不使用Android上的pull-to-refresh."
https://plus.google.com/109453683460749241197/posts/eqYxXR8L4eb