问题描述
我要运行 StickyListHeaders 在Eclipse中的示例项目。对于我复制粘贴他们所有的课程,布局等。到我的项目,但到了code产生许多错误!
I want to run a sample project of StickyListHeaders in Eclipse. For that I copy-pasted all their classes,layouts,etc. into my project but into that code many errors are generated !
由于code是如此之大,与放大器;有这么多的错误,我不喜欢在这里提问。
As the code is so large & there are so much errors that I don't feel like questioning here.
和我没有发现任何类似的教程在谷歌和放大器;也是在SO,约StickyListHeaders问题受到一些/其他问题。
And I don't find any tutorial like it on google & also in SO, the questions about StickyListHeaders are subjected to some/other problem.
任何人都可以在运行我提供的演示应用程序(教程)实施StickyListHeaders。
Anyone can provide me a demo app (tutorial) implementing StickyListHeaders that runs.
对不起,如果我的问题没有显示任何研究工作,或者如果你认为我强迫别人做一些工作!
Sorry, If my question doesn't show any research effort or If you think that I am forcing someone to do some work !
谢谢..
推荐答案
您也可以使用以下code实现StickyListHeaders,
You can also use below code to achieve 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
接口被实施了 onScroll()
听的滚动列表。 onScroll()
将被调用后,滚动一直completed.There是使用 ViewHolder
适配器存储头视图
,也是一个变量 previousTop
来追踪变化的滚动每个连续的滚动事件。这是因为这一事实,即 offsetTopAndBottom()
修改关系到它的previous位置视图的偏移量。
As you can see above android's AbsListView.OnScrollListener
interface is used by implementing its onScroll()
to listen the scrolling of the list.onScroll()
will be called after the scroll has completed.There is an adapter which uses ViewHolder
to store the header view
and also a variable previousTop
to keep track of the change in the scroll for each successive scroll events .This is because of the fact that offsetTopAndBottom()
changes offset of the view related to the previous location of it.
来到适配器
类,添加(Integer.toString(一));
则只是用来加整数值的的ArrayList
反过来用于显示作为标题。因此,你可以看到在的ListView
50头。 无效android.widget.ArrayAdapter.add(String对象)
将指定对象添加在阵列的末端
Coming to the Adapter
class, add(Integer.toString(i));
is used just to add integer values to an ArrayList
which in turn used to show as a header. As a result you can see 50 headers in the ListView
. void android.widget.ArrayAdapter.add(String object)
Adds the specified object at the end of the array.
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
这对于内容part.You可以使用任何视图它根据您的需要。
The xml below contains a TextView
which is for Header and an ImageView
which is for the content part.You can use any view for it as per your need.
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>
在code是pretty的自我explanatory.Still,如果您需要任何的解释,我会解释。
The code is pretty self explanatory.Still if you need any explanation, I will explain.
您也可以参考下面
HTTP://sriramramani.word$p $ pss.com / 2012/10/17 / Instagram的列表视图/
https://bitbucket.org/ashish2py/instagramheader/downloads
注意:我抄的地方,我不记得这code。如果任何人都可以向我提供的链接,这个code,我会添加引用here.Sorry不给原作者给予信贷。的
这篇关于无法解析从StickyListHeaders错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!