我想运行 StickyListHeaders 的示例项目在 eclipse 中。为此,我复制粘贴了他们所有的类、布局等。进入我的项目,但在该代码中生成了许多错误!
由于代码太大并且错误太多,我不想在这里提问。
我在谷歌上找不到类似的教程,而且在 SO 中,有关 StickyListHeaders 的问题遇到了一些/其他问题。
任何人都可以为我提供一个实现运行的 StickyListHeaders 的演示应用程序(教程)。
抱歉,如果我的问题没有显示出任何研究成果,或者如果您认为我强制某人做一些工作!
谢谢..
最佳答案
您还可以使用下面的代码来实现StickyListHeaders,
MainActivity.java
public class MainActivity extends Activity implements AbsListView.OnScrollListener {
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
list = (ListView) findViewById(R.id.list);
list.setAdapter(new Adapter(this));
list.setOnScrollListener(this);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
//the listview has only few children (of course according to the height of each child) who are visible
for(int i=0; i < list.getChildCount(); i++){
View child = list.getChildAt(i);
ViewHolder holder = (ViewHolder) child.getTag();
//if the view is the first item at the top we will do some processing
if(i == 0){
boolean isAtBottom = child.getHeight() <= holder.header.getBottom();
int offset = holder.previousTop - child.getTop();
if(!(isAtBottom && offset > 0)){
holder.previousTop = child.getTop();
holder.header.offsetTopAndBottom(offset);
holder.header.invalidate();
}
} //if the view is not the first item it "may" need some correction because of view re-use
else if (holder.header.getTop() != 0) {
int offset = -1 * holder.header.getTop();
holder.header.offsetTopAndBottom(offset);
holder.previousTop = 0;
holder.header.invalidate();
}
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {}
private static class Adapter extends ArrayAdapter<String> {
public Adapter(Context context) {
super(context, R.layout.activity_main, R.id.header);
for(int i=0; i < 50; i++){
add(Integer.toString(i));
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
ViewHolder holder = new ViewHolder();
holder.header = (TextView) convertView.findViewById(R.id.header);
convertView.setTag(holder);
}
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.header.setText(getItem(position));
return convertView;
}
}
private static class ViewHolder {
TextView header;
int previousTop = 0;
}
}
正如你在上面看到的,android 的 AbsListView.OnScrollListener
接口(interface)通过实现其 onScroll()
来监听列表的滚动。onScroll()
将在滚动完成后调用。有一个适配器使用 ViewHolder
来存储 header view
以及一个变量 previousTop
跟踪每个连续滚动事件的滚动变化。这是因为 offsetTopAndBottom()
更改了与其先前位置相关的 View 偏移量。
来到Adapter
类,add(Integer.toString(i));
仅用于将整数值添加到ArrayList
其又用于显示为标题。结果,您可以在 ListView
中看到 50 个 header 。 void android.widget.ArrayAdapter.add(String object)
将指定的对象添加到数组末尾。
fragment_main.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="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
下面的 xml 包含一个用于标题的 TextView
和一个用于内容部分的 ImageView
。您可以根据需要使用任何 View 。
activity_main.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="fill_parent"
android:orientation="vertical" android:background="#FFFFFF">
<ImageView
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher"
android:background="#aaaaff"
android:layout_marginTop="40dp"/>
<TextView
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:padding="12dp"
android:text="Deneme Row"
android:textColor="#000000"
android:background="#99ffffff"/>
</RelativeLayout>
代码非常不言自明。如果您需要任何解释,我会解释。
您还可以引用以下内容,
http://sriramramani.wordpress.com/2012/10/17/instagram-list-view/
https://bitbucket.org/ashish2py/instagramheader/downloads
注意:我从我不记得的地方复制了这段代码。如果有人可以向我提供此代码的链接,我将在此处添加引用。很抱歉没有将功劳归给原作者。
关于android - 无法解决 StickyListHeaders 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23382164/