我正在开发一个Android应用程序,其中我在ListView中添加了一个标题视图。 现在我想只有当用户向下滚动时才显示标题视图 列表显示。我试过跟随,但没有成功实现所需的输出。
即使列表中只有一个项目,我也需要相同的行为。但是当列表中的数据 限制,标题始终可见。
任何建议都将受到高度赞赏。
谢谢, 阿马尔
答案 0 :(得分:0)
使用简单的技巧可以轻松实现此效果,确保在compile 'com.android.support:support-v13:20.0.0'
中包含build.gradle
,或者如果您在Eclipse上,则下载最新的支持jar文件:
使用以下xml布局:
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pullToShowBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:orientation="vertical">
<TextView
android:id="@+id/searchBar"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="-48dp"
android:background="@android:color/darker_gray"
android:gravity="center"
android:text="My Hidden Search Bar" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_world" />
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
这是您的Activity
代码:
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
final View searchBar = findViewById(R.id.searchBar);
final SwipeRefreshLayout sw = (SwipeRefreshLayout) findViewById(R.id.pullToShowBar);
sw.setColorSchemeColors(Color.TRANSPARENT, Color.TRANSPARENT, Color.TRANSPARENT, Color.TRANSPARENT);
sw.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
sw.setRefreshing(false);
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) searchBar.getLayoutParams();
layoutParams.setMargins(0, 0, 0, 0);
searchBar.setLayoutParams(layoutParams);
}
});
}
}
最后一篇:
import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.widget.AbsListView;
public class PullToSearchBarLayout extends SwipeRefreshLayout {
private AbsListView mListView;
public PullToSearchBarLayout(Context context) {
super(context);
}
public PullToSearchBarLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean canChildScrollUp() {
if (mListView == null) {
mListView = (AbsListView) findViewById(android.R.id.list);
}
if (android.os.Build.VERSION.SDK_INT < 14) {
final AbsListView absListView = mListView;
return absListView.getChildCount() > 0
&& (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
.getTop() < absListView.getPaddingTop());
} else {
return ViewCompat.canScrollVertically(mListView, -1);
}
}
}